洛谷 P4409 [ZJOI2006] 皇帝的烦恼

题目链接-> OVO

题解:

很久没有写博客了,可能是因为最近太颓废了吧。

刚刚考完期末考试,无比期盼早点外出学习,不要面对成绩,害怕。

 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> 
#define N 20007 
using namespace std;
int n, a[N], maxn[N], minn[N], l, r, ans;
bool check(int x) {
    maxn[1] = minn[1] = a[1];
    for(int i = 2; i <= n; i++) 
        maxn[i] = min(a[i], a[1] - minn[i - 1]), minn[i] = max(0, a[i] - (x - a[i - 1] - a[1] + maxn[i - 1]));
    return !minn[n];
}
int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]), l = max(l, a[i] + a[i - 1]); r = l << 1;
    while(l <= r) {
        int mid = l + r >> 1;
        if(check(mid)) ans = mid, r = mid - 1;
        else l = mid + 1;
    }
    printf("%d", ans);
    return 0;
}

 

一世安宁

 

posted @ 2019-01-13 20:40  GTBA  阅读(288)  评论(6编辑  收藏  举报