2014年3月6日 星期四

[HOJ] 3 - 童話故事


#include<stdio.h>
#include<algorithm>
using namespace std;
struct P{
    long long int x,y;
}s[100005];
int n;
long long int tmp=0;
bool check(long long int k){
    int i;
    long long int add=0,mi=0;
    for(i=0;i<n;i++){
        if(s[i].y>k+s[i].x){
            add+=s[i].y-s[i].x-k;
        }
        if(s[i].y<k){
            mi+=k-s[i].y+s[i].x;
        }
    }
    if(add>=mi) return 1;
    return 0;
}
int main(){
    int i;
    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf("%lld%lld",&s[i].x,&s[i].y);
        tmp+=s[i].x;
    }
    long long int l=0,m,r=2147483648LL;
    while(l+1!=r){
        m=(l+r)/2;
        if(check(m)) l=m;
        else r=m;
    }
    printf("%lld\n",l);
}

沒有留言:

張貼留言