K-th Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 463 Accepted Submission(s): 181
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
Recommend
jiangzijing2015
题意:给你n个数,将所有大小不小于k的区间中的第k大值取出来,形成一个新的序列,找出新序列中第m大的值
解题思路:二分+尺取法,二分答案,二分时将不小于对应值的位置都变为1,其他都变为0,然后用尺取法就可以处理出有多少个区间的和不小于k,若这样的区间个数大于等于m,则答案应该继续变大,否则应该变小
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
int n, k;
int a[1000009], b[1000009], c[1000009];
LL m;
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]), b[i] = a[i];
sort(b + 1, b + 1 + n);
int mm = unique(b + 1, b + 1 + n) - b;
int l = 1, r = mm - 1, ans;
while (l <= r)
{
int mid = (l + r) >> 1;
for (int i = 1; i <= n; i++)
{
if (a[i] >= b[mid]) c[i] = 1;
else c[i] = 0;
}
int ll = 1, rr = 1, sum = 0;
LL tot = 0;
while (ll <= n)
{
while (sum < k&&rr <= n) sum += c[rr++];
if (sum >= k) tot += 1LL * (n - rr + 2);
sum -= c[ll++];
}
if (tot < m) r = mid - 1;
else { ans = mid, l = mid + 1; }
}
printf("%d\n", b[ans]);
}
return 0;
}