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 ...
随机推荐
- iOS常用第三方库大全,史上最全第三方库收集
下拉刷新 EGOTableViewPullRefresh – 最早的下拉刷新控件. SVPullToRefresh – 下拉刷新控件. MJRefresh – 仅需一行代码就可以为UITableVie ...
- django使用类做业务逻辑
在django中一般定义一个带有request参数的函数用来处理url,但是更推荐用类做 从django.views.generic.base 导入的views有get,post等各种函数,用来处理对 ...
- 对Linux文件权限的理解
755,775,777,ugoa 等分别代表什么含义?这些数字是如何得到的? 1.常用的linux文件权限: 444 -r--r--r-- 600 -rw------- 644 -rw-r--r-- ...
- 【Hadoop】HDFS冗余数据块的自动删除
HDFS冗余数据块的自动删除 在日常维护hadoop集群的过程中发现这样一种情况: 某个节点由于网络故障或者DataNode进程死亡,被NameNode判定为死亡, HDFS马上自动开始数据块的容错拷 ...
- How to get the edited text from itext in fabricjs
https://stackoverflow.com/questions/39286826/how-to-get-the-edited-text-from-itext-in-fabricjs http: ...
- http header 具体解释
HTTP(HyperTextTransferProtocol)即超文本传输协议,眼下网页传输的的通用协议. HTTP协议採用了请求/响应模型,浏览器或其它client发出请求,server给与响应. ...
- 质量平台建设之Mock平台
转载:http://blog.csdn.net/spark2008/article/details/51372913 基于目前系统越来越多,在项目开发过程中,减少各模块之间的开发依赖,另保证各个系统模 ...
- m 调用传参图片切换
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- hbuilder - wap to app
官方文档: http://www.dcloud.io/wap2app.html 新建Wap2App,示例网址:www.baidu.com 随后,我们可以在 最后,我们可以 打包完成以后,下载即可
- JavaScript 转换小技巧
1.变量转换 看起来很简单,但据我所看到的,使用构造函数,像Array()或者Number()来进行变量转换是常用的做法.始终使用原始数据类型(有时也称为字面量)来转换变量,这种没有任何额外的影响的做 ...