Description
Mr. T is a quality supervisor who recently inspected the quality of a group of minerals. This group of minerals has a total of N ore numbers from 1 to n, each of which has its own weight WI and value VI. The process for mineral testing is:
1. Given m intervals [Li, Ri];
2. Select a parameter W;
3. For an interval [Li, Ri], calculateInspection value Yi:
Yi = Σ 1 * Σ VJ, J ε [Li, Ri] and WJ ≥ W, J is the ore number
This batch of mineralsThe test result Y is the sum of the test values of each interval.. That is, y = Σ Yi, iε [1, m]
If this batch of mineralsInspection resultThere are too many differences with the given standard value s, so we need to test another batch of minerals. T doesn't want to take time to test another batch of minerals, so he wants to adjust the value of parameter wInspection resultAs close as possible to the standard value s, even ifS-YIs the smallest absolute value. Please help find the minimum value.
Format input format
The first line contains three integers, n, m, and s, indicating the number of ores, the number of intervals, and the standard value.
The next n rows have two integers in each line, separated by spaces. line I + 1 indicates the weight WI and value VI of the I ore.
The next m row represents the interval. Each row has two integers separated by spaces. The I + n + 1 row represents the two endpoints Li and RI of the interval [Li, Ri.Note: different intervals may overlap or overlap with each other.
Output Format
The output contains only one row and an integer, indicating the minimum value.
Example 1 input 1 [copy]
5 3 151 52 53 54 55 51 52 43 3
Sample output 1 [copy]
10
Restrictions
1 s
Prompt
Example: When W is 4, the test values of the three intervals are 20, 5, and 0, respectively. The test results of these minerals are 25, at this time, the minimum difference from the standard value S is 10.
For 10% of the data, there are 1 ≤ n, m ≤ 10;
For 30% of data, there are 1 ≤ n, m ≤ 500;
For 50% of data, there are 1 ≤ n, m ≤ 5,000;
For 70% of data, there are 1 ≤ n, m ≤ 10,000;
For 100% of data, there are 1 ≤ n, m ≤ 200,000, 0 <WI, VI ≤ 10 ^ 6, 0 <S ≤ 10 ^ 12, 1 ≤ Li ≤ rI ≤ n.
Source
Noip2011 raise group day2 second question
Question
We can find that with the increase of W, the final y is also increasing, so we can give a try of several W closest to S. The algorithm complexity is O (nlogw. Yes.
Code
1 /*Author:WNJXYK*/ 2 #include<cstdio> 3 #include<iostream> 4 using namespace std; 5 const int Maxn=200000; 6 long long w[Maxn+10],v[Maxn+10]; 7 long long num[Maxn+10],vs[Maxn+10]; 8 int l[Maxn+10],r[Maxn+10]; 9 long long S;10 int n,m;11 long long maxw;12 inline long long abs(long long x){13 if (x<0) return -x;14 return x;15 }16 inline long long getAns(int x){17 num[0]=vs[0]=0;18 for (int i=1;i<=n;i++){19 if (w[i]>=x){20 num[i]=1;21 vs[i]=v[i];22 }else{23 num[i]=0;24 vs[i]=0;25 }26 num[i]+=num[i-1];27 vs[i]+=vs[i-1];28 }29 long long Ans=0;30 for (int i=1;i<=m;i++){31 Ans+=(num[r[i]]-num[l[i]-1])*(vs[r[i]]-vs[l[i]-1]);32 }33 return Ans;34 }35 int main(){36 scanf("%d%d%lld",&n,&m,&S);37 for (int i=1;i<=n;i++) scanf("%lld%lld",&w[i],&v[i]);38 for (int i=1;i<=n;i++) if (maxw<w[i]) maxw=w[i];39 for (int i=1;i<=m;i++) scanf("%d%d",&l[i],&r[i]);40 int left=0,right=maxw;41 while(left+3<right){42 int mid=(left+right)/2;43 if (getAns(mid)>S){44 left=mid;45 }else{46 right=mid;47 }48 } 49 long long Ans=abs(getAns(left)-S);50 for (int i=left+1;i<=right;i++){51 long long tmp=abs(getAns(i)-S);52 if (tmp<Ans)Ans=tmp;53 }54 printf("%lld\n",Ans);55 return 0;56 }
View code
Vijos p1740 smart QC staff