题目链接:Video Reviews
题意:有n个人(有顺序),a[i]代表i之前有a[i]个人评论了这个游戏这个人才会评论,但是你可以强迫一个人评论(无论他评论的条件是多少人),问你最少强迫多少人评论才能满足不少于m个人都评论。
样例解释
input
7 4
2 1 3 3 4 2 3
output
1
强迫第一个人评论,那么编号为1,2,6,7的人都会评论,共4个人,输出1
思路:
- 首先是一个贪心:假设你一定要强迫一个人,那么最优方案为强迫第一个不加入的人,因为从加入的人开始后面的人加入的可能性变大,越前面加入,开始的越早(转自Video Reviews (强大的二分)%%%)。
- 然后是二分找强迫次数
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 200010;
int n, m;
int a[N];
int judge(int now)
{
int tmp = 0;
for(int i = 0; i < n; i++)
{
if(a[i] > tmp && now > 0)
{
now--;
tmp++;
}
else if(tmp >= a[i]) tmp++;
}
if(tmp >= m) return 1;
else return 0;
}
int main()
{
scanf("%d %d", &n, &m);
for(int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
int l, r;
l = 0, r = m;
while(r > l)
{
int mid = (l+r)/2;
if(judge(mid)) r = mid;
else l = mid+1;
}
printf("%d\n", l);
return 0;
}