【题解】
二分答案,check的时候枚举ai再二分b,求出有多少个数比mid大(或小)。其实check的时候用two pointer也可以,因为mid是单调的,a、b也是单调的。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<vector> 5 #define LL long long 6 #define rg register 7 #define N 100010 8 using namespace std; 9 LL n,k,l,r,a[N],b[N]; 10 inline int read(){ 11 int k=0,f=1; char c=getchar(); 12 while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar(); 13 while('0'<=c&&c<='9')k=k*10+c-'0',c=getchar(); 14 return k*f; 15 } 16 inline LL check(LL x){ 17 LL sum=0; 18 for(rg int i=1;i<=n;i++){ 19 LL tmp=x/a[i]+1; 20 sum+=lower_bound(b+1,b+1+n,tmp)-b-1; 21 // printf("sum=%d ",sum); 22 } 23 // printf("sum=%d ",sum); 24 return sum; 25 } 26 int main(){ 27 n=read(); k=read(); k=n*n-k+1; 28 // printf("%d ",k); 29 for(rg int i=1;i<=n;i++) a[i]=read(),b[i]=read(); 30 sort(a+1,a+1+n); sort(b+1,b+1+n); 31 l=a[1]*b[1]-1; r=a[n]*b[n]; 32 while(l+1<r){ 33 LL mid=(l+r)>>1; 34 // printf("%lld %lld ",l,r); 35 if(check(mid)>=k) r=mid; else l=mid; 36 } 37 printf("%lld ",r); 38 return 0; 39 }