Garbage Disposal

Description

Enough is enough. Too many times it happened that Vasya forgot to dispose of garbage and his apartment stank afterwards. Now he wants to create a garbage disposal plan and stick to it.

For each of next n

days Vasya knows ai — number of units of garbage he will produce on the i-th day. Each unit of garbage must be disposed of either on the day it was produced or on the next day. Vasya disposes of garbage by putting it inside a bag and dropping the bag into a garbage container. Each bag can contain up to k

units of garbage. It is allowed to compose and drop multiple bags into a garbage container in a single day.

Being economical, Vasya wants to use as few bags as possible. You are to compute the minimum number of bags Vasya needs to dispose of all of his garbage for the given n

days. No garbage should be left after the n

-th day.

Input

The first line of the input contains two integers n

and k (1≤n≤2⋅105,1≤k≤109) — number of days to consider and bag's capacity. The second line contains n space separated integers ai (0≤ai≤109) — the number of units of garbage produced on the i

-th day.

Output

Output a single integer — the minimum number of bags Vasya needs to dispose of all garbage. Each unit of garbage should be disposed on the day it was produced or on the next day. No garbage can be left after the n

-th day. In a day it is allowed to compose and drop multiple bags.

Sample Input

Input
3 2
3 2 1
Output
3
Input
5 1
1000000000 1000000000 1000000000 1000000000 1000000000
Output
5000000000
Input
3 2
1 0 1
Output
2
Input
4 4
2 8 4 1
Output
4
题意:
有n天,第i天有a[i]个垃圾,每天的垃圾最多能留到第二天扔,每个垃圾口袋最多装k个垃圾,问,最少用多少个垃圾口袋能把所有的垃圾装完。
思路:
把每天的垃圾数分两种,一种是刚好能用垃圾口袋装下的;一种是还剩余的垃圾,这个时候,把剩余的垃圾留到第二天去处理,将第二天的垃圾数减掉(k-剩余的),让第一天剩余的垃圾和第二天里的垃圾凑成一个垃圾口袋
如果,第二天的垃圾数减去第一天需要的后小于0,这个时候就让第二天的垃圾数等于0。然后输出垃圾口袋数就好
注意ans要开long long
 1 #include<algorithm>
2 #include<cstdio>
3 #include<iostream>
4 using namespace std;
5 int main(){
6 int n,k;
7 while(~scanf("%d %d",&n,&k)) {
8 int a[200005];
9 for(int i=1;i<=n;i++)
10 scanf("%d",&a[i]);
11 long long ans=0,t=0;
12 for(int i=1;i<=n;i++) {
13 ans+=a[i]/k;//当天刚好能装袋的垃圾
14 t=a[i]%k;//剩余没能当天装袋的垃圾
15 if(t){//如果剩余的垃圾>0
16
17 a[i+1]-=k-t;//将第二天的垃圾数减去第一天剩余装袋的垃圾所需要的垃圾
18 if(a[i+1]<0)//如果相见之后垃圾数<0
19 a[i+1]=0;//让垃圾数=0
20 ans++;//垃圾口袋数++
21 }
22 }
23 printf("%lld\n",ans);
24 }
25 return 0;
26 }

Garbage Disposal(模拟垃圾装垃圾口袋)的更多相关文章

  1. 【JVM从小白学成大佬】4.Java虚拟机何谓垃圾及垃圾回收算法

    在Java中内存是由虚拟机自动管理的,虚拟机在内存中划出一片区域,作为满足程序内存分配请求的空间.内存的创建仍然是由程序猿来显示指定的,但是对象的释放却对程序猿是透明的.就是解放了程序猿手动回收内存的 ...

  2. 【java】对象变成垃圾被垃圾回收器gc收回前执行的操作:Object类的protected void finalize() throws Throwable

    package 对象被回收前执行的操作; class A{ @Override protected void finalize() throws Throwable { System.out.prin ...

  3. java什么时候进行垃圾回收,垃圾回收的执行流程

    java的垃圾回收分为 三个区域新生代 老年代 永久代 一个对象实例化时 先去看伊甸园有没有足够的空间如果有 不进行垃圾回收 ,对象直接在伊甸园存储.如果伊甸园内存已满,会进行一次minor gc然后 ...

  4. 2018-2019 ICPC, NEERC, Southern Subregional Contest

    目录 2018-2019 ICPC, NEERC, Southern Subregional Contest (Codeforces 1070) A.Find a Number(BFS) C.Clou ...

  5. 【python进阶】Garbage collection垃圾回收2

    前言 在上一篇文章[python进阶]Garbage collection垃圾回收1,我们讲述了Garbage collection(GC垃圾回收),画说Ruby与Python垃圾回收,Python中 ...

  6. PHP-----浅谈垃圾回收机制

    前言 大多数编程语言都会有自身的垃圾回收机制,php也不例外.经常听很多人说gc,也就是垃圾回收器,全程为Garbage Collection. 在php5.3之前,是不包括垃圾回收机制的,也没有专门 ...

  7. 深入探究JVM之垃圾回收器

    @ 目录 前言 正文 一.垃圾收集算法 标记-复制 标记-清除 标记-整理 分代回收 二.常用的垃圾回收器 Serial/SerialOld ParNew Parallel Scavenge/Para ...

  8. java虚拟机入门(四)-垃圾回收的故事

    谈到垃圾回收器,java程序员骄傲了起来,c语言你是够快,但是你有管家帮你打扫吗,还不是得靠自己的一双手,有钱就是任性.既然如此令java程序员骄傲的垃圾回收器,怎能让人不想去一探究竟呢! 垃圾回收器 ...

  9. CLR via C#深解笔记七 - 自动内存管理(垃圾回收)

    每个应用程序都要使用这样或者那样的资源,比如文件.内存缓冲区.屏幕空间.网络连接.数据库资源等.事实上,在面向对象的环境中,每个类型都代表可供程序使用的一种资源. 要使用这些资源,必须为代表资源的类型 ...

随机推荐

  1. php英语单词大全95

    abstract抽象的 -挨伯丝拽克特 access存取.访问 -挨克色丝 account账户 -厄靠恩特 action动作 -爱克身 activate激活 -爱克特维特 active活动的 -爱克得 ...

  2. 同时开始了SQL。。。

    SQL LIMIT OFFSET 和 LIMIT code1: SELECT id, name, score FROM table ORDER BY score DESC LIMIT OFFSET 4 ...

  3. 网络ping不通原因总结

    1.检查主机是否在线,硬件是否连接好 2.查看ip地址是否输入正确 3.查看是否防火墙拦截,如果有拦截关闭防火墙 4.两台电脑是否接入同一局域网(WiFi也是)

  4. vim粘贴缩进问题

    vim不支持直接从其他应用复制内容粘贴过来,而是模拟用户键盘输入来实现的,一般设置vim在换行时自动以上一行的的缩进为初始位置,这样就会导致复制过来的内容出现缩进错乱. set paste 解决粘贴乱 ...

  5. 监控端口是否开放,端口未开放关闭虚拟ip,端口开放启动虚拟IP

    #!/bin/bash#该脚本监控本机的一个端口,当端口异常时,停止lvs的本地ip直到恢复.该脚本依托于lvs.sh启动脚本#目前只支持监控1个vip #定义常用变量#配置检查的ip以及端口chec ...

  6. 1.1.25 word图片批量对齐

    1.打开文件,点击[开始]>[编辑]>[替换](或ctrl+h)> \ 在[查找内容]输入^g>定位到[替换为]>[格式]>[段落]>[对齐方式|居中]> ...

  7. java工程师-面试知识点总结

    目录(转载) [x] 一.Java基础(语言.集合框架.OOP.设计模式等) [x] 二.Java高级(JavaEE.框架.服务器.工具等) [x] 三.多线程和并发 [x] 四.Java虚拟机 [x ...

  8. ios-微信支付登录分享-notification通知

    // //  AppDelegate.m //  NewAppBase // //  Created by ENERGY on 2018/5/17. //  Copyright © 2018年 ENE ...

  9. nginx配置http强制跳转https

    nginx配置http强制跳转https 网站添加了https证书后,当http方式访问网站时就会报404错误,所以需要做http到https的强制跳转设置. 一.采用nginx的rewrite方法 ...

  10. git初始化本地项目及关联github远程库

    一.初始化本地项目 idea中在项目文件夹下执行:git init . 二.在github官网上创建一个库 三.执行如下命令关联远程库: git remote add origin 你创建的git远程 ...