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 ...
随机推荐
- setTimeout 定时器用法
setTimeout($(".msg").slideUp(5000)); msg的div 5秒往上收边 setTimeout("函数名称",1000);
- el表达式跟ognl表达式的区别
: EL表达式 单纯在jsp页面中出现,是在四个作用域中取值,page,request,session,application. 如果在struts环境中,它除了有在上面的四个作用域的取值功能外,还 ...
- POJ 3537 Crosses and Crosses(SG/还未想完全通的一道SG)
题目链接 #include<iostream> #include<cstdio> #include<cstring> using namespace std; ]; ...
- POJ 3020 Antenna Placement(无向二分图的最小路径覆盖)
( ̄▽ ̄)" //无向二分图的最小路径覆盖数=顶点总数-最大匹配数/2(最大匹配数=最小点覆盖数) //这里最大匹配数需要除以2,因为每两个相邻的*连一条边,即<u,v>和< ...
- delphi 中sql的语法规范
1.引号配对: 这是在Delphi使用SQL语句时容易出错的地方,由于delphi规定在字符串中用两个西文的单引号“''”表示一个“'”,在拼装语句的时候就容易疏忽遗漏. Delphi里有个函数Q ...
- ESPlatform 支持的三种群集模型 —— ESFramework通信框架 4.0 进阶(09)
对于最多几千人同时在线的通信应用,通常使用单台服务器就可以支撑.但是,当同时在线的用户数达到几万.几十万.甚至百万的时候,我们就需要很多的服务器来分担负载.但是,依据什么规则和结构来组织这些服务器,并 ...
- LeetCode OJ 64. Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- Gentoo安装详解(四)-- 声卡设置
硬件检测 To choose the right driver, first detect the used audio controller. You can use lspci for this ...
- VS中调试时监控线程的最后错误代码
进行调试的时候,监控线程的最后错误代码是非常有用的. 在 Microsoft Visual studio 中,Microsoft 的调试程序支持一个非常有用的特性,即可以配置 Watch 窗口,以便始 ...
- C++对象模型笔记之程序设计模型
C++程序设计模型支持三种程序设计模型 1.程序模型(procedural model) 可以理解为过程化模型,就像C一样 2.抽象数据类型模型(ADT) 数据结构教材里有说过,查了下资料也不是很明确 ...