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 ...
随机推荐
- leetcode题解:Search for a Range (已排序数组范围查找)
题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...
- Java Enum的多态性
转载自:http://pf-miles.iteye.com/blog/187155 Enum+多态,我没说错,不过Enum是不可以被继承的,也不可以继承自别人,只是能实现接口而已,何谈多态?不过还是先 ...
- 通过脚本发送zabbix邮件报警
zabbix原生的报警媒介类型中,邮件报警是我们常用的方式.当我们在CentOS6上面安装zabbix3.0并配置邮件报警的时候,在邮件配置正确的前提下,不管触发器如何触发,邮件总是发送不出去,但是在 ...
- Linux学习之三-Linux系统的一些重要配置文件
Linux学习之三-Linux系统的一些重要配置文件 1.网卡配置文件 /etc/sysconfig/network-scripts/ifcfg-eth0 说明: DEVICE=eth0 ...
- Linux SSH和SFTP服务分离
Linux SSH和SFTP服务分离 学习了:https://www.cnblogs.com/zihanxing/articles/5665383.html 都是监听22端口:
- 转:阿里 Weex 思路与实战(web相关)
Weex——关于移动端动态性的思考.实现和未来 2016-04-05 勾股.伊耆 移动开发前线 本文由手机淘宝技术团队赵锦江(勾股).黄金涌(伊耆)等专家创作.手淘作为电商应用,对客户端/前端的动态性 ...
- IOS--苹果各地区开发者支援的电话号码
网页地址: https://developer.apple.com/contact/phone/ 中国区的咨询热线:4006 701 855 邓白氏申请中心的电话(400-6701855)
- IIS7 经典模式和集成模式的区别分析
IIS7.0中的Web应用程序有两种配置模式:经典模式和集成模式.两者区别大家可以参考下,根据实际情况选用. 经典模式是为了与之前的版本兼容,使用ISAPI扩展来调用ASP.NET运行库,原先运行于I ...
- automaticallyAdjustsScrollViewInsets(UITextView文字顶部留有空白)
iOS7新添加的UIViewController的属性automaticallyAdjustsScrollViewInsets 此属性默认为YES,这样UIViewController下如果只有一个U ...
- sql语句偶记录
查看表结构: show FULL COLUMNS FROM tablename;