您的位置:首页 > 其它

hdu 5248(二分)

2015-05-30 19:06 337 查看
题解:二分出最小代价x,然后倒着让每个数字都在x的变化范围内单调递减,如果无法实现就return false,左边界为x + 1,否则右边界是x。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 100005;
long long n, a
;

bool judge(long long x) {
int temp = a[n - 1] + x;
for (int i = n - 2; i >= 0; i--) {
if (abs(temp - 1 - a[i]) <= x)
temp = temp - 1;
else if (temp - 1 >= a[i])
temp = a[i] + x;
else
return false;
}
return true;
}

int main() {
int t, cas = 1;
scanf("%d", &t);
while (t--) {
scanf("%lld", &n);
long long l = 0, r = 1000000;
for (int i = 0; i < n; i++)
scanf("%lld", &a[i]);

while (l < r) {
long long mid = (l + r) / 2;
if (judge(mid))
r = mid;
else
l = mid + 1;
}
printf("Case #%d:\n%lld\n", cas++, l);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: