HDU 6231
K-th Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 599 Accepted Submission(s): 234
Problem Description
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.
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.
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
Source
题意:
大小为n的a数组,把其中所有的长度不小于k的区间中第k大的数加入到b数组,最后求b数组中的m大的数
输入:
n,k,m
a[1~n];
代码:
//m要用long long 啊。 //二分答案x,然后尺取。找到有多少个区间存在至少k个大于等于x的数,如果这样的区间数不少于m个就说明第m大的数 //比x大,因此有单调性。尺取区间[l,r]中有k个不小于x的数那么会有n-r+1个符合的区间。 #include<iostream> #include<cstdio> #include<cstring> using namespace std; typedef long long ll; const int MAXN=100009; int n,k,a[MAXN]; ll m; bool solve(int x) { ll sum=0; int tmp=0,l=1,r=0; while(1){ while(tmp<k){ ++r; if(r>n) break; if(a[r]>=x) tmp++; } if(r>n) break; sum+=(n-r+1); if(a[l]>=x) tmp--; l++; } if(sum>=m) return 1; return 0; } int main() { int t; scanf("%d",&t); while(t--){ scanf("%d%d%lld",&n,&k,&m); for(int i=1;i<=n;i++) scanf("%d",&a[i]); int l=1,r=1e9,ans=0; while(l<=r){ int mid=(l+r)>>1; if(solve(mid)) { ans=mid;l=mid+1; } else r=mid-1; } printf("%d\n",ans); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?
2016-12-01 CF2.C(二分贪心)