Codeforces Round #377 (Div. 2) B. Cormen — The Best Friend Of a Man(贪心)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #377 (Div. 2) B. Cormen — The Best Friend Of a Man(贪心)相关的知识,希望对你有一定的参考价值。
Description
Recently a dog was bought for Polycarp. The dog‘s name is Cormen. Now Polycarp has a lot of troubles. For example, Cormen likes going for a walk.
Empirically Polycarp learned that the dog needs at least k walks for any two consecutive days in order to feel good. For example, if k = 5and yesterday Polycarp went for a walk with Cormen 2 times, today he has to go for a walk at least 3 times.
Polycarp analysed all his affairs over the next n days and made a sequence of n integers a1, a2, ..., an, where ai is the number of times Polycarp will walk with the dog on the i-th day while doing all his affairs (for example, he has to go to a shop, throw out the trash, etc.).
Help Polycarp determine the minimum number of walks he needs to do additionaly in the next n days so that Cormen will feel good during all the n days. You can assume that on the day before the first day and on the day after the n-th day Polycarp will go for a walk with Cormen exactly k times.
Write a program that will find the minumum number of additional walks and the appropriate schedule — the sequence of integers b1, b2, ..., bn (bi ≥ ai), where bi means the total number of walks with the dog on the i-th day.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 500) — the number of days and the minimum number of walks with Cormen for any two consecutive days.
The second line contains integers a1, a2, ..., an (0 ≤ ai ≤ 500) — the number of walks with Cormen on the i-th day which Polycarp has already planned.
Output
In the first line print the smallest number of additional walks that Polycarp should do during the next n days so that Cormen will feel good during all days.
In the second line print n integers b1, b2, ..., bn, where bi — the total number of walks on the i-th day according to the found solutions (ai ≤ bi for all i from 1 to n). If there are multiple solutions, print any of them.
Sample Input
3 5
2 0 1
3 1
0 0 0
4 6
2 4 3 5
Sample Output
4
2 3 2
1
0 1 0
0
2 4 3 5
思路
题意:
给出一串数,要求前后连续的两个数的和不小于k,问每个数在符合要求的情况下,最少需要加多少,输出最后的串的值。
官方题解:
If we don‘t make enough walks during days i and i + 1, it‘s better to make an additional walk on day i + 1 because it also counts as a walk during days i + 1 and i + 2 (and if we walk one more time on day i, it won‘t help us in the future). So we can start iterating from the second day (1"=indexed). We will add max(0, k - ai - ai - 1) walks to the day i (and to our answer), so Cormen has enough walks during days i and i - 1. After we have iterated through all days, we can print the answer.
Time complexity: O(n).
#include<bits/stdc++.h> using namespace std; const int maxn = 505; int main() { int n,k,sum = 0,a[maxn]; scanf("%d%d",&n,&k); for (int i = 0;i < n;i++) scanf("%d",&a[i]); for (int i = 1;i < n;i++) sum += max(0,k-a[i-1]-a[i]),a[i] +=max(0,k-a[i-1]-a[i]); printf("%d\n",sum); printf("%d",a[0]); for (int i = 1;i < n;i++) printf(" %d",a[i]); printf("\n"); return 0; }
以上是关于Codeforces Round #377 (Div. 2) B. Cormen — The Best Friend Of a Man(贪心)的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #377 (Div. 2)F - Tourist Reform
Codeforces Round #377 (Div. 2) B. Cormen — The Best Friend Of a Man(贪心)
Codeforces Round #222 (Div. 1) (ABCDE)
Codeforces Round #436 E. Fire(背包dp+输出路径)