Boxes and 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
输入
N x
a1 a2 … aN
输出
样例输入
Copy
3 3
2 2 2
样例输出 Copy
1
提示
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
inline int read() {int x=,f=;char c=getchar();while(c!='-'&&(c<''||c>''))c=getchar();if(c=='-')f=-,c=getchar();while(c>=''&&c<='')x=x*+c-'',c=getchar();return f*x;}
typedef long long ll;
const int maxn=5e5+;
const int M=1e7+;
const int INF=0x3f3f3f3f;
ll n,x;
ll a[maxn];
void inint(){
cin>>n>>x;
for(int i=;i<n;i++){
cin>>a[i];
}
}
int main(){
inint();
ll sum=;
for(int i=;i<n;i++){
if(a[i]+a[i-]>x){
if(x-a[i-]>=){
sum+=a[i]-(x-a[i-]);
a[i]=(x-a[i-]);
}
else{
sum+=(a[i]+a[i-]-x);
a[i]=;
}
}
}
printf("%lld",sum);
return ;
}
Boxes and Candies的更多相关文章
- Boxes and Candies(贪心)
Boxes and Candies Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement Ther ...
- 2018.09.23 atcoder Boxes and Candies(贪心)
传送门 一道挺有意思的贪心. 从1到n依次满足条件. 注意要特判第一个数已经大于x的情况. 但是如何贪心吃呢? 如果靠左的数没有越界,我们吃靠右的数. 原因是下一次靠右的数就会成为靠左的数,相当于多贡 ...
- 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 ...
- Codeforces Round #278 (Div. 2)
题目链接:http://codeforces.com/contest/488 A. Giga Tower Giga Tower is the tallest and deepest building ...
- 大数开方 ACM-ICPC 2018 焦作赛区网络预赛 J. Participate in E-sports
Jessie and Justin want to participate in e-sports. E-sports contain many games, but they don't know ...
- atcoder题目合集(持续更新中)
Choosing Points 数学 Integers on a Tree 构造 Leftmost Ball 计数dp+组合数学 Painting Graphs with AtCoDeer tarja ...
随机推荐
- [CF3B] Lorry - 贪心
有一辆载重量为 v 的货车, 准备运送两种物品. 物品 A 的重量为 1, 物体 B 的重量为 2, 每个物品都有一个价值. 求货车可以运送的物品的最大价值. Solution 考虑把物品分为两类,枚 ...
- [P5490] 【模板】扫描线 - 线段树
求 \(n\) 个矩形的面积并 Solution 将矩形转化为 \(y_1\) 位置的 + 修改 和 \(y_2\) 位置的 - 修改.然后按照 \(+y\) 顺序依次处理所有的修改,到达的一个新的位 ...
- 修复ThinkPHP导出excel数字过大时显示为科学记数法
修复ThinkPHP导出excel数字过大时显示为科学记数法,这种显示对于查看的用户来说是及其不友好的.所以,我们要使其转化为正常的数字串! 我在google 的过程中,查了一些资料.其中 1).// ...
- cc.rectContainsPoint使用中发现的问题
cc.rectContainsPoint(rect, point);此中rect常用node.getBoundingBox()方法取得,而这个方法通过node的x,y和width,height算得.也 ...
- 学习 Rust cookbook 之算法篇(algorithm)
原文作者:suhanyujie 永久链接:https://github.com/suhanyujie/rust-cookbook-note 博客链接:https://ishenghuo.cnblogs ...
- 题解 【洛谷P1115】最大子段和
这是一道枚举经典题. 本题有三种做法,各位需要根据每个题的数据范围来决定自己用哪种方法. 本题解中统一设最大和为Max. 方法一. 枚举子序列,从起点到终点求和.时间复杂度:O(n^3) 我们可以枚举 ...
- HDU2020——绝对值排序(java实现,使用map)
Question Description Input Output Sample Input Sample Output 解题思路简述: 在接收每一个数组的过程中,将负数及其绝对值以键值对的形式存入m ...
- Learn from Niu 2020.1.13
观念: 1. 把可视化的东西拾起来, 毕竟是做那个出身的. 2. 可视化里面时序数据,时空数据一直都是非常重要的. 3. know your data永远是最重要的一步, 我想更好的方式是,数据驱动, ...
- python3练习100题——011
原题链接:http://www.runoob.com/python/python-exercise-example11.html 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔 ...
- web-css-文本
一.文本的水平对齐方式 使用text-align来设置文本的对齐方式:text-align的取值:left(向左对齐)/center(水平居中对齐)/right(向右对齐)/justify(两端对齐) ...