Two answer NOIP 2011 smart Quality Supervisor

Meaning: nn ore, each ore has its own weight wiwi, and value vivi. The next four operations are as follows:
1. Given mm intervals [li,ri][li,ri];
2. Select a parameter WW;
3. For an interval [li,ri][li,ri], calculate the ore inspection value Yi = ∑ j1 * ∑ jvj, j ∈ [li,ri] on this interval and WJ > = WYI = ∑ j1 * ∑ jvj, j ∈ [li,ri] and WJ > = w, jj is the ore number.
4. Inspection results of these minerals Y = ∑ miYiY = ∑ imYi

Given a SS, find the minimum value of | S − Y | S − Y |.

We find that the larger the WW value is, the smaller the YY value is, so when y > sy > s, we need to increase the WW value, and vice versa.

For each binary WW value, we can find out Σ j 1 Σ j 1 and Σ jvj Σ jvj according to the requirements, and then enumerate each interval to find the value of YY. Finally, according to the values of YY and SS, it can be judged whether to increase or decrease ww.

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll w[202000],v[202000],sum[202000],le,ri,summ,maxx;
ll n,m,s,l[202000],r[202000],cnt[202000],ans=1e+12;
bool check(ll mid)
{
    for(int i=1;i<=n;++i)
    {
        sum[i]=0;
        cnt[i]=0;
    }
    summ=0;
    for(ll i=1;i<=n;++i)
    {
        if(w[i]>=mid)
        {
            sum[i]=sum[i-1]+v[i];
            cnt[i]=cnt[i-1]+1;
        }
        else
        {
            sum[i]=sum[i-1];
            cnt[i]=cnt[i-1];
        }
    }
    for(ll i=1;i<=m;++i)
    {
        ll x=sum[r[i]]-sum[l[i]-1];
        ll y=cnt[r[i]]-cnt[l[i]-1];
        summ+=x*y;
    }
    if(s<summ)
        return true;
    return false;
}
int main()
{
    cin>>n>>m>>s;
    for(ll i=1;i<=n;++i)
    {
        scanf("%lld%lld",&w[i],&v[i]);
        maxx=max(maxx,w[i]);
    }    
    le=0,ri=maxx;
    for(ll i=1;i<=m;++i)
        scanf("%lld%lld",&l[i],&r[i]);
    ll mid;
    while(ri>=le)
    {
        mid=(le+ri)/2;
        if(check(mid))
            le=mid+1;
        else
            ri=mid-1;
        if(ans>llabs(s-summ))
            ans=llabs(s-summ);
    }
    cout<<ans;
    return 0;
}

Posted by dast on Fri, 03 Jan 2020 00:54:44 -0800