【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 parameter?KK?as following rules:?
Initially, the array B is empty. Consider each interval in array A. If the length of this interval is less than?KK, then ignore this interval. Otherwise, find the?KK-th largest number in this interval and add this number into array?BB.?
In fact Alice doesn't care each element in the array B. She only wants to know the?MM-th largest element in the array?BB. 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),MN(1≤N≤105),K(1≤K≤N),M. The second line contains?NN?numbers?Ai(1≤Ai≤109)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?MM-th largest element in the array?BB.
Sample Input
2 5 3 2 2 3 1 5 4 3 3 1 5 8 2Sample Output
3 2題目大意:
有一個長度為N的序列A={a1,a2,a3.....aN},該序列有多個子區間,考慮所有長度大于等于K的區間,我們找出所有這些區間的第K大值b,這些b又組成了一個序列B,現在你需要求出序列B的的第M大值。
解題報告:
首先我們知道如果這個數是x的話,那>x的數都可以看成x,二分這個數之后,在check函數中看有多少個區間滿足第K大的數是>=x的,我們如果把>=x的數都標記成1的話,也就是看有多少區間的1的個數是>=k的,這一點可以尺取完成。如果區間數>=M的話,說明需要調整左端點,同時需要記錄答案。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair using namespace std; const int MAX = 2e5 + 5; int a[MAX],b[MAX]; int qq[MAX],sum[MAX]; int n,k; ll M; int ok(int x) {ll sum = 0;for(int i = 1; i<=n; i++) qq[i] = a[i] >= x;int l = 1,r = 1,tmp = 0;while(l<=r && r<=n) {tmp += qq[r];while(tmp >= k && l<=r) {tmp -= qq[l];l++;}sum += l-1;r++;} // sum--;return sum>=M; } int main() {int t;cin>>t;while(t--) {scanf("%d%d%lld",&n,&k,&M);for(int i = 1; i<=n; i++) scanf("%d",a+i),b[i] = a[i]; // int l = 0,r = *max_elememt(a+1,a+n+1); 這樣不行 太大了。sort(b+1,b+n+1);int l=1,r=n,m,ans;while(l<=r) {int mid=(l+r)>>1;if(ok(b[mid])) l = mid+1,ans=mid;//else r = mid-1;}printf("%d\n",b[ans]); }return 0 ; }?
總結
以上是生活随笔為你收集整理的【HDU - 6231】K-th Number(二分,思维)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sqlservr.exe - sqlse
- 下一篇: 银保监会憋不住了,周五出台最狠新规,银行