Boxes and Candies(贪心)
Boxes and Candies
Time limit : 2sec / Memory limit : 256MB
Score : 300 points
Problem Statement
There are N boxes arranged in a row. Initially, the i-th box from the left contains ai candies.
Snuke can perform the following operation any number of times:
- Choose a box containing at least one candy, and eat one of the candies in the chosen box.
His objective is as follows:
- Any two neighboring boxes contain at most x candies in total.
Find the minimum number of operations required to achieve the objective.
Constraints
- 2≤N≤105
- 0≤ai≤109
- 0≤x≤109
Input
The input is given from Standard Input in the following format:
N x
a1 a2 … aN
Output
Print the minimum number of operations required to achieve the objective.
Sample Input 1
3 3
2 2 2
Sample Output 1
1
Eat one candy in the second box. Then, the number of candies in each box becomes (2,1,2).
Sample Input 2
6 1
1 6 1 2 0 4
Sample Output 2
11
For example, eat six candies in the second box, two in the fourth box, and three in the sixth box. Then, the number of candies in each box becomes (1,0,1,0,0,1).
Sample Input 3
5 9
3 1 4 1 5
Sample Output 3
0
The objective is already achieved without performing operations.
Sample Input 4
2 0
5 5
Sample Output 4
10
All the candies need to be eaten.
//一道简单的贪心,我想得太复杂了。。。而且结果要用 long long 保存
#include <stdio.h> int box[];
int main()
{
int n,x;
while (scanf("%d%d",&n,&x)!=EOF)
{
for (int i=;i<=n;i++)
scanf("%d",&box[i]);
long long all=;
if (box[]>x)
{
all+=box[]-x;
box[]=x;
}
for (int i=;i<=n;i++)
{
int k = box[i]+box[i-]-x;
if (k>)
{
all+=k;
box[i]-=k;
}
}
printf("%lld\n",all);
}
}
Boxes and Candies(贪心)的更多相关文章
- Boxes and Candies
问题 G: Boxes and Candies 时间限制: 1 Sec 内存限制: 128 MB[提交] [状态] 题目描述 There are N boxes arranged in a row. ...
- 2018.09.23 atcoder Boxes and Candies(贪心)
传送门 一道挺有意思的贪心. 从1到n依次满足条件. 注意要特判第一个数已经大于x的情况. 但是如何贪心吃呢? 如果靠左的数没有越界,我们吃靠右的数. 原因是下一次靠右的数就会成为靠左的数,相当于多贡 ...
- CF GukiZ hates Boxes 【二分+贪心】
Professor GukiZ is concerned about making his way to school, because massive piles of boxes are bloc ...
- BZOJ 4368: [IOI2015]boxes纪念品盒 贪心
题意:给定一个环,环上有一些点包裹,你要从 $0$ 号点出发,然后每次带上一个容量为 $k$ 的背包. 问:如果要把所有的包裹都带回 $0$ 好点最少要走多少距离. 每一次只有 $3$ 种走法:走整圆 ...
- atcoder题目合集(持续更新中)
Choosing Points 数学 Integers on a Tree 构造 Leftmost Ball 计数dp+组合数学 Painting Graphs with AtCoDeer tarja ...
- Codeforces Round #229 (Div. 2) C. Inna and Candy Boxes 树状数组s
C. Inna and Candy Boxes Inna loves sweets very much. She has n closed present boxes lines up in a ...
- Codeforces 488B - Candy Boxes
B. Candy Boxes 题目链接:http://codeforces.com/problemset/problem/488/B time limit per test 1 second memo ...
- Codeforces Round #278 (Div. 2) B. Candy Boxes [brute force+constructive algorithms]
哎,最近弱爆了,,,不过这题还是不错滴~~ 要考虑完整各种情况 8795058 2014-11-22 06:52:58 njczy2010 B - Ca ...
- Codeforces Round #229 (Div. 2) C
C. Inna and Candy Boxes time limit per test 1 second memory limit per test 256 megabytes input stand ...
随机推荐
- 10 种机器学习算法的要点(附 Python)(转载)
一.前言 谷歌董事长施密特曾说过:虽然谷歌的无人驾驶汽车和机器人受到了许多媒体关注,但是这家公司真正的未来在于机器学习,一种让计算机更聪明.更个性化的技术 也许我们生活在人类历史上最关键的时期:从使用 ...
- CentOS6.5安装ganglia3.6
参考来源: 1.http://yhz.me/blog/Install-Ganglia-On-CentOS.html 2.http://blog.csdn.net/sdlyjzh/article/det ...
- netty handle处理流程
server handlerAdded server channelRegistered server channelActive server read server channelInactive ...
- JAVA Eclipse 启动 Eclipse 弹出“Failed to load the JNI shared library jvm_dll”怎么办
原因1:给定目录下jvm.dll不存在. 对策:(1)重新安装jre或者jdk并配置好环境变量.(2)copy一个jvm.dll放在该目录下. 原因2:eclipse的版本与jre或者jdk版本不一致 ...
- 打造你的前端神器-webstorm11
说起前端编辑器,用过dw,sublime,hbuilder,webstorm也不陌生,之前的版本8有用过一下,但是觉得比sublime重量太多,但是随着后来用node的开始,发现需要打造个web前端神 ...
- STL学习笔记(数值算法)
运用数值算法之前必须先加入头文件<numeric> 加工运算后产生结果 1.对序列进行某种运算 T accumulate(InputIterator beg,InputIterator e ...
- 【Excle数据透视表】如何为一个字段添加多种分类汇总方式
解决方案1 右键单击人员分类字段包含的任意单元格→右键→字段设置→自定义→(最大值.最小值) 解决方案2 单击人员分类→分析→字段设置
- 操作log.py
# 把双数日期的日志,里面给随便写点东西# 1.获取到log目录下的所有文件os.walk()# 2.根据文件名来判断,是否是双数日期,分割字符串,取到日期# 3.12%2==0# 4.打开这个文件 ...
- flex-direction用法解释
语法: flex-direction:row | row-reverse | column | column-reverse 默认值:row 适用于:flex容器 继承性:无 动画性:否 计算值:指定 ...
- Mac 下Versions的 svn无法上传 .a 文件的问题
Xcode自带的svn和Versions以及一些其它工具都默认ignore".a"文件. 解决办法有两个: 方法一:使用命令行添加文件([转]原文在这) 1.打开终端,输入cd,空 ...