日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【HDU - 6231】K-th Number(二分,思维)

發布時間:2023/12/10 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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 2

Sample 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(二分,思维)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。