HDU 6231 K-th Number (2017CCPC哈爾濱 B)離散化+二分

K-th Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 3111    Accepted Submission(s): 1126


 

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.
 

 

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.
 

 

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,k,m(1 <= k <= n <= 1e5 , m <= n * (n+1) / 2 )
再給你一個包含n個數的序列a(1 <= a[i] <= 1e9 ),
求序列a 任意長度大於等於k的子區間的第k大值(這些值又構成了一個新的集合) 的第m大值。
 
思路:
比賽的時候打死就沒想到二分。。。
先對序列進行離散化將原數組a變爲b,對hash數組從小到大排序,直接二分第m大值對應hash數組的下標mid。
首先將0加入到vector當中,對於i=1,2,...,n,若b[i]>=mid,將i加入到vector當中。
依次遍歷vector,設當前遍歷的下標爲j(j=0,1,...,vc.size()-1)
若j<k 跳過
否則[ vc[j-k+1] , vc[i] ]這段區間內就會有>=k個數大於當前二分的下標對應的值。
以[vc[j-k]+1,vc[j-k+1]]爲左端點,[vc[i],n]爲可選右端點。
共有(vc[j-k+1]-vc[j-k])*(n-vc[i]+1)個區間的第k大值>=當前二分的下標對應的值。(其實此時枚舉的是符合條件的區間的左端點)
這樣就可以二分出答案。。。(仔細品)
代碼:
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define mst(head,x,n) memset(head+1,x,n*sizeof(head[0]))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define dep(i,a,b) for(int i=(a);i>=(b);i--)
using namespace std;
const int maxn=1e5+5;
//const double pi=acos(-1.0);
//const double eps=1e-9;
//const ll mo=1e9+7;
ll n,m,k;
ll ans,tmp,cnt,fg,tot;
vector<int>vc;
ll a[maxn],hs[maxn],b[maxn];
template <typename T>
inline void read(T &X){
    X=0;int w=0; char ch=0;
    while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
    while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    if(w) X=-X;
}
int jud(int mid){
    vc.clear();
    ll sum=0;
    vc.push_back(0);
    rep(i,1,n){
        if(b[i]>=mid) vc.push_back(i);
    }
    int up=vc.size()-1;
    rep(i,0,up){
        if(i<k) continue;
        sum+=(ll)(vc[i-k+1]-vc[i-k])*(ll)(n-vc[i]+1);
    }
    return sum>=m;
}
int main(){
    int T,cas=1;
    read(T);
    while(T--)
    {
        read(n);read(k);read(m);
        rep(i,1,n) {read(a[i]);hs[i]=a[i];}
        sort(hs+1,hs+1+n);
        tot=unique(hs+1,hs+1+n)-hs-1;
        rep(i,1,n){
            b[i]=lower_bound(hs+1,hs+1+tot,a[i])-hs;
        }
        int l=1,r=tot,ans=0;
        while(l<=r){
            int mid=(l+r)>>1;
            if(jud(mid)) {
                ans=mid;
                l=mid+1;
            }
            else r=mid-1;
        }
        printf("%d\n",hs[ans]);
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章