20分玄关求调!

P1314 [NOIP2011 提高组] 聪明的质监员

smallpeople @ 2024-09-29 08:00:45

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 200005;

ll n,m,s,ans = 0x3f3f3f3f3f3f3f3f;
int w[N],v[N],a[N],b[N],l[N],r[N],lt = 9999999,rt;

int check(int ww)
{
    int sum = 0;
    for(int i = 1;i <= n;i ++)
    {
        a[i] = a[i - 1];
        b[i] = b[i - 1];
        if(w[i] >= ww)a[i] ++,b[i] += v[i];
    }
    for(int i = 1;i <= m;i ++)
        sum += (a[r[i]] - a[l[i] - 1]) * (b[r[i]] - b[l[i] - 1]);
    ans = min(ans,abs(s - sum));
    if(s - sum < 0)return 1;
    else return 0;
}

int main()
{
    cin>>n>>m>>s;
    for(int i = 1;i <= n;i ++)
    {
        cin>>w[i]>>v[i];
        rt = max(rt,w[i]);
        lt = min(lt,w[i]); 
    }
    for(int i = 1;i <= m;i ++)
        cin>>l[i]>>r[i];

    while(lt <= rt)
    {
        int mid = (lt + rt) / 2;
        if(check(mid))lt = mid + 1;
        else rt = mid - 1;
    }
    cout<<ans<<'\n';

    return 0;
}

by fanminghao000 @ 2024-10-11 19:52:55

@smallpeople

注意本题数据范围

第七行定义的前缀和数组b理论最大值为2*10^5*1*10^6=2*10^{11},需要long long

第11行的sum也要long long

遇到数据明显不会卡常的直接用define int long long 不香吗(可能是我太蒟了吧)


by smallpeople @ 2024-10-13 14:21:54

@fanminghao000 。。。(又被long long搞了),已A,感谢


|