D. Black Hills golden jewels

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

In Rapid City are located the main producers of the Black Hills gold jewelry, a very popular product among tourists. The main characteristics of these jewels are the designs of leaves and grape clusters that are made with gold alloys in yellow, green and pink tones.

Planning a trip to Rapid City in 2017, Nay Suames decides that he wants to buy these famous jewels, but he doesn't have much money to spend. Searching for the lowest prices, he found a jewelry store that will have a sale in May, the same month he will travel to that city. Lucky guy!

The jewelry store intends to sell the jewels that were produced with small defects, in general, only perceivable by experts. The jewels are separated in N categories of defects. Even if the defect is imperceptible, the jewelry store can't sell the jewel at full price, so it gives a discount proportional to the defect.

Knowing the high demand for these jewels, the jewelry store created a system aiming to be fair and, at the same time, to prevent everybody from buying only the cheapest jewels. The clients have to make a queue and will be served one at a time. When their time comes, the client must choose two jewels from different categories. Furthermore, they can't choose the same combination of categories that was previously chosen by another client.

Nay asks for your help to find the minimum amount of money he needs, being the K-th client in the queue, so that it is guaranteed that he can buy two different jewels.

Input

The first line contains two integers, N and K, representing the number of categories and the position of Nay in the queue, respectively. The next line contains N integers a1, a2, ..., aN (0 ≤ ai ≤ 109), corresponding to the prices of each category.

Output

Print a single integer, the minimum amount of money to guarantee that Nay can buy the jewels.

Examples
input
4 3
2 4 5 2
output
6
input
6 9
1 2 3 4 5 6
output
7
【题意】:内容介绍了很多,其实跟题意没什么关系。关键的题意可以浓缩成,给定一个序列,从序列中任意取两个数形成一个和,两个数不可相同,要求求出第k小的组合。
【分析】:数据量是10的五次方,任取两个记录下来的话是10的10次方级的,再拍个序的话,肯定既超控件又超时间。但10的五次方级别的是可以拍个序的,排完序后就可以确定拿取组合的上限即最大和次大元素的组合,最小组合即最小和次小元素的组合。确定了上限和下限,又因为所求的值是有单调性的,所以考虑用二分。
    在确定使用二分之后,此时需要验证一个值是否满足条件。即任意组合中不大于该组合的组合数是否不小于k。若不小于k的话,则不断向左侧区间逼近,求出最小的符合的值。在求取组合数目时,如果使用普通的二重循环的话,肯定又超时。此时从最小的元素开始,设x为验证元素,使用upper_bound函数求出第一个大于x-a[i]的元素的位置,其函数返回的值即是和该元素相加不大于x的个数,即是组合数。upper_bound函数本质上也是一个二分。在此基础上可以进行一定的优化,在循环过程中只要所记数值大于等于k就可结束循环。知道大于x的a[i]标记,缩小循环范围也可优化。 【代码】:
#include <bits/stdc++.h>
using namespace std; //#pragma comment(linker, "/STACK:1024000000,1024000000") #define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w",stdout) typedef __int64 LL; const int MAXN = 1e5 + ;
const LL INF = 0x3f3f3f3f3f3f3f3f;
LL A[MAXN];
LL N, K; bool check (LL m) {
LL ks = ;
for (int i = N - ; i >= ; i --) {
ks += lower_bound (A, A + i, m - A[i]) - A;
}
if (ks >= K) return true;
return false;
} int main() {
#ifndef ONLINE_JUDGE
FIN;
//FOUT;
#endif
while (~scanf ("%I64d %I64d", &N, &K) ) {
for (int i = ; i < N; i ++) {
scanf ("%I64d", &A[i]);
}
sort (A, A + N);
LL lb = -, ub = INF, mid;
while (lb <= ub) {
mid = (ub + lb) >> ;
if (check (mid) ) {
ub = mid - ;
} else {
lb = mid + ;
}
}
printf ("%I64d\n", ub); }
return ;
}

双重二分

Gym 101064 D Black Hills golden jewels 【二分套二分/给定一个序列,从序列中任意取两个数形成一个和,两个数不可相同,要求求出第k小的组合】的更多相关文章

  1. Gym 101064 D Black Hills golden jewels (二分)

    题目链接:http://codeforces.com/gym/101064/problem/D 问你两个数组合相加的第k大数是多少. 先sort数组,二分答案,然后判断其正确性(判断过程是枚举每个数然 ...

  2. Java实现 LeetCode 719 找出第 k 小的距离对(二分搜索法+二分猜数字)

    719. 找出第 k 小的距离对 给定一个整数数组,返回所有数对之间的第 k 个最小距离.一对 (A, B) 的距离被定义为 A 和 B 之间的绝对差值. 示例 1: 输入: nums = [1,3, ...

  3. poj 3579 Median 二分套二分 或 二分加尺取

    Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5118   Accepted: 1641 Descriptio ...

  4. poj 3685 Matrix 二分套二分 经典题型

    Matrix Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 5724   Accepted: 1606 Descriptio ...

  5. 719. 找出第 K 小的数对距离

    719. 找出第 K 小的数对距离 这道题其实有那么一点二分猜答案的意思,也有很多类似题目,只不过这道题确实表达的不是很清晰不容易想到,题没问题,我的问题.既然是猜答案,那么二分边界自然就是距离最大值 ...

  6. poj3579 二分套二分

    和poj3685类似,都是二分答案然后在判断时再二分 这题的内层二分可以用stl代替 /* 二分套二分,思路:升序排序数据,先二分答案x进行判断,判断时枚举每个元素,二分找到和其之差小于等于x的所有值 ...

  7. POJ-3579 Median---二分第k大(二分套二分)

    题目链接: https://cn.vjudge.net/problem/POJ-3579 题目大意: 求的是一列数所有相互之间差值的序列的最中间的值是多少. 解题思路: 可以用二分套二分的方法求解第m ...

  8. 输入两个整数n 和m,从数列1,2,3.......n 中任意取几个数, 使其和等于m ,要求将当中全部的可能组合列出来

    中兴面试题之中的一个.难度系数中. 题目描写叙述例如以下:输入两个整数n 和m,从数列1,2.3.......n 中任意取几个数, 使其和等于m ,要求将当中全部的可能组合列出来. 逻辑分析: 1.比 ...

  9. python实现给定K个字符数组,从这k个字符数组中任意取一个字符串,按顺序拼接,列出所有可能的字符串组合结果!

    题目描述:给定K个字符数组,从这k个字符数组中任意取一个字符串,按顺序拼接,列出所有可能的字符串组合结果! 样例: input:[["a","b"," ...

随机推荐

  1. js:随记

    typeof:没有大写,因为typeof是运算符 *1:是转数字 +string:是转数字,在Date对象上是getTime ""+:是转字符串 "":bool ...

  2. Mysql密码加密方式

    以Mysql 4.1版本为分界线,两种加密方式 Mysql323加密:(16位) select  old_password('root'); //Mysql自带加密函数old_password(str ...

  3. WPF异步回调时回调函数如何获取异步函数产生的变量

    有这么一个问题,WPF在使用异步回调的时候,回调函数需要用到异步函数里产生的一个变量,例如异步函数里查询数据库得到了一个DataTable,如何传递给回调函数呢? [方案一]使用全局变量 很容易想到的 ...

  4. Centos7和Centos6防火墙开放端口配置方法(避坑教学)

    ▲这篇文章主要为大家详细介绍了Centos7防火墙开放端口的快速方法,感兴趣的小伙伴们可以参考一下! 一.CentOS 7快速开放端口: CentOS升级到7之后,发现无法使用iptables控制Li ...

  5. Neural Network

    逻辑回归用神经网络节点的方式表示 前面已经介绍过逻辑回归的模型,样本为(x,y) 其中y的值为1或0,假设x有2个特征,则对应关系如下图所示.  实际情况是需要求需要三个参数,因此输入层需要添加一个 ...

  6. day39---mysql基础三

    1.索引: 字典得目录,便于数据查找. 原理:将列信息存储在其相关的文件,这些信息使用便于检索的方式如B-tree.哈希来存储 索引的分类: 普通所有:name,只能帮助查找 唯一索引:name,帮助 ...

  7. php和js中数组的总结

      php中数组的表示方法:array()或者[] js中数组的表示方法:new array()或者[] 一.php中初始化命名数组 在PHP中声明数组的方式主要有两种:一是应用array()函数声明 ...

  8. mac攻略(九) -- ssh工具secureCRT

    mac ssh 客户端 : 本身mac直接使用终端来ssh连接就很方便,但是使用过程中随着远程服务器的增多和zsh和远程服务器编码不同产生了乱码,决定安装一款ssh终端软件,以下方法亲测可用,感谢提供 ...

  9. 启动子Activity

    启动普通子Activity: 一个activity启动另一个activity最简单的方式是使用 startActivity(Intent) 方法: public void startActivity( ...

  10. 最简单的RSA及其几个网站和工具

    最简单的形式 给你公钥和一个密文. flag.enc就是密文,我们用记事本是看不出什么的,其实也不用看,因为后边的解密是直接用脚本读取文件的,只需要知道这是密文. pub.pem就是公钥,用记事本打开 ...