问题 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. Codeforces 383C Propagating tree, 线段树, 黑白染色思想

    按深度染色,奇深度的点存反权值. #include <bits/stdc++.h> using namespace std; vector <]; ],a[],s[],vis[],i ...

  2. java多线程CountDownLatch

    先上一个介绍:https://blog.csdn.net/shihuacai/article/details/8856370 用视频https://www.bilibili.com/video/av8 ...

  3. 开发过程中遇到的代理Proxy配置问题

    proxy代理问题 在公司不能访问外网的时候,使用IDEA开发.. 需要配置IDEA Proxy Maven Git IDEA开发工具Proxy配置,使用Spring Boot快读构建工具 Maven ...

  4. RN开发-Navigator

    1.在入口组件render方法中返回<Navigator>            let defaultName = 'Welcome';            let defaultCo ...

  5. 2级迁移类Q201-Oracle RAC 到单机RMAN迁移(同字节序)非公

    项目文档引子系列是根据项目原型,制作的测试实验文档,目的是为了提升项目过程中的实际动手能力,打造精品文档AskScuti. 项目文档引子系列目前不对外发布,仅作为博客记录.如学员在实际工作过程中需提前 ...

  6. JUC-LOCK接口

    Synchronized 1.多线程编程模版上 (1)线程 操作 资源类 (2)高内聚低耦合 2.实现步骤 (1)创建资源类 (2)资源类里创建同步方法,同步代码块 3.例子:卖票 LOCK 接口 锁 ...

  7. 在docker上部署centos

    1.查找镜像源$ docker search centosNAME DESCRIPTION STARS OFFICIALcentos The official build of CentOS. 385 ...

  8. day02_1hibernate

    对象状态与一级缓存 一.对象缓存状态的介绍: ①在使用hibernate时对象的三种状态:(代码如下) 瞬时状态 :没有与session关联,没有主键OID标识(主键的OID指的是对象id,在配置文件 ...

  9. Manacher算法求最长回文串模板

    #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> ...

  10. JMeter压力测试,http请求压测,5分钟让你学会如何压测接口!

    JMeter压力测试 官网:https://jmeter.apache.org 最新款的jmeter需要java8的支持,所以请自行安装jdk8.这里就不啰嗦了. 可以根据自己的系统下载zip或者是t ...