HDU - 6231 K-th Number (2017CCPC哈尔滨站 二分+尺取法)
Alice are given an array A[1..N] with N numbers.
Now Alice want to build an array B by a parameter K as following rules:
Initially, the array B is empty. Consider each interval in array A. If the length of this interval is less than K, then ignore this interval. Otherwise, find the K-th largest number in this interval and add this number into array B.
In fact Alice doesn't care each element in the array B. She only wants to know the M-th largest element in the array B
. Please help her to find this number.
Input
The 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),M. The second line contains N numbers Ai(1≤Ai≤109)
.
It's guaranteed that M is not greater than the length of the array B.
Output
For each test case, output a single line containing the M-th largest element in the array B
.
Sample Input
2
5 3 2
2 3 1 5 4
3 3 1
5 8 2
Sample Output
3
2
题意:给你个数组A,将每个区间的第k大的数字加入数组B,不存在则跳过,求B中第m大的数
题解:二分答案,尺取法计当大于mid的第k大个数的个数,如果大于m,则说明答案在mid与right之间,否则在left与mid之间。(二分左闭右开)
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
#define maxn 100010
#define CLR(a,b) memset(a,b,sizeof(a))
long long n,k,m;
int a[maxn]; bool check(int mid)
{
int cnt = ;
int l = ;
int r = ;
long long sum = ;
while(r<=n){
if(cnt < k){
r++;
if(a[r] >= mid) cnt++;
}
if(cnt >= k){
sum+=(n-r+);
if(a[l] >= mid) cnt--;
l++;
}
}
return (sum>=m);
} int main()
{
int t;
cin>>t;
while(t--){
cin>>n>>k>>m;
int l = ;
int r = ;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
r = max(a[i]+,r);
}
while(l+<r){
int mid = (r+l)/;
if(check(mid))
l = mid;
else
r = mid;
}
cout<<l<<endl;
}
return ;
} /* 1
8 3 4
2 3 1 5 3 4 4 5 */
最新文章
- HTTP服务器(2)
- ASP.NET MVC之Session State性能问题(七)
- Inno Setup 安装、卸载前检测进程或服务
- Codeforces Round #222 (Div. 1) C. Captains Mode 对弈+dp
- 基于Spring设计并实现RESTful Web Services(转)
- 关于Java基本数据类型
- Android MonkeyRunner自动拨打电话
- 纯CSS3实现超立体的3D图片侧翻倾斜效果
- C#获取上个月的第一天零点和最后一天23点59分59秒
- ThinkPHP 3.1.2 查询方式的一般使用1
- 如何一步一步用DDD设计一个电商网站(十四)—— 回顾与总结
- Babel学习小记
- js实现全选反选(开关门)
- [ZZ] 麻省理工( MIT)大神解说数学体系
- idea tomcat上传图片,无法显示的问题解决
- win都是数据更新
- 微信小程序接入百度统计
- Java Socket通信实例
- jQuery扩展插件以及正则相关函数练习
- SQL Server Mobile/Compact Edition 简单介绍
热门文章
- 微软BI 之SSIS 系列 - 变量查询语句引起列输出顺序不一致的解决方法
- 使用ThreadLocal来实现一个本地缓存
- 在Razor中输出Html的两种方式
- CSS中的偏僻知识点
- 如何生成唯一的server Id,server_id为何不能重复?
- device eth0 does not seem to be present, delaying initialization(转)
- Nginx 介绍
- Java并发之线程池ThreadPoolExecutor源码分析学习
- unity, ComputeScreenPos 作用
- Docker 使用Docker知识简易部署一个LNMP平台