hdu 6231 -- K-th Number(二分+尺取)
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.
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.
题意:有个数列包含n个数,现在从这个数列中取出所有子区间中的第k大的数(所有长度大于等于k的子区间),构成一个新的数列,求这个新的数列的第M大的数?
思路:我们可以利用尺取求出区间第k大数大于等于x的这样的区间有多少个,然后根据这个进行二分求出准确的第M大数,时间复杂度O(n*log n)。
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int N=1e5+;
int a[N],b[N]; LL get(int x,int n,int k)
{
LL ans=;
int pos=;
int num=;
for(int i=;i<=n;i++)
{
if(a[i]>=x) num++;
if(num==k)
{
ans+=n-i+;
while(a[pos]<x)
{
ans+=n-i+;
pos++;
}
num--; pos++;
}
}
return ans;
} int main()
{
int T; cin>>T;
while(T--)
{
int n,k;
LL m;
scanf("%d%d%lld",&n,&k,&m);
for(int i=;i<=n;i++) scanf("%d",&a[i]), b[i]=a[i];
sort(b+,b+n+);
int num=unique(b+,b+n+)-(b+);
int L=,R=num;
while(L<=R)
{
int mid=(L+R)>>;
LL tmp=get(b[mid],n,k);
if(tmp<m) R=mid-;
else L=mid+;
}
printf("%d\n",b[L-]);
}
return ;
}
hdu 6231 -- K-th Number(二分+尺取)的更多相关文章
- Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 【二分 + 尺取】
任意门:http://codeforces.com/contest/1073/problem/C C. Vasya and Robot time limit per test 1 second mem ...
- 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 ...
- 【二分+尺取】HDU 6119 小小粉丝度度熊
http://acm.hdu.edu.cn/showproblem.php?pid=6119 [思路] 首先通过处理交叉的可以处理成不交叉的 然后二分查找答案 如何判断一个长度是否可行? 双指针O(n ...
- 51nod-1686 第K大区间(二分+尺取法)
题目链接: 第K大区间 基准时间限制:1 秒 空间限制:131072 KB 定义一个区间的值为其众数出现的次数.现给出n个数,求将所有区间的值排序后,第K大的值为多少. Input 第一行两个数 ...
- UVALive - 2678 二分/尺取
题意:求最小的长度L满足该长度上的元素和大于等于S 最近dp做多了总有一种能用dp解决一切的错觉 二分长度解决 #include<iostream> #include<algorit ...
- hdu 6119 小小粉丝度度熊(尺取)
小小粉丝度度熊 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU 5178 pairs【二分】||【尺取】
<题目链接> 题目大意: 给定一个整数序列,求出绝对值小于等于k的有序对个数. 解题分析: $O(nlong(n))$的二分很好写,这里就不解释了.本题尺取$O(n)$也能做,并且效率很不 ...
- 1686 第K大区间(尺取+二分)
1686 第K大区间 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 定义一个区间的值为其众数出现的次数.现给出n个数,求将所有区间的值排序后,第K大的值为多少. ...
- 【单调队列+尺取】HDU 3530 Subsequence
acm.hdu.edu.cn/showproblem.php?pid=3530 [题意] 给定一个长度为n的序列,问这个序列满足最大值和最小值的差在[m,k]的范围内的最长子区间是多长? [思路] 对 ...
随机推荐
- 关于加载离线SHP文件、geodatabase文件所遇到的路径问题
正文开始之前还是先吐槽一下,一行代码DEBUG了一天不知道怎么改,终于误打误撞弄出来了(以下以shp文件为例) 对于虚拟机测试 public String getPath(){ File sdDir ...
- golang 标准库间依赖的可视化展示
简介 国庆看完 << Go 语言圣经 >>,总想做点什么,来加深下印象.以可视化的方式展示 golang 标准库之间的依赖,可能是一个比较好的切入点.做之前,简单搜了下相关的内 ...
- Linux修改hostname的几种方法
修改hostname有几种方式 1: hostname DB-Server --运行后立即生效(新会话生效),但是在系统重启后会丢失所做的修改 ...
- CentOS7 + Nginx1.13.5 + PHP7.1.10 + MySQL5.7.19 源码编译安装
一.安装Nginx 1.安装依赖扩展 # yum -y install wget openssl* gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng ...
- webpack2使用ch6-babel使用 处理es6 优化编译速度
1 目录结构 安装依赖 cnpm install --save-dev babel-loader babel-core babel-preset-env babel-preset-latest &qu ...
- Python统计列表中的重复项出现的次数的方法
本文实例展示了Python统计列表中的重复项出现的次数的方法,是一个很实用的功能,适合Python初学者学习借鉴.具体方法如下:对一个列表,比如[1,2,2,2,2,3,3,3,4,4,4,4],现在 ...
- ZOJ3508 The War 贪心,最大流
传送门:I Am Here 常规解法是贪心,但是在复习最大流的写法,因此用sap来写的.思路是很好想的 #include<cstdio> #include<cstdlib> # ...
- HDU1085 多重背包
Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- JS全选与不选、反选
思路: 1.获取元素. 2.用for循环历遍数组,把checkbox的checked设置为true即实现全选,把checkbox的checked设置为false即实现不选. 3.通过if判断,如果ch ...
- mysql多实例-主从复制安装
安装环境:Centos6.5 mysql版本:mysql-5.5.32.tar.gz 一:安装前准备: 1.安装一些依赖库 yum install cmake gcc gcc-c++ ncurses- ...