Gym 101064 D Black Hills golden jewels 【二分套二分/给定一个序列,从序列中任意取两个数形成一个和,两个数不可相同,要求求出第k小的组合】
D. Black Hills golden jewels
2 seconds
256 megabytes
standard input
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.
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.
Print a single integer, the minimum amount of money to guarantee that Nay can buy the jewels.
- 4 3
- 2 4 5 2
- 6
- 6 9
- 1 2 3 4 5 6
- 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小的组合】的更多相关文章
- Gym 101064 D Black Hills golden jewels (二分)
题目链接:http://codeforces.com/gym/101064/problem/D 问你两个数组合相加的第k大数是多少. 先sort数组,二分答案,然后判断其正确性(判断过程是枚举每个数然 ...
- Java实现 LeetCode 719 找出第 k 小的距离对(二分搜索法+二分猜数字)
719. 找出第 k 小的距离对 给定一个整数数组,返回所有数对之间的第 k 个最小距离.一对 (A, B) 的距离被定义为 A 和 B 之间的绝对差值. 示例 1: 输入: nums = [1,3, ...
- poj 3579 Median 二分套二分 或 二分加尺取
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5118 Accepted: 1641 Descriptio ...
- poj 3685 Matrix 二分套二分 经典题型
Matrix Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 5724 Accepted: 1606 Descriptio ...
- 719. 找出第 K 小的数对距离
719. 找出第 K 小的数对距离 这道题其实有那么一点二分猜答案的意思,也有很多类似题目,只不过这道题确实表达的不是很清晰不容易想到,题没问题,我的问题.既然是猜答案,那么二分边界自然就是距离最大值 ...
- poj3579 二分套二分
和poj3685类似,都是二分答案然后在判断时再二分 这题的内层二分可以用stl代替 /* 二分套二分,思路:升序排序数据,先二分答案x进行判断,判断时枚举每个元素,二分找到和其之差小于等于x的所有值 ...
- POJ-3579 Median---二分第k大(二分套二分)
题目链接: https://cn.vjudge.net/problem/POJ-3579 题目大意: 求的是一列数所有相互之间差值的序列的最中间的值是多少. 解题思路: 可以用二分套二分的方法求解第m ...
- 输入两个整数n 和m,从数列1,2,3.......n 中任意取几个数, 使其和等于m ,要求将当中全部的可能组合列出来
中兴面试题之中的一个.难度系数中. 题目描写叙述例如以下:输入两个整数n 和m,从数列1,2.3.......n 中任意取几个数, 使其和等于m ,要求将当中全部的可能组合列出来. 逻辑分析: 1.比 ...
- python实现给定K个字符数组,从这k个字符数组中任意取一个字符串,按顺序拼接,列出所有可能的字符串组合结果!
题目描述:给定K个字符数组,从这k个字符数组中任意取一个字符串,按顺序拼接,列出所有可能的字符串组合结果! 样例: input:[["a","b"," ...
随机推荐
- DFS:HDU1518-Square(剪枝较多的DFS)
题目: Square Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- PowerShell批量配置VM端点
我们可以通过PowerShell脚本批量添加VM端点.请您参考以下方案. 准备工作 – PowerShell连接China Azure 1. 从官网下载页面,下载并安装Windows Azure Po ...
- 快速排序算法(C)
sort快排函数的基本版,效率n*logn,快排的完全版就是在递归之中夹杂对序列的预判断,最优的选择排序方法,快速排序算法只是其中之一. 简单的说明一下快速排序的思想,对于一个数列,首先选择一个基数( ...
- Android 本地css引用
/** 全局web样式 * 以前看不懂,现在仔细,耐心的看看,全懂了,认真的看一遍都懂了 * * * */ // 链接样式文件,代码块高亮的处理 public final static String ...
- 54、edittext输入类型限制为ip,inputType应该如何设置
<EditText android:id="@+id/et_setting_printer_edit_info_ip" android:layout_width=" ...
- SEO搜索引擎优化基础
要如何提高自己网站的知名度,那必须了解一些SEO知识. 1.什么是搜索引擎 所谓的搜索引擎(Search Engines)是一些能够主动搜索信息(搜索网页上的单词和简短的特定的内容描述)并将其自动索 ...
- 南邮部分wp
MYSQL 打开robots.txt 鍒お寮€蹇冿紝flag涓嶅湪杩欙紝杩欎釜鏂囦欢鐨勭敤閫斾綘鐪嬪畬浜嗭紵 鍦–TF姣旇禌涓紝杩欎釜鏂囦欢寰€寰€瀛樻斁鐫€鎻愮ず淇℃伅 这一看乱码,放到新建tx ...
- WordCount by Java
WordCount by Java 软测第二周作业 该项目github地址如下: https://github.com/YuQiao0303/WordCount 一.概述 项目WordCount的需求 ...
- Shell脚本直接执行sql语句和不显示列名
在shell脚本编程的时候,可以通过在mysql连接命令添加-N和-e参数实现查询结果不显示列名和直接执行sql语句操作 demo $(mysql -h ${HOST} -u ${USER} -p${ ...
- 处理python字符串中的中文字符
# -*- coding:utf-8 -*- import sys,os txta = open('a.txt','r') str = '' for line in txta: str += line ...