问题 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

  1. 3 3
  2. 2 2 2

样例输出 Copy

  1. 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变成负数
  1. #pragma GCC optimize(2)
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. 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;}
  5. typedef long long ll;
  6. const int maxn=5e5+;
  7. const int M=1e7+;
  8. const int INF=0x3f3f3f3f;
  9. ll n,x;
  10. ll a[maxn];
  11. void inint(){
  12. cin>>n>>x;
  13. for(int i=;i<n;i++){
  14. cin>>a[i];
  15. }
  16. }
  17. int main(){
  18. inint();
  19. ll sum=;
  20. for(int i=;i<n;i++){
  21. if(a[i]+a[i-]>x){
  22. if(x-a[i-]>=){
  23. sum+=a[i]-(x-a[i-]);
  24. a[i]=(x-a[i-]);
  25. }
  26. else{
  27. sum+=(a[i]+a[i-]-x);
  28. a[i]=;
  29. }
  30. }
  31. }
  32. printf("%lld",sum);
  33. return ;
  34. }

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. 题解【AcWing91】最短Hamilton路径

    题面 看到数据范围这么小,第一眼想到爆搜. 然而这样做的复杂度是 \(\mathcal{O}(n! \times n)\) 的,明显会 TLE. 于是考虑状压 DP. 我们设 \(dp_{i,j}\) ...

  2. RN开发-Android原生交互

    在使用RN开发过程中,难免有些原生功能需要要自己来实现,下面总结一下在使用RN与原生开发交互. 1.在原生代码中定义实现类 1.1  首先继承 ReactContextBaseJaveModule抽象 ...

  3. flaskapp

    前置知识 https://blog.csdn.net/u013457794/article/details/88997699?depth_1-utm_source=distribute.pc_rele ...

  4. 浅析网页meta标签中X-UA-Compatible属性的使用

    今天有一个做开发的朋友突然问你知道很多网站上面加入的X-UA-Compatible属性的意义么?其实这个在以前还专门花了一点时间来验证我自己的想法,结果也确实如自己所预想的那样,八九不离十,当然有一点 ...

  5. logistic回归损失函数(非常重要,深入理解)

    2.2 logistic回归损失函数(非常重要,深入理解) 上一节当中,为了能够训练logistic回归模型的参数w和b,需要定义一个成本函数 使用logistic回归训练的成本函数 为了让模型通过学 ...

  6. python接口自动化之fiddler使用(二)

    1.快捷设置,自定义会话框,查看get和post请求 (1)鼠标放在#后面,右键 (2)选择Customize columns (3)选择Miscellaneous (4)选择RequestMetho ...

  7. 微信小程序 selectComponent 值为null

    这个东西的执行时间感觉有点迷, 我遇到的情况是在page 的onReady  onShow 当中 使用 selectComponent 无法获取到子组件的对象 只好退而求其次  在需要触发的方法当中 ...

  8. [CF]Round514

    A Cashier 题意:一个人,一天的工作时长为\(L\),从第\(t_i\)时刻开始有长度为\(l_i\)的工作,他想在工作间隙抽烟,抽一根要\(a\)分钟,问能抽几根. 直接模拟. B Forg ...

  9. LoadRunner使用记录

    基本术语 性能测试--通过自动化的测试工具模拟多种正常.峰值以及异常负载条件来对系统的各项性能指标进行测试. 负载测试和压力测试都属于性能测试,两者可以结合进行. 负载测试,确定在各种工作负载下系统的 ...

  10. CentOS 7防火墙快速开放端口配置方法

    CentOS升级到7之后,发现无法使用iptables控制Linuxs的端口,baidu之后发现Centos 7使用firewalld代替了原来的iptables.下面记录如何使用firewalld开 ...