码迷,mamicode.com
首页 > 其他好文 > 详细

手写nth_element模板(随机化版快排)

时间:2019-10-02 17:07:24      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:pen   back   etc   register   getc   bsp   style   line   algo   

#include<ctime>
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
const int maxn=1e6+5;
ll s,gd[maxn];
int n,m,b[maxn],k[maxn],ans;
inline ll read()
{
    ll t=0,f=1; char ch=getchar();
    while(ch<0||ch>9){ if(ch==-) f=-1; ch=getchar();}
    while(ch>=0&&ch<=9)  t=(t<<3)+(t<<1)+(ch^48),ch=getchar();
    return f*t;
}
inline bool cmp(ll a,ll b){ return a>b; }
void sort(int l,int r,int k){
    if(l>=r) return;
    int x=rand()%(r-l+1)+l;
    ll tmp=gd[x];   int nl=l,nr=r;
    swap(gd[x],gd[l]);
    while(nl^nr){
        while(gd[nr]<=tmp&&nr>nl)  --nr;
        while(gd[nl]>=tmp&&nl<nr)  ++nl;
        swap(gd[nl],gd[nr]);
    }
    swap(gd[nl],gd[l]);
    if(nl-l+1>k)  sort(l,nl-1,k);
    if(k-(nl-l+1)>0)  sort(nl+1,r,k-(nl-l+1));
}
inline bool check(int ans){
    for(register int i=1;i<=n;++i)   gd[i]=1ll*k[i]*ans+b[i];
    sort(1,n,m);
    ll sum=0;
    for(register int i=1;i<=m;++i){
        if(gd[i]<=0)  continue;  
        if(s-sum<=gd[i])   return 1;  
        sum+=gd[i];
    }
    return sum>=s;
}
int main()
{
    //freopen("merchant3.in","r",stdin);
    srand(time(0));
    n=read(),m=read(),s=read();
    for(register int i=1;i<=n;++i)  k[i]=read(),b[i]=read();
    int l=1,r=1000000000,mid,sum=0;
    if(check(0)){ puts("0"); return 0;}
    while(l<=r){
        int mid=(l+r)>>1;
        if(check(mid))  ans=mid,r=mid-1;
        else l=mid+1;
    }
    cout<<ans;
}

 

 

手写nth_element模板(随机化版快排)

标签:pen   back   etc   register   getc   bsp   style   line   algo   

原文地址:https://www.cnblogs.com/three-D/p/11617590.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!