K. Video Reviews

http://codeforces.com/gym/101755/problem/K

题解:二分+贪心(数据大且思想上可贪)

       直接二分答案,不要犹豫(千万不要去二分下标位置),票不够就劝说,够就票加1,每次劝说都ans++,这样可保证你这么劝说这些人绝对能把评论拼满了 判断ans和二分值就可以了,

#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<stdlib.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=1000000;
int n,h;
int a[maxn];
int check(int m){
	int t=0;
	int num=0;
	for(int i=0;i<n;i++){
		if(a[i]<=t){
			t++;
		}
		else{
			if(num<m){
				num++;
				t++;
			}
		}
		if(num>m) return 0;
	}
	if(num<=m&&t>=h) return 1;
	else return 0;
}
int main(){
	cin>>n>>h;
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	int l=-1;
	int r=n;
	int ans=inf;
	while(l<=r){
		int mid=(l+r)/2;
		if(check(mid)){
		//	cout<<l<<" "<<r<<endl;
			r=mid-1;
			ans=min(ans,mid);
		//	cout<<ans<<endl;
		}
		else{
		//	cout<<l<<" "<<r<<endl;
			l=mid+1;
		}
	}
	cout<<ans<<endl;
} 

猜你喜欢

转载自blog.csdn.net/gml1999/article/details/89195354
今日推荐