标签:gray href sort def algorithm col bsp std display
第1行:2个数N和K,中间用空格分隔。N为数组的长度,K对应第K大的数。(2 <= N <= 50000,1 <= K <= 10^9) 第2 - N + 1行:每行2个数,分别是A[i]和B[i]。(1 <= A[i],B[i] <= 10^9)
输出第K大的数。
3 2 1 2 2 3 3 4
9
-----
#include<cstdio> #include<cstring> #include<algorithm> using std::sort; const int N=50010; #define ll long long ll a[N],b[N]; int n,k; inline ll check(ll x) { int sum=0; for(int i=1;i<=n;i++) { // printf("!!!\n"); int l=1,r=n; ll ans,mid; while(l<r) { mid=(l+r)>>1; // printf("std:: 2 %d %d %lld\n",l,r,mid); ans=a[i]*b[mid]; if(ans>x) r=mid; else l=mid+1; } ans=a[i]*b[l]; if(ans>x) sum+=(n-l+1); else { ans=a[i]*b[r]; if(ans>x) sum+=(n-r+1); } } return sum; } int main() { scanf("%d %d",&n,&k); for(int i=1;i<=n;i++) scanf("%lld %lld",&a[i],&b[i]); sort(a+1,a+1+n),sort(b+1,b+1+n); ll l=a[1]*b[1],r=a[n]*b[n]; ll ans=0,mid;0 while(r>l) { mid=(l+r)>>1; // printf("std 1:: %lld****\n",mid); ans=check(mid); if(ans>k-1) l=mid+1; else r=mid; } if(check(l)>k-1) printf("%lld\n",l); else printf("%lld\n",r); return 0; }
=====================
标签:gray href sort def algorithm col bsp std display
原文地址:http://www.cnblogs.com/12fs/p/7745337.html