AtCoder Beginner Contest 155 D.Pairs
Problem Statement
We have integers .There are ways to choose two of them and form a pair. If we compute the product of each of those pairs and sort the results in ascending order, what will be the -th number in that list?
Constraints
All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1
4 3
3 3 -4 -2
Sample Output 1
-6
Sample Input 2
10 40
5 4 3 2 -1 0 0 0 0 0
Sample Output 2
6
Sample Input 3
30 413
-170202098 -268409015 537203564 983211703 21608710 -443999067 -937727165 -97596546 -372334013 398994917 -972141167 798607104 -949068442 -959948616 37909651 0 886627544 -20098238 0 -948955241 0 -214720580 277222296 -18897162 834475626 0 -425610555 110117526 663621752 0
Sample Output 3
448283280358331064
暴力肯定是不行的,我想到了二分,可惜不会……看了别人的代码才明白怎么二分,我现在发现atcoder很喜欢范围二分,即从所给范围中二分得到最佳答案,我们求两个数的乘积,范围为 ,我们在这个范围内二分,
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+5;
int n;
ll k,a[N],L,R;
int main(){
scanf("%d%lld",&n,&k);
for(int i=0;i<n;i++) scanf("%lld",&a[i]);
sort(a,a+n);
L=-1e18,R=1e18;
while(L+1!=R){
ll Mid=(L+R)/2;
ll cnt=0;
for(int i=0;i<n;i++)
{
if(a[i]<0){//小于0则向下找
int l=i,r=n;
while(l+1!=r){
int mid=(l+r)/2;
if(a[i]*a[mid]<=Mid) r=mid;
else l=mid;
}
cnt+=n-r;
}
else{
int l=i,r=n;
while(l+1!=r){//大于0向上找
int mid=(l+r)/2;
if(a[i]*a[mid]<=Mid) l=mid;
else r=mid;
}
cnt+=l-i;
}
}
if(cnt<k) L=Mid;
else R=Mid;
}
printf("%lld",R);
return 0;
}