Since we do not know the value of W required, but we understand that the value of W does not exceed the value of the ore, if W is greater than the value of the ore, then the value of y is 0, the optimal solution cannot be reached.
Therefore, it is easy to think of a two-point method in determining the value of W.
When analyzing this problem, it is easy to know that Y's value is satisfying monotonicity, and when the value of W is greater, the lower the value of Y, because the larger the W, the less ore is available.
So we use the obtained Y value as the judging condition, if Y is smaller than s, it shows the test value, and W takes the big. Give ans the minimum value each time you change the W.
So how should y find out? The N,M has the largest 2*10^5 in the title, and if the burst is definitely timed out, we need to preprocess it when we enumerate W. Iterates over the W array, preserving the prefix and the number of V and the numbers that satisfy the condition. Then iterate over the required interval, i.e. sum + = (Cnt[r[i] [cnt[l[i]-1]) * (Sumv[r[i]]-sumv[l[i]), the total time complexity is changed from O (M*n*log (MAXW)) to O ((m+n) *log (M AXW))
Note that the question is to be opened long long to save
#include <cstdio> #include <cstring> #include <iostream> #define MAXN 200005#define LL Long longusing namespace Std; LL n,m; LL s; LL sum; LL W[MAXN],V[MAXN]; ll cnt[maxn],sumv[maxn];//save the prefix and ll ops[maxn][2];void pre (int limit)//limit for the current enumeration of W{memset (Cnt,0,sizeof cnt); Memset ( Sumv,0,sizeof sumv); sum = 0;for (int i = 1; I <= n; i++) {cnt[i] = cnt[i-1];sumv[i] = sumv[i-1];if (w[i] >= limit) {cnt[ I]++;sumv[i] + = V[i];}} for (int i = 1; I <= m; i++) sum + = (cnt[ops[i][1]]-cnt[ops[i][0]-1]) * (Sumv[ops[i][1]]-sumv[ops[i][0]-1]);} ll Myabs (ll x) {return x>0?x:-x;} int main () {//freopen ("qc14.in", "R", stdin),//freopen ("Qc.out", "w", stdout); LL L = 1,r = 0;scanf ("%lld%lld%lld", &n,&m,&s); for (int i = 1; I <= n; i++) {scanf ("%lld%lld", &w[i],&v [i]); if (W[i] > R) r = W[i];} for (int i = 1; I <= m; i++) {scanf ("%lld%lld", &ops[i][0],&ops[i][1]);} LL mid; LL ans = 100000000000000ll;while (L <= R) {mid = (l+r)/2;pre (mid);//preprocessing and satisfying current W's test value if (Ans > myabs (s-sum)) ans = myABS (S-sum), if (sum < s) R = mid-1;if (Sum > s) L = mid+1;if (sum = = s) break;} printf ("%lld\n", ans);}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
NOIP2011 Intelligent Quality Supervisor (two points)