HDU qw fans (two points)

HDU qw fans
Thinking:
Suppose a minimum of personal interview x, x divided by two between m to n, x and sort these people, and determines whether there exists m height difference between the highest and personal satisfaction minimum height does not exceed the k-th unit of length. The process has been updated binary value of x, if the last is -1, it indicates that there is no output Impossible, or output.

Source: graduate examination on a computer simulation game

#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<list>
#include<set>
#include<cmath>
using namespace std;
typedef long long ll;
#define N 100050
int a[N], b[N];
int n, m, k;

bool check(int x){
	for(int i=1;i<=x;i++){
		b[i] = a[i];
	}
	sort(b+1,b+x+1);
	for(int i=1;i+m-1<=x;i++){
		if(b[i+m-1]-b[i]<=k) return true; 
	}
	return false;
}

int main() {
	int t;
	cin >> t;
	while(t--){
		cin >> n >> m >> k;
		for(int i=1;i<=n;i++) scanf("%d",a+i);
		int l = m,r = n,mid,res=-1;
		while(l<=r){
			mid = (l+r)/2;
			if(check(mid)){
				res = mid;
				r = mid - 1;
			}else {
				l = mid+1;
			}
		}
		if(res==-1) cout << "impossible\n";
		else cout << res << endl;
	}
	return 0;
}
Published 79 original articles · won praise 37 · views 8883

Guess you like

Origin blog.csdn.net/SinclairWang/article/details/104109259