Codeforces Round #377 (Div. 2) 被坑了

http://codeforces.com/contest/732/problem/B

题目要求任意两个连续的日子都要 >= k

那么如果a[1] + a[2] < k,就要把a[2]加上数字使得总和 = k,因为这样能传递到a[3]而且是最优的,

a[2]不需要加那么多,只需要加到两个的总和 = k即可,

很坑爹的地方就是 n = 1的时候,其实题目都说了,before the next n days 他们已经走了k

所以n = 1的时候,直接输出0和a[1]即可。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;

#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 1000 + 20;
int a[maxn], b[maxn];
void work() {
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        b[i] = a[i];
    }
    if (n == 1) {
        cout << 0 << endl;
        cout << a[1] << endl;
        return;
    }
    for (int i = 2; i <= n; ++i) {
        if (a[i] + a[i - 1] < k) {
            int add = k - (a[i] + a[i - 1]);
            a[i] += add;
        }
    }
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
        ans += a[i] - b[i];
    }
    cout << ans << endl;
    for (int i = 1; i <= n; ++i) {
        cout << a[i] << " ";
    }
    cout << endl;
}

int main() {
#ifdef local
    freopen("data.txt","r",stdin);
#endif
    work();
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/liuweimingcprogram/p/5972156.html