codeforces 732B - Cormen --- The Best Friend Of a Man
来源:互联网 发布:淘宝剁手党段子 编辑:程序博客网 时间:2024/05/17 22:17
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 integersb1, b2, ..., bn (bi ≥ ai), where bi means the total number of walks with the dog on the i-th day.
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.
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.
简单的贪心算法,就是当两个相加小于k时,选择将后面一个数增大;
AC代码:
# include <stdio.h>using namespace std;typedef long long int ll;int ans[510], a[510];int main(){int i,j, k, n;scanf("%d%d", &n, &k);for(i=1; i<=n; i++){scanf("%d", &a[i]);}if(n==1){printf("%d\n%d", 0, a[1]);}else{ans[1]=a[1];int sum=0;for(i=2; i<=n; i++){if(a[i]+ans[i-1]<k){sum=sum+k-a[i]-ans[i-1];ans[i]=k-ans[i-1];}else{ans[i]=a[i];}}printf("%d\n", sum);for(i=1; i<=n; i++){if(i!=n){printf("%d ", ans[i]);}else{printf("%d", ans[i]);}}}return 0;}
- codeforces 732B - Cormen --- The Best Friend Of a Man
- 【56.74%】【codeforces 732B】Cormen --- The Best Friend Of a Man
- codeforces 732B Cormen — The Best Friend Of a Man (水题)
- CodeForces 732B Cormen — The Best Friend Of a Man
- Codeforces 732B Cormen — The Best Friend Of a Man【贪心】
- Codeforces 732B-Cormen—The Best Friend Of a Man
- CodeForces 732B 之 Cormen — The Best Friend Of a Man
- CodeForces 732B Cormen — The Best Friend Of a Man
- CodeForces-732B-Cormen — The Best Friend Of a Man(简单模拟)
- CodeForces 732 B.Cormen — The Best Friend Of a Man(贪心)
- codeforces 732B Cormen — The Best Friend Of a Man
- CodeForces 732B Cormen — The Best Friend Of a Man
- B. Cormen — The Best Friend Of a Man
- 【codeforces 723 B Cormen — The Best Friend Of a Man】
- Codeforces Round #377 (Div. 2) B. Cormen — The Best Friend Of a Man
- CodeForces 732B Cormen — The Best Friend Of a Man(带着宠物轧马路)
- Cormen — The Best Friend Of a Man (贪心)
- Codeforces Round #377 (Div. 2) Cormen --- The Best Friend Of a Man
- 实现一个函数翻转字符串
- c++调试帮助(assert、NDEBUG)
- Linux命令简写和全称
- my first blog
- 《潜规则:中国历史中的真实游戏》读后感
- codeforces 732B - Cormen --- The Best Friend Of a Man
- Java transient关键字使用小记
- SLAM学习资料整理
- 第八章上机练习2 老师答案
- String字符串去掉头尾的空格
- PAT T1001. Battle Over Cities - Hard Version (35)
- 在Centos7上搭建jenkins
- oracle删除表字段和oracle表增加字段
- springboot中配置文件说明