问题 G: Boxes and Candies

时间限制: 1 Sec  内存限制: 128 MB
[提交] [状态]

题目描述

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

输入

The input is given from Standard Input in the following format:
N x
a1 a2 … aN

输出

Print the minimum number of operations required to achieve the objective.

样例输入
Copy

3 3
2 2 2

样例输出 Copy

1

提示

Eat one candy in the second box. Then, the number of candies in each box becomes (2,1,2).
问题大意:给定序列,可以一次改变一个值,使得任意相邻的两个之和小于等与k;
问题解析:先两个两个的改变,一定先改变两者后面的那个,因为后面的那个对后面的有贡献(比如说a1,a2,a3,先遍历a1和a2,判断是否符合要求,如果不符合要求,要先减少a2,因为a2还对a1有贡献),注意不能让a2变成负数
#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的更多相关文章

  1. Boxes and Candies(贪心)

    Boxes and Candies Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement Ther ...

  2. 2018.09.23 atcoder Boxes and Candies(贪心)

    传送门 一道挺有意思的贪心. 从1到n依次满足条件. 注意要特判第一个数已经大于x的情况. 但是如何贪心吃呢? 如果靠左的数没有越界,我们吃靠右的数. 原因是下一次靠右的数就会成为靠左的数,相当于多贡 ...

  3. 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 ...

  4. Codeforces 488B - Candy Boxes

    B. Candy Boxes 题目链接:http://codeforces.com/problemset/problem/488/B time limit per test 1 second memo ...

  5. Codeforces Round #278 (Div. 2) B. Candy Boxes [brute force+constructive algorithms]

    哎,最近弱爆了,,,不过这题还是不错滴~~ 要考虑完整各种情况 8795058                 2014-11-22 06:52:58     njczy2010     B - Ca ...

  6. 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 ...

  7. Codeforces Round #278 (Div. 2)

    题目链接:http://codeforces.com/contest/488 A. Giga Tower Giga Tower is the tallest and deepest building ...

  8. 大数开方 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 ...

  9. atcoder题目合集(持续更新中)

    Choosing Points 数学 Integers on a Tree 构造 Leftmost Ball 计数dp+组合数学 Painting Graphs with AtCoDeer tarja ...

随机推荐

  1. 改善深层神经网络(三)超参数调试、Batch正则化和程序框架

    1.超参数调试: (1)超参数寻找策略: 对于所有超参数遍历求最优参数不可取,因为超参数的个数可能很多,可选的数据过于庞大. 由于最优参数周围的参数也可能比较好,所以可取的方法是:在一定的尺度范围内随 ...

  2. Wannafly Camp 2020 Day 7H 游戏 - 欧拉筛,GCD

    忘记特判 \(1\) ,血了一地 听说 \(O(n^2 \log n)\) 能过? #include <bits/stdc++.h> #define int long long using ...

  3. PP: GRU-ODE-Bayes: Continuous modeling of sporadically-observed time series

    From: KU Leuven; ESAT-STADIUS比利时鲁汶大学 ?? How to model real-world multidimensional time series? especi ...

  4. 解决SourceTree每次拉取提交都需要输入密码的问题

    打开终端并且输入: git config --global credential.helper osxkeychain 第一次需要输入密码,以后都不需要了

  5. window.resizeTo

    概述 动态调整窗口的大小. 语法 window.resizeTo(aWidth, aHeight) 参数 aWidth 是一个整数,表示新的 outerWidth(单位:像素)(包括滚动条.窗口边框等 ...

  6. 2018护网杯easy_tornado(SSTI tornado render模板注入)

    考点:SSTI注入 原理: tornado render是python中的一个渲染函数,也就是一种模板,通过调用的参数不同,生成不同的网页,如果用户对render内容可控,不仅可以注入XSS代码,而且 ...

  7. jQuery实现隔行变色、悬停变色 ( CSS3伪类选择器:nth-child() )

    <title>实现隔行变色</title> <script src="Js/jquery-1.8.0.min.js" type="text/ ...

  8. SIFT算法原理(2)-极值点的精确定位

    在SIFT解析(一)建立高斯金字塔中,我们得到了高斯差分金字塔: 检测DOG尺度空间极值点 SIFT关键点是由DOG空间的局部极值点组成的.以中心点进行3X3X3的相邻点比较,检测其是否是图像域和尺度 ...

  9. pyodbc 向excel中读写数据

    import pyodbc conn_info=( 'DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};''DBQ=[Sh ...

  10. dict 字典合并

    实例 1 : 使用 update() 方法,第二个参数合并第一个参数 def Merge(dict1, dict2): return(dict2.update(dict1))   实例 2 : 使用 ...