65pts,求调。(悲

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

NPH_Zhao @ 2023-08-14 23:32:27

#include <bits/stdc++.h>
#define int long long

using namespace std;

const int MAXN = 2e5 + 5;
int n, m, s, w[MAXN], v[MAXN];
int _max = INT_MIN, _min = INT_MAX, l, r;
int presumv[MAXN], presum[MAXN];
int L[MAXN], R[MAXN];
int sum = 0, ans = INT_MAX;

signed main(){
    cin >> n >> m >> s;
    for(int i = 1; i <= n; i ++){
        cin >> w[i] >> v[i];
        _max = max(_max, w[i]);
        _min = min(_min, w[i]);
    }
    for(int i = 1; i <= m; i ++){
        cin >> L[i] >> R[i];
    }
    int l = _min, r = _max;
    while(l <= r){
        int mid = l + ((r - l) >> 1);
        sum = 0;
        memset(presumv, 0, sizeof(presumv));
        memset(presum, 0, sizeof(presum));
        for(int i = 1; i <= n; i ++){
            if(w[i] >= mid){
                presumv[i] = presumv[i - 1] + v[i];
                presum[i] = presum[i - 1] + 1;
            }
            else{
                presumv[i] = presumv[i - 1];
                presum[i] = presum[i - 1];
            }
        }
        for(int i = 1; i <= m; i ++){
            sum += ((presum[R[i]] - presum[L[i] - 1]) * (presumv[R[i]] - presumv[L[i] - 1]));
        }
        if(sum > s) l = mid + 1;
        else if(sum == s){
            cout << 0 << endl;
            return 0;
        }
        else r = mid - 1;
        if(abs(sum - s) < ans) ans = abs(sum - s);
    }
    cout << ans << endl;
    return 0;
}

by MiPloRAs_3316 @ 2023-08-15 07:44:58

@NPH_Zhao 边界值的问题

- int l = _min, r = _max;
+ int l = _min-1, r = _max+1

还请点个关注喵


by NPH_Zhao @ 2023-08-15 17:54:38

@Brown_Sugar 关注啦,但是还是WA了诶。


by MiPloRAs_3316 @ 2023-08-15 18:36:28

@NPH_Zhao awa,我再看看


by MiPloRAs_3316 @ 2023-08-15 18:38:43

@NPH_Zhao

- int l = _min, r = _max;
+ int l = _min-1, r = _max+1;
- int sum = 0, ans = INT_MAX;
+ int sum = 0, ans = LONG_LONG_MAX;

by MiPloRAs_3316 @ 2023-08-15 18:41:00

@NPH_Zhao 您把 int define 成 long long,那 ans 的初值也应该设置成 long long 的最大值,也就是 LONG_LONG_MAX,如果是 INT_MAX,那 define 就没有意义了


by NPH_Zhao @ 2023-08-15 19:03:31

@Brown_Sugar AC啦,qwq。还是您巨。谢谢!


by NPH_Zhao @ 2023-08-15 19:03:43

ctj


|