标签:
5 3 15
1 5
2 5
3 5
4 5
5 5
1 5
2 4
3 3
10
对样例的解释
当W 选4 的时候,三个区间上检验值分别为 20、5 、0 ,这批矿产的检验结果为 25,此时与标准值S 相差最小为10。
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<algorithm> using namespace std; const int maxn = 220005; struct STONE{ long long w; long long v; }; long long n,m,s,l[maxn],r[maxn],sum[maxn],sumv[maxn],all,ans = 98765432112345L; STONE stone[maxn]; void input(){ cin>>n>>m>>s; all = 0; for(int i = 1;i <= n;i++){ scanf("%lld%lld",&stone[i].w,&stone[i].v); if(all < stone[i].w) all = stone[i].w; } for(int i = 1;i <= m;i++){ scanf("%lld%lld",&l[i],&r[i]); } } bool check(long long t){ sum[0] = sumv[0] = 0; for(int i = 1;i <= n;i++){ if(stone[i].w >= t){ sumv[i] = sumv[i-1] + stone[i].v; sum[i] = sum[i-1] + 1; }else{ sumv[i] = sumv[i-1]; sum[i] = sum[i-1]; } } all = 0; for(int i = 1;i <= m;i++){ all += (sumv[r[i]] - sumv[l[i]-1]) * (sum[r[i]] - sum[l[i]-1]); } ans = min(abs(all - s),ans); return all < s; } void div(){ long long lans = 0,rans = all,mans; while(lans <= rans){ mans = (lans + rans) >> 1; if(check(mans)){ rans = mans - 1; }else{ lans = mans + 1; } } check(mans+1); if(mans > 0)check(mans-1); cout<<ans; } int main(){ input(); div(); return 0; }
标签:
原文地址:http://www.cnblogs.com/hyfer/p/5656217.html