本文主要是介绍百度之星初赛1002(二分搜索),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
序列变换
Accepts: 816
Submissions: 3578
Time Limit: 4000/2000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
这题窝觉得贪心才是才是正解,但是贪心没贪出来,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 + a[i]) 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;
}
这篇关于百度之星初赛1002(二分搜索)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!