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 52 0 1
3 10 0 0
4 62 4 3 5

Sample Output

42 3 2
10 1 0
02 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;
}

  

时间: 2024-11-16 07:35:47

Codeforces Round #377 (Div. 2) B. Cormen — The Best Friend Of a Man(贪心)的相关文章

Codeforces Round #377 (Div. 2)

题目链接:https://codeforces.com/contest/732 A - Buy a Shovel 题意:有无穷枚10元硬币和1枚r元硬币,且r是[1,9].每个物品的单价是x元,求最少买多少件不用找钱. 题解:记得区分用r元和不用r元两种情况,分别对应模10的余数为r或者为0.现在枚举不超过10个就可以出结果. B - Cormen --- The Best Friend Of a Man 题意:宠物狗每连续两天都需要总共走至少k步,注意对于[0,1]和[n,n+1]没有这个要求

Codeforces Round #377 (Div. 2) 732A B C D E

蒟蒻只能打div 2 A题水,10和个位数的使用互不影响,所以直接在模10意义下做 1 #include<bits/stdc++.h> 2 using namespace std; 3 int k,r; 4 int main(){ 5 scanf("%d%d",&k,&r); 6 k=k%10; 7 for(int i=1;i<=100;i++){ 8 if(k*i%10==0||k*i%10==r) { 9 printf("%d\n&quo

Codeforces Round #177 (Div. 1) C. Polo the Penguin and XOR operation(贪心)

题目地址:http://codeforces.com/problemset/problem/288/C 思路:保证位数尽量大的情况下使得每二进制位均为一的情况下结果最大,从后向前枚举(保证位数尽量大),num表示该数i二进制有几位,x即为使得与i异或后每二进制位均为一的数,v[x]标记是否使用过该数,若未使用则与i搭配. #include<cstdio> #include<cmath> #include<cstring> #include<iostream>

Codeforces Round #177 (Div. 2)---E. Polo the Penguin and XOR operation(贪心)

Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 to n, inclusive. For permutation p?=?p0,?p1,?-,?pn, Polo has defined its beauty - number . Expression means applying the operation of bitwise excluding "

Codeforces Round #169 (Div. 2)---C. Little Girl and Maximum Sum(简单贪心)

The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defi

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #424 (Div. 2) C. Jury Marks(乱搞)

题目链接:Codeforces Round #424 (Div. 2) C. Jury Marks 题意: 给你一个有n个数序列,现在让你确定一个x,使得x通过挨着加这个序列的每一个数能出现所有给出的k个数. 问合法的x有多少个.题目保证这k个数完全不同. 题解: 显然,要将这n个数求一下前缀和,并且排一下序,这样,能出现的数就可以表示为x+a,x+b,x+c了. 这里 x+a,x+b,x+c是递增的.这里我把这个序列叫做A序列 然后对于给出的k个数,我们也排一下序,这里我把它叫做B序列,如果我