2017CCPC哈尔滨站 K-th Number 【二分】
2017-11-12 16:03
267 查看
K-th Number
[b]Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 76 Accepted Submission(s): 26
[/b]
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case, output a single line containing the
M-th
largest element in the array B.
[align=left]Sample Input[/align]
2
5 3 2
2 3 1 5 4
3 3 1
5 8 2
[align=left]Sample Output[/align]
3
2
题意:给出一个长度为N的数组a[i],问在这个数组中的所有连续区间中的第K小的数组成的数组b[i]中第M大的数。
思路难得,如果想得到所有区间中的第K小,达到O(n*n)很轻松,转换一下思路,一个区间中,如果第K小的数要大于最终答案,那么这个区间中至少要有K个数大于等于最终答案。我们设最终答案为x,且这个x满足有大于等于m个区间的第k小大于等于x,这个区间的数量我们可以通过枚举每一个区间的端点对于每个左端L,可以找一个最小的r使得,当右端点大于等于r时,[L,r]有k个数大于等于x。所以L为左端点的区间中满足要求的区间数有 n-r+1个,通过区间的数量与m的关系决定二分的走向。
#include<bits/stdc++.h> using namespace std; int n,m,k,a[100001]; long long find(int x) { long long ans=0; int q=0; int out=0; for(int i=1;i<=n;i++){ while(out<k&&q<=n){ if(a[++q]>=x) out++; } ans+=n-q+1; if(a[i]>=x) out--; } return ans; } int main(){ long long m; int t,l,r,mid; scanf("%d",&t); while(t--){ scanf("%d%d%lld",&n,&k,&m); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); } l=1,r=1000000000; while(l<r) { mid=(l+r+1)/2; if(find(mid)>=m) l=mid; else r=mid-1; } printf("%d\n",r); } return 0; }
相关文章推荐
- HDU - 6231 K-th Number (2017CCPC哈尔滨站 二分+尺取法)
- 2017CCPC哈尔滨 B:K-th Number(二分)
- 2017ccpc哈尔滨 hdu 6231 B k-th number 题解 二分答案+尺取法
- [HDU] 6231 - 哈尔滨 - 感谢哈理工 - K-th Number - 二分 + 尺取
- POJ 2104 K-th Number【整体二分 + 树状数组】
- poj2104 K-th Number(整体二分+树状数组)
- hdu 2104 K-th Number(静态求区间第k小+整体二分)
- CCPC.2017哈尔滨站-重现赛-B(二分)
- 2017CCPC秦皇岛H(二分 + 二分图的最大匹配数)
- POJ 2104 K-th Number 整体二分
- HDU 6231 K-th Number(二分+尺取)
- hdu 6231 -- K-th Number(二分+尺取)
- 【POJ - 2104 】K-th Number 【分块 (平方分割)+二分】
- POJ 2104 K-th Number(线段树+二分)
- POJ2104_K-th Number_归并树|二分 ||平方分割的分桶法|二分
- 2017中国大学生程序设计竞赛-哈尔滨站 (B.尺取 + 二分)
- Poj 2104 K-th Number(主席树&&整体二分)
- HDU - 6237 A Simple Stone Game (2017CCPC哈尔滨站 思维)
- poj 2104 K-th Number 区间第K大 二分 离散化 + (莫队 树状数组/平方分解/线段树)
- [POJ]2104 K-th Number 主席树&线段树合并&整体二分