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哈尔滨站 二分+尺取法)的更多相关文章

  1. 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 ...

  2. 51nod-1686 第K大区间(二分+尺取法)

    题目链接: 第K大区间 基准时间限制:1 秒 空间限制:131072 KB    定义一个区间的值为其众数出现的次数.现给出n个数,求将所有区间的值排序后,第K大的值为多少. Input 第一行两个数 ...

  3. HDU 6240 Server(2017 CCPC哈尔滨站 K题,01分数规划 + 树状数组优化DP)

    题目链接  2017 CCPC Harbin Problem K 题意  给定若干物品,每个物品可以覆盖一个区间.现在要覆盖区间$[1, t]$. 求选出来的物品的$\frac{∑a_{i}}{∑b_ ...

  4. hdu 2158 最短区间版大家来找碴(尺取法)

    Problem Description 给定一个序列,有N个整数,数值范围为[0,N).有M个询问,每次询问给定Q个整数,可能出现重复值.要求找出一个最短区间,该区间要包含这Q个整数数值.你能找的出来 ...

  5. HDU 6231

    K-th Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Tot ...

  6. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  7. HDU 6093 - Rikka with Number | 2017 Multi-University Training Contest 5

    JAVA+大数搞了一遍- - 不是很麻烦- - /* HDU 6093 - Rikka with Number [ 进制转换,康托展开,大数 ] | 2017 Multi-University Tra ...

  8. hdu 6216 A Cubic number and A Cubic Number【数学题】

    hdu 6216 A Cubic number and A Cubic Number[数学] 题意:判断一个素数是否是两个立方数之差,就是验差分.. 题解:只有相邻两立方数之差才可能,,因为x^3-y ...

  9. HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)

    HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意:  给一个序列由 ...

随机推荐

  1. 树莓派3中没有/dev/video0的解决方法(使用OpenCV编程调用树莓派摄像头的方法)

    一.问题 使用下列方法调用OpenCV编程调用树莓派摄像头时总是失败,提示调用Grabber的start()时失败. import org.bytedeco.javacpp.opencv_core; ...

  2. java的两种同步方式, Synchronized与ReentrantLock的区别

    java在编写多线程程序时,为了保证线程安全,需要对数据同步,经常用到两种同步方式就是Synchronized和重入锁ReentrantLock. 相似点: 这两种同步方式有很多相似之处,它们都是加锁 ...

  3. CentOS7中systemctl的使用与CentOS6中service的区别

    https://blog.csdn.net/u012834750/article/details/80501440 从CentOS 7.x开始,CentOS开始使用systemd服务来代替daemon ...

  4. SNF快速开发平台MVC-名片管理(实际名片样式)

    名片管理实际的做的意义在于演示应用,在这里使用的技术有排序控件,查询条件.自由样式瀑布流式分页等技术. 下面是自由样式效果图: 下面表格样式效果图: 具体操作: 新增名片 在新增时可以上传图像进行裁剪 ...

  5. mysql分享二-防止sql注入

    一:pdo 提供给预处理语句的参数不需要用引号括起来,驱动程序会自动处理.如果应用程序只使用预处理语句,可以确保不会发生SQL 注入.(然而,如果查询的其他部分是由未转义的输入来构建的,则仍存在 SQ ...

  6. [svc]find+xargs/sed&sed后向引用+awk多匹配符+过滤行绝招总结&&产生随机数

    30天内的文件打包 find ./test_log -type f -mtime -30|xargs tar -cvf test_log.tar.gz find,文件+超过7天+超过1M的+按日期为文 ...

  7. ES6入门基础

    let和const 一.块级作用域 ES5 只有全局作用域和函数作用域,没有块级作用域,这样的缺点是:1.用来计数的循环变量泄露为全局变量.2.内层变量可能会覆盖外层变量 var tmp = new ...

  8. 读取Excel二进制写入DB,并从DB中读取生成Excel文件

    namespace SendMailSMSService { class Program { static void Main(string[] args) { var connString = Sq ...

  9. border绘制三角形

    (1)有边框的三角形 我们来写下带边框的三角形. 如果是一个正方形,我们写边时,会用到border,但我们这里讨论的三角形本身就是border,不可能再给border添加border属性,所以我们需要 ...

  10. mac中安装wxpython

    一.简介 wxPython是Python语言的一套优秀的GUI图形库,允许Python程序员很方便的创建完整的.功能键全的GUI用户界面. wxPython是作为优秀的跨平台GUI库wxWidgets ...