题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5248
题目如果从正面去做的话,很难想到一种有效的贪心策略去处理这个问题(不排除一些大神有特殊的想法),但是反过来,给我们一个答案,我们能够很容易的贪心去判断是否可行,所以我们采取二分答案,再反过来判断是否可行。
关于二分答案之后的判断,我们采取的贪心策略就是每一位都尽量的低。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100002;
const int INF = 1000002;
int a[maxn], b[maxn], n;
bool judge(int c)
{
b[0] = a[0]-c;
for(int i=1; i<n; i++)
{
if(b[i-1] < a[i]) b[i] = max(b[i-1]+1, a[i]-c);
else
{
if(a[i]+c <= b[i-1]) return false;
else b[i] = b[i-1]+1;
}
}
return true;
}
int main()
{
int T,t=1;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
int l = 0, r = INF, mid;
while(l < r)
{
mid = (l+r)>>1;
if(judge(mid)) r = mid;
else l = mid+1;
}
printf("Case #%d:\n%d\n", t++, l);
}
return 0;
}