Cormen — The Best Friend Of a Man
1 second
256 megabytes
standard input
standard output
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.
3 5
2 0 1
4
2 3 2
3 1
0 0 0
1
0 1 0
4 6
2 4 3 5
0
2 4 3 5
分析:模拟即可;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3fLL
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<ll,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
const int maxn=1e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,a[maxn];
ll ans;
int main()
{
int i,j;
scanf("%d%d",&n,&m);
rep(i,,n)scanf("%d",&a[i]);
rep(i,,n)
{
if(a[i]<m-a[i-])
{
ans+=m-a[i-]-a[i];
a[i]=m-a[i-];
}
}
printf("%lld\n",ans);
rep(i,,n)printf("%d ",a[i]);
//system("Pause");
return ;
}
Cormen — The Best Friend Of a Man的更多相关文章
- 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 ...
- CodeForces 732B Cormen — The Best Friend Of a Man
B. Cormen - The Best Friend Of a Man time limit per test 1 second memory limit per test 256 megabyte ...
- 【56.74%】【codeforces 732B】Cormen --- The Best Friend Of a Man
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- CodeForces 732B Cormen — The Best Friend Of a Man (贪心)
题意:给定n和k表示,狗要在任意连续两天散步次数要至少为k,然后就是n个数,表示每天的时间,让你增加最少次数使得这个条件成立. 析:贪心,策略是从开始到最后暴力,每次和前面一个相比,如果相加不够k,那 ...
- 算法导论 (Thomas H.Cormen / Charles E.Leiserson / Ronald L.Rivest / Clifford Stein 著)
第一部分 基础知识 第1章 算法在计算中的作用 第2章 算法基础 第3章 函数的增长 第4章 分治策略 第5章 概率分析和随机算法 第二部分 排序和顺序统计量 第6章 堆排序 第7章 快速排序 第8章 ...
- OpenCASCADE Interpolation - Lagrange
OpenCASCADE Interpolation - Lagrange eryar@163.com Abstract. Power basis polynomial is the most simp ...
- 数据结构与算法 Big O 备忘录与现实
不论今天的计算机技术变化,新技术的出现,所有都是来自数据结构与算法基础.我们需要温故而知新. 算法.架构.策略.机器学习之间的关系.在过往和技术人员交流时,很多人对算法和架构之间的关系感 ...
- hdu 1232, disjoint set, linked list vs. rooted tree, a minor but substantial optimization for path c 分类: hdoj 2015-07-16 17:13 116人阅读 评论(0) 收藏
three version are provided. disjoint set, linked list version with weighted-union heuristic, rooted ...
- c/c++常用网址
个人主页Dennis Ritchie's home pagehttp://cm.bell-labs.com/cm/cs/who/dmr/index.html Brian Kernighan's hom ...
随机推荐
- linux下svn用法
linux下svn的一些常用命令: checkout代码到当前目录: svn co svn://192.168.22.23/project ./ swich 切换分支: 先查看当前工作副本:svn ...
- LeetCode OJ 45. Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Chapter 14_3 非全局的环境
关于“环境”的一大问题在于它是全局的,任何对它的修改都会影响程序的所有部分. 例如:若安装一个元表用于控制全局变量的访问,那么整个程序都必须遵循这个规范. 当使用某个库时,没有先声明就使用了全局变量, ...
- ios navigationController代码创建
1.新建类文件FirstViewController,subClass of:UIViewController 2.新建类文件MYNavigationController,subClass of:UI ...
- erlang lists模块函数使用大全
转自:http://qqdenghaigui.iteye.com/blog/1554541 一,带函数Pred1, all(Pred, List) -> boolean()如果List中的每个元 ...
- Python 跳出多重循环
Python 本身没有“break n” 和“goto” 的语法,这也造成了Python 难以跳出多层(特定层数)循环.下面是几个跳出多层(特定层数)循环的tip. 1.自定义异常 class g ...
- ThinkPHP模板
[MVC模式] M:Model 数据模型层,负责数据操作 V:View 视图层,负责显示视图 C:Controller 控制器,实现业务逻辑 tp框架url地址可以由以下四种 http://网址/in ...
- A - 小Y上学记——修学分
A - 小Y上学记——修学分 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) S ...
- C# for循环嵌套
今天,我主要学习了for循环的嵌套. 第一个部分主要学习了打印直角在不同方向的三角形,求阶乘的和以及打印九九乘法表等. 例:打印直角在左下角的三角形. Console.WriteLine(" ...
- iis本地无法通过ip地址访问网站
防火墙等通通检查过没有发现问题,最后发现是我安装了一款adsafe的广告过滤软件捣的鬼,此软件设置界面没有这样的设置条款,估计是默认的配置,退出后,就ok了.