95二分求助

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

fanhy @ 2019-01-28 09:08:32

#13是什么鬼

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

const int MAXN = 2000000 + 5;
int n, m;
long long s;
int w[MAXN], v[MAXN];
int l[MAXN], r[MAXN];
int a[MAXN], b[MAXN];
int Left, Right;
long long pre[MAXN];
int all[MAXN];
long long tot;
long long ans = 9223372036854775807;

bool check(int W)
{
    long long cnt, sum;
    tot = 0;
    for (int i = 1; i <= n; i++)
    {
        pre[i] = pre[i - 1];
        all[i] = all[i - 1];
        if (w[i] >= W)
        {
            pre[i] += v[i];
            all[i]++;
        }
    }
    for (int i = 1; i <= m; i++)
    {
        cnt = all[r[i]] - all[l[i] - 1];
        sum = pre[r[i]] - pre[l[i] - 1];
        tot += cnt * sum;
    }
    if (tot - s > 0) return true;
    else return false;
}

int main()
{
    cin >> n >> m >> s;
    for (int i = 1; i <= n; i++) cin >> w[i] >> v[i], Right = max(Right, w[i]);
    for (int i = 1; i <= m; i++) cin >> l[i] >> r[i];
    while (Left + 1 < Right)
    {
        int mid = (Left + Right) / 2;
        if (check(mid)) Left = mid;
        else Right = mid;
        ans = min(ans, abs(tot - s));
    }
    cout << ans << endl;
    return 0;
}

by ziiidan @ 2019-03-17 19:59:42

把主程序第二行那个取max改成加和, 如果还不行的话,就把所得之和再乘上吗, 我比较弱,没办法给您完全解决,根据题意的话,刚开始的右端点应该不是我w[i]的最大值


|