Gym 101064 D Black Hills golden jewels (二分)

时间:2022-04-05 16:28:16

题目链接:http://codeforces.com/gym/101064/problem/D

问你两个数组合相加的第k大数是多少。

先sort数组,二分答案,然后判断其正确性(判断过程是枚举每个数然后二分)。

 //#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
const int N = 1e5 + ;
LL a[N], k, n; bool judge(LL val) {
LL cnt = ;
for(int i = ; i <= n; ++i) {
LL pos = (LL)(upper_bound(a + , a + n + , val - a[i]) - a);
if(pos > (LL)i)
cnt += n - pos + ;
else
cnt += n - pos;
}
cnt /= ;
return (n - )*n/ - k >= cnt;
} int main()
{
while(~scanf("%lld %lld", &n, &k)) {
for(int i = ; i <= n; ++i) {
scanf("%lld", a + i);
}
sort(a + , a + n + );
LL l = a[] + a[], r = a[n] + a[n - ];
while(l < r) {
LL mid = (l + r) / ;
if(judge(mid)) {
r = mid;
} else {
l = mid + ;
}
}
printf("%lld\n", l);
}
return ;
}