这题窝觉得贪心才是才是正解,但是贪心没贪出来,QAQ,二分更好想
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <vector> #include <queue> using namespace std; typedef long long LL; const int MOD = 1e9 + 7; const int INF = 0x7fffffff; const int N = 1e6 + 10; int a[N], n; bool f(int x) { int pre = a[0] - x; for(int i = 1; i < n; i++) { if(pre >= x + <span style="font-family: Arial, Helvetica, sans-serif;">a[i]</span>) return false; else pre = max(a[i] - x, pre + 1);//pre+1是看当前减去x是否小于pre-1,如果是则不能 } return true; } int main() { int T, C = 1; scanf("%d", &T); while(T--) { scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%d", &a[i]); } int ans = 0; int l = 0, r = 1e7, mid; while(r >= l) { int mid = (l + r) / 2; if(f(mid)) { ans = mid; r = mid - 1; } else { l = mid + 1; } } printf("Case #%d:\n%d\n", C++, ans); } return 0; }
原文:http://blog.csdn.net/u013445530/article/details/46277839