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 ...
随机推荐
- java8的ConcurrentHashMap为何放弃分段锁,为什么要使用CAS+Synchronized取代Segment+ReentrantLock
原文地址:https://cloud.tencent.com/developer/article/1509556 推荐一篇 ConcurrentHashMap 和 HashMap 写的比较的的文章 j ...
- Vue ui创建项目
vue-cli 3.0 版本为我们提供了集 创建.管理.分析 为一体的可视化界面vue UI,一个可视化项目管理器 一.打开终端,安装最新vue-cli npm install -g @vue/cli ...
- java序列化与反序列化的使用
个人博客 地址:http://www.wenhaofan.com/article/20180925214701 1.什么是序列化和反序列化 Serialization(序列化)是一种将对象以一连串的字 ...
- spring security和java web token整合
思路: spring security 1.用户输入用户名密码. 2.验证:从库中(可以是内存.数据库等)查询该用户的密码.角色,验证用户名和密码是否正确.如果正确,则将填充Authenticatio ...
- 数组的concat连接
let arr1 = [1,3,5],arr2 = [2,32,78],arr3 = [];arr3 = arr1.concat(arr2);// arr1 = [1,3,5] arr2 = [2,3 ...
- AcWing 487. 金明的预算方案
#include <cstring> #include <iostream> #include <algorithm> #include <vector> ...
- 记录 shell学习过程(2) read的用法
echo -n "login:"read username #read后面直接使用一个变量用于接收输入的数据 echo -n "password:"read ...
- C++-随机数的产生
一.随机数 以前学C语言的时候感觉随机数没啥用的,现在想想是自己无知啦,在帮人做一个项目的时候发现随机数还是相当有用的,我们可以利用随机数来生成大量的测试数据. 有两种方法可以让你的程序每次运行结果不 ...
- python3练习100题——027
又是一道迭代的题,没做好. 看了答案才试着写出来. 我一定要加油啊,为了尽快摆脱现在讨厌的生活! 原题链接:http://www.runoob.com/python/python-exercise-e ...
- JQ DOM元素 创建 添加 删除
创建元素 // 创建元素节点 $('<p></p>'); // 创建属性节点 $('<p class="wow"></p>'); / ...