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 */
HDU - 6231 K-th Number (2017CCPC哈尔滨站 二分+尺取法)的更多相关文章
- HDU - 6231:K-th Number (不错的二分)
Alice are given an array A[1..N]A[1..N] with NN numbers. Now Alice want to build an array BB by a pa ...
- 51nod-1686 第K大区间(二分+尺取法)
题目链接: 第K大区间 基准时间限制:1 秒 空间限制:131072 KB 定义一个区间的值为其众数出现的次数.现给出n个数,求将所有区间的值排序后,第K大的值为多少. Input 第一行两个数 ...
- HDU 6240 Server(2017 CCPC哈尔滨站 K题,01分数规划 + 树状数组优化DP)
题目链接 2017 CCPC Harbin Problem K 题意 给定若干物品,每个物品可以覆盖一个区间.现在要覆盖区间$[1, t]$. 求选出来的物品的$\frac{∑a_{i}}{∑b_ ...
- hdu 2158 最短区间版大家来找碴(尺取法)
Problem Description 给定一个序列,有N个整数,数值范围为[0,N).有M个询问,每次询问给定Q个整数,可能出现重复值.要求找出一个最短区间,该区间要包含这Q个整数数值.你能找的出来 ...
- HDU 6231
K-th Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Tot ...
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- HDU 6093 - Rikka with Number | 2017 Multi-University Training Contest 5
JAVA+大数搞了一遍- - 不是很麻烦- - /* HDU 6093 - Rikka with Number [ 进制转换,康托展开,大数 ] | 2017 Multi-University Tra ...
- 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 题意: 给一个序列由 ...
随机推荐
- WIN10平板 如何修改网络IP地址为固定
右击网络,属性,更改适配器设置,然后可以找到当前的无线网络 然后依次点开即可修改IP地址
- 外网IP监测上报程序(使用Poco库的SMTPClientSession发送邮件)
目录 IPReport 项目介绍 编译说明 安装使用说明 获取外网IP方式 邮件发送关键代码 IPReport 代码地址https://gitee.com/solym/IPReport 项目介绍 外网 ...
- C# 怎么让winform程序中的输入文本框保留上次的输入
选中TextBox控件,在属性窗格中找到(ApplicationSettings),然后设置它. 绑定配置文件 private Settings settings = new Settings(); ...
- 初始cfx开发webservice, 简单实例应用
项目结构图: 步骤一: 添加maven 依赖包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...
- o2o的关健在于线下!
其实,说到底,O2O不是那么简单的.一堆空降兵和一群自以为是的风投都把所有的线下产业想简单了.没有真正的去了解和体验这个产业,就动不动一个小点子,创新一个小平台就妄想能改变或替代中国某某产业.看看这几 ...
- 数据源从druid迁移到HikariCP
最近正好在做新项目,使用的是druid数据源,也真是巧,有朋友建议我使用HikariCP这个数据源,可以说是牛的一笔,速度快的飞起,性能极高! 要比druid不知道好多少倍,druid其实在某些情况下 ...
- 源码分析HotSpot GC过程(一)
«上一篇:源码分析HotSpot GC过程(一)»下一篇:源码分析HotSpot GC过程(三):TenuredGeneration的GC过程 https://blogs.msdn.microsoft ...
- oracle数据泵笔记
1.创建目录 查询已有目录:select * from dba_directories 创建并授权: CREATE DIRECTORY dump_dir AS '/tmp/' grant read,w ...
- yarn 常用命令(基于vue框架)
初始化项目 yarn add init 安装vue yarn add vue 安装webpack,webpack-dev-server(是一个小型的Node.js Express服务器) yarn a ...
- javascript and jquery 代码块
1.5秒隐藏 (setTimeout ) setTimeout( function(){ $('.ad_midd').slideUp(); } , 5000 )