手写nth_element模板(随机化版快排)
Posted three-d
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了手写nth_element模板(随机化版快排)相关的知识,希望对你有一定的参考价值。
#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模板(随机化版快排)的主要内容,如果未能解决你的问题,请参考以下文章