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"," ...
随机推荐
- 14,UA池和代理池
今日概要 scrapy下载中间件 UA池 代理池 一,下载中间件(Downloader Middlewares) 位于scrapy引擎和下载器之间的一层组件. - 作用: (1)引擎将请求传递给下载器 ...
- border-color与color
1.border-color就是color,即border-color的默认颜色就是color 当没有指定border-color的时候,会使用color作为边框的颜色! 类似的还有text-shad ...
- Careercup - Microsoft面试题 - 23123665
2014-05-12 07:44 题目链接 原题: Given an array having unique integers, each lying within the range <x&l ...
- xml文件的生成
关于android中自定义xml文件的生成,请看示例代码(主要来源于黑马教程): import java.io.File; import java.io.FileNotFoundException; ...
- IOS开发学习笔记023-UIToolBar的使用
这里使用代码实现 大概过程: 1.创建工具条 2.创建插入条 3.添加头像.标签.删除按钮 4.点击头像获取标签信息 做一个简单的联系人列表,可以添加删除联系人,现在还没有添加头像和文字,接下来慢慢添 ...
- SSH的基本操作
一. 登陆SSH远程控制服务器 我们以192.168.100.42服务器为例. SSH乱码: LANG="zh_CN.GB18030" export LANG=zh_CN.gb23 ...
- col-md-push-*和col-md-offset的区别
实现方式的区别:col-md-offset-*,是利用margin-left实现的,col-md-push-*/col-md-pull-*是利用相对定位实现的. 效果的区别,col-md-offset ...
- monolog使用
安装composer curl -sS https://getcomposer.org/installer | phpmv composer.phar /usr/local/bin/composer ...
- Mysql实战之主从复制的读写分离
author:JevonWei 版权声明:原创作品 ProxySQL构建主从复制的读写分离 ProxySQL官网及下载地址 http://www.proxysql.com/ 架构角色 mysql-sl ...
- 【Luogu】P2173网络(LCT)
题目链接 这次坑我的是与或的结合顺序…… 开十个LCT记录一下即可.以上. #include<cstdio> #include<cstring> #include<cst ...