数 据 过 水

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

halehu @ 2023-07-02 16:56:18

连二分都没用的算法,纯粹的枚举W的值,不吸氧85pts,吸了氧就过了(#13惊险776ms),显然大数据中相同的w[i]较多,建议加强一些w[i]均不相同的大数据。

#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int N = 2e5 + 5,M = 1e6 + 5;
LL inf = 1e12 + 5;
LL m,n,s,w[N],v[N],l[N],r[N],box[M],num[N],tot[N],sum[N],minn = inf;
int main(){
    scanf("%lld%lld%lld",&n,&m,&s);
    for(int i=1;i<=n;i++) scanf("%lld%lld",&w[i],&v[i]),box[w[i]] ++;
    for(int i=1;i<=m;i++) scanf("%lld%lld",&l[i],&r[i]);
    for(int i=0;i<=1000000;i++)//这里如果不同的w[i]较多,复杂度就上去了
        if(box[i]){
            LL res = 0;
            for(int j=1;j<=n;j++){
                num[j] = (w[j] >= i);
                tot[j] = tot[j-1] + num[j];
                sum[j] = sum[j-1] + num[j] * v[j];
            }
            for(int j=1;j<=m;j++)
                res += (tot[r[j]] - tot[l[j]-1]) * (sum[r[j]] - sum[l[j]-1]);
            minn = min(abs(res - s),minn);
        }
    printf("%lld\n",minn);
    return 0;
}

by rainygame @ 2023-07-10 10:08:28

@halehu 需自己提供数据


|