Clever solution to a problem of quality supervision [P1314]

Topic Link

This quality supervisionSooner or later to be laid off, And this data is really the question of water, I tried two approaches gave wrong place later ......

Solution [NOIP2011] smart quality supervision

Title effect: given \ (n-\) th ore, the ore has a weight per \ (W \) , and the value \ (V \) , given \ (m \) intervals \ ([L_i, R_i] \) , a defined interval contribution \ (Y_i = \ sum_j1 \ Times \ sum_j v_J \ Quad J \ in [L_i, R_i] \; and \; w_j> W is = \) , find an optimal parameter \ (W is \) such that \ (\ sum Y \) as close as possible given \ (S \)

Analysis: First, this question in order to understand \ (Y \) value is monotonous , because \ (W \) is smaller, the more we can choose the ore, so the \ (Y \) greater

Then we can go on a bipartite think, first we will enter a given \ (W \) to heavy and sorted in ascending order, then this case we can find a minimum \ (W_i \) such that \ (\ sum Y \ leq S \) , then when \ (W is \) take \ (w_i-1 \) when \ (\ SUM the Y> S \) (borderline cases, i.e. \ (i = 1 \) when not satisfied), then the time we \ (W_i \) and \ (w_ {i-1} \) in enough to take the optimal solution

But pay attention to a problem, we want to \ (w \) is inserted into a collection \ (INF \) value, so that it can represent the situation of all the ore is not selected (so this question is really water data)

still have a question(Brain supplement Daddy voice), How to count \ (\ the Y-SUM \) , if the words of the complexity of violence \ (\) nm directly to heaven (a tree began to write chairman, wrote half thinkPumping his slap),wePleasantly surprisedWhether the number is found to meet the conditions of the ore, or \ (\ sum v \) can all be used to optimize the prefix and so the complexity of a single dichotomous can be done \ (n + m \) levels of

So the complexity of the \ (O ((n + m ) log \; card (w)) \)

\ (cz \) is true gods, do not play table \ (56ms \) is how to run out of Orz

// luogu-judger-enable-o2
#include <algorithm>
#include <cstdio>
#include <cctype>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 100;
inline ll read(){
    ll x = 0;char c = getchar();
    while(!isdigit(c))c = getchar();
    while(isdigit(c))x = x * 10 + c - '0',c = getchar();
    return x;
}
struct Seg{int l,r;}seg[maxn];
struct Stone{int w,v;}stone[maxn];
ll sum_cnt[maxn],sum_v[maxn],w[maxn],s;
int n,m,l = 0x7fffffff,r = -0x7fffffff,tmp;
ll solve(int w){
    ll res = 0;
    for(int i = 1;i <= n;i++)
        if(stone[i].w >= w)sum_cnt[i] = sum_cnt[i - 1] + 1,sum_v[i] = sum_v[i - 1] + stone[i].v;
        else sum_cnt[i] = sum_cnt[i - 1],sum_v[i] = sum_v[i - 1];
    for(int i = 1;i <= m;i++)
        res += (sum_cnt[seg[i].r] - sum_cnt[seg[i].l - 1]) * (sum_v[seg[i].r] - sum_v[seg[i].l - 1]);
    return res;
}
int main(){
    n = read(),m = read(),s = read();
    for(int i = 1;i <= n;i++)
        w[i] = stone[i].w = read(),stone[i].v = read();
    for(int i = 1;i <= m;i++)
        seg[i].l = read(),seg[i].r = read();
    w[n + 1] = 0x7fffffff;//插入一个INF代表所有矿石都不选
    sort(w + 1,w + 2 + n);
    int siz = unique(w + 1,w + 2 + n) - w - 1;//去重 + 排序
    l = 1,r = siz;
    while(l <= r){
        int mid = (l + r) >> 1;
        if(solve(w[mid]) <= s)tmp = mid,r = mid - 1;
        else l = mid + 1;
    }
    ll ans = min(abs(solve(w[tmp]) - s),abs(solve(w[tmp - 1]) - s));//取最优
    return printf("%lld\n",ans),0;
}

Guess you like

Origin www.cnblogs.com/colazcy/p/11515137.html