Codeforces 1251D Salary Changing

D. Salary Changing
effect: there are n variables, each variable has a value interval, n variables required for this assignment, variables such that n does not exceed the median as large as possible and the S (n always an odd number)
bipartite answer, the median is greater than or equal mid Q can have (n + 1) / 2 are greater than or equal mid variables, greedy ordering get away.

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long i64;
const int maxn = 200002;
struct interval{
    int l, r;
    bool operator < (const interval &B)const{
        return l < B.l;
    }
}E[maxn];
bool ok(int target, int n, i64 S){
    i64 cost = 0;
    int cnt = 0;
    for(int i=n;i>=1;--i){
        if(cnt*2<n&&E[i].r>=target){
            if(E[i].l>=target)cost += E[i].l;
            else cost += target;
            cnt++;
        }else{
            cost += E[i].l;
        }
    }
    return cost<=S && cnt*2>n;

}
void work(){
    i64 S;
    int n;
    scanf("%d%lld", &n, &S);
    for(int i=1;i<=n;++i){
        scanf("%d%d", &E[i].l, &E[i].r);
    }
    sort(E+1, E+n+1);
    int L = E[n/2+1].l;
    int R = 1000000000;
    while(L<=R){
        int mid = (L+R)>>1;
        if(ok(mid, n, S))L = mid + 1;
        else R = mid - 1;
    }
    printf("%d\n", L-1);
}
int main(){
    int t;scanf("%d", &t);
    while(t--){
        work();
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/liu-runda/p/11980200.html