chen83

· · 个人记录

P1102

lower_bound(a+1,a+1+n,x)

找到第一个大于等于x的数的地址(数组有序)

upper_bound(a+1,a+1+n,x)

找到第一个大于x的数的地址(数组有序)

这题先排序\

a-b=c

可以变形成

a-c=b

枚举a

在二分找到b

就能得到答案

CODE

#include <bits/stdc++.h>
#define int long long
#define fr(i, l, r) for(int i = l; i <= r; ++ i)
using namespace std;
int a[200100],ans;
inline void read(int& a) {
    int s = 0, w = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') w = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        s = s * 10 + ch - '0';
        ch = getchar();
    }
    a = s * w;
}
signed main()
{
    int n,c;
    read(n);
    read(c);
    fr(i,1,n)   
        read(a[i]);
    sort(a+1,a+1+n);//方便二分
    fr(i,1,n)
        ans+=upper_bound(a+1,a+1+n,a[i]-c)-lower_bound(a+1,a+1+n,a[i]-c);//b的数量
    cout<<ans;
    return 0;
}

B - Glider

上升气流可以不下降

否则每前进1格,就下降1高度

只要求出最多经过了多少上升气流

答案就是高度+经过的上升气流数

CODE

#include <bits/stdc++.h>
#define int long long
#define fr(i, l, r) for(int i = l; i <= r; ++ i)
using namespace std;
int s[1001000];
int e[1001000];
int sum[1001000];//非上升气流
int ans[1001000];//上升气流
inline void read(int& a) {
    int s = 0, w = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') w = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        s = s * 10 + ch - '0';
        ch = getchar();
    }
    a = s * w;
}
signed main()
{
    int n,k;
    cin>>n>>k;
    fr(i,1,n){
        cin>>s[i]>>e[i];
        if(i>1)sum[i]=sum[i-1]+(s[i]-e[i-1]);//边界
        ans[i]=ans[i-1]+(e[i]-s[i]);
    }
    int res =0;
    fr(i,1,n){
        int pos=lower_bound(sum+i+1,sum+1+n,k+sum[i])-sum-1;//k+sum[i]是从起点往后飞的距离
        res=max(res,ans[pos]-ans[i-1]);//最多经过的上升气流
    }
    cout<<res+k;
    return 0;
}

C - Romantic Glasses

明显的前缀和

sum1[r]-sum1[l]=sum2[r]-sum2[l]

可以变形成

sum1[r]-sum2[r]=sum1[l]-sum2[l]

所以只要看是不是第一次出现

CODE

#include <bits/stdc++.h>
#define int long long
#define fr(i, l, r) for(int i = l; i <= r; ++ i)
using namespace std;
inline void read(int& a) {
    int s = 0, w = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') w = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        s = s * 10 + ch - '0';
        ch = getchar();
    }
    a = s * w;
}
int a[1001000];
int sum1[1001000];//奇数
int sum2[1001000];//偶数
map<int ,int>cnt;//数组存不下
signed main()
{
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        fr(i,1,n){
            read(a[i]);
            if(i%2==1){
                sum1[i]=sum1[i-1]+a[i];
                sum2[i]=sum2[i-1];
            }
            else{
                sum1[i]=sum1[i-1];
                sum2[i]=sum2[i-1]+a[i];
            }
        }
        bool flag=1;
        fr(r,0,n){
            cnt[sum1[r]-sum2[r]]++;
            if(cnt[sum1[r]-sum2[r]]>1){//不是第一次出现
                cout<<"YES\n";
                flag=0;
                break;//t组
            }
        }
        if(flag)cout<<"NO\n";//不成功
        cnt.clear();//清空
    }
    return 0;
}