Educational Codeforces Round 75 (Rated for Div. 2) D

The meaning of problems

n n intervals, each interval a number taken from the median as large as the last required

The meaning of problems

The first reaction is two points
but I, two points monotony of understanding, poor
I was rejected, I think about enumeration, then I'm gone.

In fact, half of the median, for a median, must be a part in your left, your right part.
If the left part of more than half, indicating that the median is too big.
If more than half of the right part, illustrate the median is too small.
If under normal circumstances, the only remaining l m i d r l\leq mid \leq r interval, and this time take the first left point makes the smallest left to take over half, then take the right half full +1, the median size of the contribution is to the right
if this ratio and s s small, indicating compliance, then take a larger median.
On the other hand, non-compliance, whichever is smaller median.

In the case of guarantee do not exceed the left and right sides, how do you prove it monotonic?
First, the vast majority of contributions certainly take the left point, to take a small part of the median.
Something like this:
a l r b a \quad \quad l \quad r \quad \quad b
a a b b ensure that part of the left and right, first of all they will not overrun, and they must take the left point.
l l r r part is contained in the median part, the left part of the left point to take the right to take the median.
So sort of the original range, must take the first half of the left point.
We consider increasing median, obviously if the prerequisites for the establishment, with median increases, the range will be able to choose more, that is, to become more median.
s u f suf represents the coordinates of the left and the suffixes
A = m i d ( i n 2 ) + s u f [ i + 1 ] A=mid*(i-\frac{n}{2})+suf[i+1]
B = ( m i d + x ) ( i + y n 2 ) + s u f [ i + y ] B=(mid+x)*(i+y-\frac{n}{2})+suf[i+y]
Comparison can be found, the left portion is enlarged, the right part is reduced.
But the right part of the left minus points, all in the left part becomes m i d mid , and m i d mid must be greater than or equal left endpoint.
and m i d mid also becomes m i d + x mid+x , so B A B \ A geq . Monotone.

to sum up

This is the first time with a legal or two points I encountered.
Precisely for illegal situation, apparently to meet the half, it left more than a big explanation, the right more explanation smaller.
In legitimate cases, but also to meet the half, because the left part of the left to take a certain point, the right part with median increases, taking the median changed much, although taken left coordinate reduced, but by then because they are to get the median, so the contribution must be increased.

#include<bits/stdc++.h>
#define FOR(i,l,r) for(int i=l;i<=r;i++)
#define sf(x) scanf("%d",&x)
typedef long long ll;
using namespace std;

const ll mod = 1e9+7;
const int maxn = 2e5+100;

struct node{
    int l,r;
    friend bool operator < (node a,node b){
        if(a.l==b.l)return a.r<b.r;
        return a.l<b.l;
    }
}A[maxn];

int n;ll s;

bool check(int x){
    int lt=0,rt=0;
    ll ret=0;
    //cout<<x<<endl;
    for(int i=1;i<=n;i++){
        if(A[i].r<x)lt++,ret+=A[i].l;
        else if(A[i].l>x)rt++,ret+=A[i].l;
    }
    if(rt>=n/2+1)return true;
    if(lt>=n/2+1)return false;
    for(int i=1;i<=n;i++){
        if(A[i].r<x||A[i].l>x)continue;
        if(lt<n/2)ret+=A[i].l,lt++;
        else ret+=x,rt++;
    }
    return ret<=s;
}

int main(){
    int T;cin>>T;
    while(T--){
        scanf("%d%lld",&n,&s);
        FOR(i,1,n)scanf("%d%d",&A[i].l,&A[i].r);
        sort(A+1,A+1+n);
        int l=1,r=1e9+1,ans=0;
        while(l<=r){
            int mid=(l+r)>>1;
            if(check(mid)){
                ans=max(ans,mid);
                l=mid+1;
            }
            else r=mid-1;
        }
        printf("%d\n",ans);
    }
}

Published 203 original articles · won praise 17 · views 20000 +

Guess you like

Origin blog.csdn.net/mxYlulu/article/details/104246811