CEOI2017 day1-Sure Bet【三分】

从大到小排序,枚举a中选几个,在b中三分。

代码

#include
#include
#include
#define maxn 100006
using namespace std;
int n;
double ans,a[maxn],b[maxn];
bool cmp(double x,double y){return x>y;}
double find(int x,double y){
    int l=1,r=n;
    while(r-l>=15){
        int t=(r-l+1)/3,mid1=l+t/3,mid2=r-t/3;
        if(min(y,b[mid1])-mid1>min(y,b[mid2])-mid2)r=mid2;
                                              else l=mid1;
    }
    double sum=-1e9;
    for(int i=l;i<=r;i++)sum=max(sum,min(y,b[i])-i-x);
    return sum;
}
int main(){
    freopen("b.in","r",stdin);
    freopen("b.out","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%lf%lf",&a[i],&b[i]);
    sort(a+1,a+1+n,cmp);sort(b+1,b+1+n,cmp);
    for(int i=2;i<=n;i++)b[i]+=b[i-1];
    double suma=0;
    for(int i=1;i<=n;i++){
        suma+=a[i];
        ans=max(ans,find(i,suma));
    }
    printf("%.4lf",ans);
    return 0;
}

你可能感兴趣的:(CEOI,三分)