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; }
相关文章推荐
- 浏览器原理与内存泄漏
- Python调用Matlab2014b引擎
- 关于detours的用法
- ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务
- linux c编程实现cd目录切换功能(带空格的目录名问题)
- 取得键盘每个按键的keyCode的值
- c++ 多态
- SD-Tax set up
- 英语总结——五月
- android 自学中的散乱笔记
- hdu 5246(模拟)
- 年轻程序员需要学习的5大经验
- nginx rewrite例子
- ScrollView 与 Autolayout
- 母函数
- onSaveInstanceState和onRestoreInstanceState触发的时机
- Android编译环境——ubuntu12.04上android2.3.4编译错误以及解决
- fzu 1692(矩阵快速幂)
- 凯云水利水电工程造价系统 (三) 材料单价 (2)
- [转]ubuntu bits/predefs.h:没有那个文件或目录