2017ccpc哈尔滨 hdu 6231 B k-th number 题解 二分答案+尺取法

题目链接:https://cn.vjudge.net/problem/HDU-6231

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
题意,t组数据,n,k,m,对于数列中每个连续的且长度大于k的区间,找出他的第k大数,然后在这些数中找出第m大的数。
做法:二分答案,然后难点变为怎么统计答案作为第K大的区间个数(这里说法是不严谨的)。首先做一个转化,对于每一个答案,要使该答案满足第m大,那么说把该答案带入到原数列中,肯定会有m个区间满足该答案是第k大,如果多了(区间数大于m)说明该答案小了,否则说明该答案大了。以此作为二分依据,那么该如何统计满足条件的区间数呢?利用尺取法即可。定义两个指针分别用来统计前面和后面的区间数。具体情况先看代码,然后我再用一组样例来解释。
code:

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int N,K;
ll M;
int a[100005];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值