AtCoder Beginner Contest 155 D.Pairs

AtCoder Beginner Contest 155 D.Pairs

Problem Statement

We have N N integers A 1 , A 2 , A 3 , . . . , A n A_1,A_2,A_3,...,A_n .There are N ( N 1 ) 2 \frac{N*(N-1)}2 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 K K -th number in that list?

Constraints

All values in input are integers.
2 N 2 × 105 2≤N≤2×105
1 K N ( N 1 ) 2 1≤K≤\frac{N*(N-1)}2
1 0 9 A i 1 0 9 ( 1 i N ) −10^9≤A_i≤10^9(1≤i≤N)

Input

Input is given from Standard Input in the following format:

N   K N K
A 1   A 2   A N A_1 A_2… A_N

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很喜欢范围二分,即从所给范围中二分得到最佳答案,我们求两个数的乘积,范围为 [ 1 0 18 , 1 0 18 ] [-10^{18},10^{18}] ,我们在这个范围内二分,

#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;
}
发布了282 篇原创文章 · 获赞 14 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_43765333/article/details/104352793