HDU - 6231:K-th Number (不错的二分)
Now Alice want to build an array BB by a parameter KK as following rules:
Initially, the array B is empty. Consider each interval in array A. If the length of this interval is less than KK, then ignore this interval. Otherwise, find the KK-th largest number in this interval and add this number into array BB.
In fact Alice doesn't care each element in the array B. She only wants to know the MM-th largest element in the array BB. Please help her to find this number.
InputThe first line is the number of test cases.
For each test case, the first line contains three positive numbers N(1≤N≤105),K(1≤K≤N),MN(1≤N≤105),K(1≤K≤N),M. The second line contains NN numbers Ai(1≤Ai≤109)Ai(1≤Ai≤109).
It's guaranteed that M is not greater than the length of the array B.
OutputFor each test case, output a single line containing the MM-th largest element in the array BB.Sample Input
2
5 3 2
2 3 1 5 4
3 3 1
5 8 2
Sample Output
3
2
题意:把所有区间从小到大的第K大取出来再排序,求第M大。
思路:把所有数拿进去二分,大于等于M的数则check成功,R=Mid+1; 尺取。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
long long n,k,m,a[],b[],T,ans;
inline bool pd(long long x){
int cnt=;long long tot=;
for(register int i=,j=;i<=n;i++){
while(cnt<k&&j<=n)cnt+=a[j++]>=x;
if(cnt<k)break;
tot+=n-j+;
cnt-=a[i]>=x;
}
return tot>=m;
}
int main(){
for(scanf("%lld",&T);T--;){
scanf("%lld%lld%lld",&n,&k,&m);ans=;
for(register int i=;i<=n;i++)scanf("%lld",a+i),b[i]=a[i];
sort(b+,b++n);
long long l=,r=n,mid;
while(l<=r){
mid=(l+r)>>;
if(pd(b[mid]))ans=mid,l=mid+;
else r=mid-;
}
printf("%lld\n",b[ans]);
}
return ;
}
HDU - 6231:K-th Number (不错的二分)的更多相关文章
- HDU 6093 - Rikka with Number | 2017 Multi-University Training Contest 5
JAVA+大数搞了一遍- - 不是很麻烦- - /* HDU 6093 - Rikka with Number [ 进制转换,康托展开,大数 ] | 2017 Multi-University Tra ...
- POJ - 3111 K Best 0-1分数规划 二分
K Best Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 12812 Accepted: 3290 Case Time ...
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- HDU 2254 奥运(矩阵高速幂+二分等比序列求和)
HDU 2254 奥运(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 2254 奥运 题意: 中问题不解释. 分析: 依据floyd的算法,矩阵的k次方表示这个矩阵走了k步. 所以k ...
- 【BZOJ3110】K大数查询(整体二分)
[BZOJ3110]K大数查询(整体二分) 题面 BZOJ 题解 看了很久整体二分 一直不知道哪里写错了 ... 又把树状数组当成线段树区间加法来用了.. 整体二分还是要想清楚在干什么: 我们考虑第\ ...
- BZOJ_3110_[Zjoi2013]K大数查询_整体二分+树状数组
BZOJ_3110_[Zjoi2013]K大数查询_整体二分+树状数组 Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位 ...
- hdu 6216 A Cubic number and A Cubic Number【数学题】
hdu 6216 A Cubic number and A Cubic Number[数学] 题意:判断一个素数是否是两个立方数之差,就是验差分.. 题解:只有相邻两立方数之差才可能,,因为x^3-y ...
- HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)
HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意: 给一个序列由 ...
- hdu 6231 -- K-th Number(二分+尺取)
题目链接 Problem Description Alice are given an array A[1..N] with N numbers. Now Alice want to build an ...
随机推荐
- 【Java】仿真qq尝试:用户注册(一)
需求: 1.流程分析:客户端程序拿到用户名和密码,将用户名和密码发送到服务端(在客户端验证合法性),服务端接收并存储用户名和密码,返回给客户端一个信息(可能是成功也可能是失败.) 2.数据怎么存?服务 ...
- 微信小程序:其中wxml和wxss的样式说明
微信小程序:其中wxml和wxss的样式说明 一.简介 对于css不熟悉的Android程序员来说,开发微信小程序面临的一个比较困难的问题就是界面的排版了.微信小程序的排版就跟wxml和wxss有关了 ...
- vue脚手架解决跨域问题-------配置反向代理
1.打开config/index.js 2.在dev配置对象中找到proxyTable:{} 3.添加如下配置 // 配置反向代理,解决跨域请求 proxyTable: { '/api': { tar ...
- shell编程学习笔记之标准输入输出(read&echo)
2017-07-17 09:32:07 输入read: 用途: 从标准输入读取一行,或者从文件描述符FD(file descriptor)中读取一行,并且将其分割成字段. 用法: read [-ers ...
- TF卡.之前的(20180923)
1.京东上搜到的(购买记录) ZC:64G的应该是 只买了一个 另一个取消了 2.TB上搜到的 购买记录是这样的(注意 也有 取消的) 3. 4. 5.
- JSON01_资料
1. 资料网址: http://blog.csdn.net/vincent_czz/article/details/7333977 http://blog.csdn.net/huangwuyi/art ...
- srm开发(基于ssh)(4)
1 input处理内容补充 -在struts2里面有错误处理机制,当上传文件超过默认的大小,自动返回结果input -在struts.xml中配置input的返回结果 <!-- 配置input结 ...
- int(3)与int(11)的区别
注意:这里的M代表的并不是存储在数据库中的具体的长度,以前总是会误以为int(3)只能存储3个长度的数字,int(11)就会存储11个长度的数字,这是大错特错的.其实当我们在选择使用int的类型的时候 ...
- APP耗电量专项测试整理
Android: (使用batterystats) 方法: 手机自带的电量监控.GT 命令(5.0以上系统才可以): 1.下载historian.py脚本,下载地址:https://github.co ...
- Project Euler 126 - Cuboid layers
这题先是推公式… 狂用不完全归纳+二次回归,最后推出这么一个奇怪的公式 \[f(t,x,y,z)=4(t-1)(x+y+z+t-2)+2(xy+yz+xz)\] 表示长宽高为\(x\).\(y\).\ ...