65pts, 求调

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

IL_2 @ 2023-08-25 21:15:44

#include <bits/stdc++.h>
using namespace std;

int n, m;
long long s;
pair<int, int> checks[200005];
int w[200005], v[200005];
long long sum1[200005], sum2[200005];

void initSum(int W)
{
    sum1[0] = w[0]>=W;
    sum2[0] = w[0]>=W ? v[0] : 0;
    for (int i=1; i<n; i++)
    {
        if (w[i]>=W)
        {
            sum1[i] = sum1[i-1]+1;
            sum2[i] = sum2[i-1]+v[i];
        }
        else
        {
            sum1[i] = sum1[i-1];
            sum2[i] = sum2[i-1];
        }
    }
}

long long check(int W)
{
    initSum(W);
    long long tot = 0;
    for (int i=0; i<m; i++)
    {
        int l=checks[i].first, r=checks[i].second;
        long long a = sum1[r]-sum1[l-1];
        long long b = sum2[r]-sum2[l-1];
        tot += a*b;
    }
    return tot;
}

int main()
{
    scanf("%d%d%lld", &n, &m, &s);
    for (int i=0; i<n; i++)
        scanf("%d%d", &w[i], &v[i]);
    for (int i=0; i<m; i++)
    {
        scanf("%d%d", &checks[i].first, &checks[i].second);
        checks[i].first--;
        checks[i].second--;
    }

    int l=0, r=1e6+5, mid;
    long long ans = INT_MAX;
    while (l<=r)
    {
        mid = (l+r)/2;
        long long res = check(mid);
        ans = min(ans, abs(res-s));
        if (res>s)
            l = mid+1;
        else
            r = mid-1;
    }
    // for (mid=0; mid<=1e6; mid++)
    // {
    //     long long res = check(mid);
    //     ans = min(ans, abs(res-s));
    // }
    printf("%lld\n", ans);
    return 0;
}

by IL_2 @ 2023-08-26 20:44:07

解决了,是long long ans = INT_MAX;这行,应为long long ans = INT_MAX;


by xinglili @ 2023-09-21 11:59:17

犯了跟你一样的错误(笑死)


|