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 ...
随机推荐
- java gc log
java full gc 经常带来延迟, 导致性能问题 如下命令使java虚拟机记录gc的log到文件, 帮助分析定位问题. java -Xloggc:./a.log -jar a.jar // ...
- ASP.NET MVC学习---(七)Razor视图引擎语法
之前体验过razor视图引擎@符号的威力了吧~ 连我这个初学者都能感觉到确实省时省力方便快捷 简直就是居家旅行*人*货必备之物啊(这里和谐两个字~) 那么现在就开始对razor的语法进一步介绍~ 1. ...
- android中Invalidate和postInvalidate的差别
Android中实现view的更新有两组方法,一组是invalidate.还有一组是postInvalidate.当中前者是在UI线 程自身中使用,而后者在非UI线程中使用. Android提供了In ...
- ionic准备之angular基础——模板引入(7)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- useradd(总结)
useradd,一条简单的语句,会引起六个文件的变化 举例一: useradd sc 1.可以看到在最后一行,多了一个用户.cat /etc/passwd [有一个字段为X,代表还没有密码] 2.密码 ...
- zabbix客户端安装shadowscoks客户端监控访问google网站
配置zabbix客户端配置文件 vim /etc/zabbix/zabbix_agentd.conf 添加 Include=/etc/zabbix/zabbix_agentd.d/ 添加脚本探测访问 ...
- winform对话框拖拽显示文件路径的问题
allow drop=true; dragEnter dragDrop vs管理员账户拖拽会失败
- ASP.NET CORE RAZOR :将新字段添加到 Razor 页面
https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/razor-pages/new-field 在本部分中,将使用 Entity Framew ...
- 征信报告页面的input验证收集
https://ipcrs.pbccrc.org.cn/ function checkLoginName() { var loginName = $.trim($("#loginname&q ...
- idea实时编译代码
实时编译就不解释了,出现错误的代码在命令行能给你提示出来.看图操作吧: