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
题意:每天都会产生x单位垃圾,垃圾袋容量为y,每天的垃圾最多留到第二天,给你1到n每天垃圾产量,问最少要多少个垃圾袋
思路:每天都装x/y袋垃圾,剩余x%y留到下一天,如果前一天有垃圾且两天的垃圾加起来小于等于y,那么也要装一袋
#include<stdio.h>
#define ll long long
int main()
{
ll n,k;
scanf("%lld %lld",&n,&k);
ll yu=0;
ll ans=0;
for(ll i=1; i<=n; i++)
{
ll temp;
scanf("%lld",&temp);
if(yu==0)
{
yu=temp%k;
ans=ans+temp/k;
}
else{
if(yu+temp<=k){
ans++;
yu=0;
}
else{
ans=ans+(temp+yu)/k;
yu=(temp+yu)%k;
}
}
}
if(yu!=0)ans++;
printf("%lld\n",ans);
}
上一篇: 高频Linux命令小结(新手向)
下一篇: 还没意识到我是车主
推荐阅读
-
全面了解JavaScirpt 的垃圾(garbage collection)回收机制
-
[JVM 相关] Java 新型垃圾回收器(Garbage First,G1)
-
JAVA垃圾回收GARBAGE COLLECTION(二、垃圾收集器)
-
[JVM 相关] Java 新型垃圾回收器(Garbage First,G1)
-
netty使用中的LEAK: ByteBuf.release() was not called before it‘s garbage-collected
-
python面试题之简要描述Python的垃圾回收机制(garbage collection)
-
全面了解JavaScirpt 的垃圾(garbage collection)回收机制
-
浅谈PHP5中垃圾回收算法(Garbage Collection)的演化
-
使用Chrome开发者工具分析JavaScript garbage collector(垃圾回收器) SAPC4CCloudCDS viewCloudFoundry
-
使用Chrome开发者工具分析JavaScript garbage collector(垃圾回收器)的实现原理 chromeSAPSAP云平台SAP Cloud PlatformSAP成都研究院