Gym 101064 D Black Hills golden jewels (二分)

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

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

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

 1 //#pragma comment(linker, "/STACK:102400000, 102400000")
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <cstdio>
 7 #include <vector>
 8 #include <cmath>
 9 #include <ctime>
10 #include <list>
11 #include <set>
12 #include <map>
13 using namespace std;
14 typedef long long LL;
15 typedef pair <int, int> P;
16 const int N = 1e5 + 5;
17 LL a[N], k, n;
18
19 bool judge(LL val) {
20     LL cnt = 0;
21     for(int i = 1; i <= n; ++i) {
22         LL pos = (LL)(upper_bound(a + 1, a + n + 1, val - a[i]) - a);
23         if(pos > (LL)i)
24             cnt += n - pos + 1;
25         else
26             cnt += n - pos;
27     }
28     cnt /= 2;
29     return (n - 1)*n/2 - k >= cnt;
30 }
31
32 int main()
33 {
34     while(~scanf("%lld %lld", &n, &k)) {
35         for(int i = 1; i <= n; ++i) {
36             scanf("%lld", a + i);
37         }
38         sort(a + 1, a + n + 1);
39         LL l = a[1] + a[2], r = a[n] + a[n - 1];
40         while(l < r) {
41             LL mid = (l + r) / 2;
42             if(judge(mid)) {
43                 r = mid;
44             } else {
45                 l = mid + 1;
46             }
47         }
48         printf("%lld\n", l);
49     }
50     return 0;
51 }
时间: 2024-12-23 06:00:24

Gym 101064 D Black Hills golden jewels (二分)的相关文章

D. Black Hills golden jewels 二分答案 + 二分判定

http://codeforces.com/gym/101064/problem/D 题目是给定一个数组,如果两两组合,有C(n, 2)种结果,(找出第一个大于等于第k大的结果) 思路, 二分答案val,判断如下. 先把数组排序. 然后暴力枚举每个数a[i],那么找出第一个大于val - a[i]的,中间的数字和它组合.都是<=val的 统计这些数字的个数,如果大于等于k就是可行解. hack. 二分答案的时候,明显下界是a[1] + a[2].上界是a[n] + a[n - 1] 如果用beg

gym 101064 G.The Declaration of Independence (主席树)

题目链接: 题意: n个操作,有两种操作: E p  c    在序号为p的队列尾部插入c得到新的队列,序号为i D p   查询并删除序号为p的队列顶部的元素,得到序号为i的新队列 思路: 需要查询历史版本,我们可以用将这些操作都更新在主席树上,这两个操作可以等价为更新一个点,查询一个点,尾部和顶部的元素我们可以分别用l[i],r[i]来维护第i个队列的顶部元素和尾部元素,查询的时候直接在主席树上找就好了. 实现代码: #include<iostream> #include<cstdi

Codeforces gym 100517(二分,同方向判断)

题意:给了一个凸包,按顺时针顺序给点,点数不超过10万,再给了两个不同点,点严格在凸包内,凸包保证没有三点共线,问凸包上有多少对点(pi, pj),满足pi和pj的线段 与 两个点的线段严格相交,线段间严格相交意思是交点不在端点. 链接:http://codeforces.com/gym/100517 (K题) 解法:设凸包有n个点,将凸包点集p扩大一倍,变为2n个点.枚举前n个点,每次枚举到 i ,在[i+1, i+n-1]内进行二分,找到两个点p1,p2,满足p1和p2是"最靠近"

Codeforces Gym 100500F Problem F. Door Lock 二分

Problem F. Door LockTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/attachments Description It was a beautiful night at Yekaterinburg, the sky was crystal clear with no clouds, and the view of the moon and the stars was

Gym 100886J Sockets 二分答案 + 贪心

Description standard input/outputStatements Valera has only one electrical socket in his flat. He also has m devices which require electricity to work. He's got n plug multipliers to plug the devices, the i-th plug multiplier has ai sockets. A device

Dales and Hills Gym - 101411D

Problem D. Dales and HillsInput le: dales.inOutput le: dales.outTime limit: 2 secondsMemory limit: 256 megabytesLet's consider a number sequence a1; · · · ; aN . We call the continuous subsequence ai; · · · ; aj ; · · · ; ak(1 ≤ i < j < k ≤ N) of th

CodeForces Gym - 100989G(二分)

题目链接:https://vjudge.net/contest/236677#problem/G 题目意思:有m个地方,有k小时,每个地方有a[i]个人.你要进行操作(一个小时可以移动一个人到另一个地方,或者不移动),最后使得m个地方的人数最多值变成最小情况.(就是移动人使每个地方的人接近平均值,就是统计学中方差就小) 思路:求出平均值,因为平均值是不变的,答案一定在平均值和最大值之间,二分就是要找到这个区间,左边临界值就是平均值,右边临界值就是最大值.在与当前最大值maxx二分得平局值mid,

[Gym - 100517K] Kingdom Division 2 二分

大致题意: 给出一个凸包,以及凸包内的两个点p1,p2,求有多少条经过凸包顶点的直线能够将凸包分割为两部分,且给出的两点分别属于不同的部分 枚举凸包的顶点,二分求出p1,p2线段左边的最大坐标L以及右边的最小坐标R,则答案为R-L-1的累加和除以2 注意文件输入,输出 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<

Gym - 101908G Gasoline 二分+最大流

After the end of the truck drivers' strike, you and the rest of Nlogônia logistics specialists now have the task of planning the refueling of the gas stations in the city. For this, we collected information on stocks of RR refineries and about the de