2018-2019 ICPC, NEERC, Southern Subregional Contest D. Garbage Disposal
D. Garbage Disposal
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 nn days Vasya knows aiai — number of units of garbage he will produce on the ii-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 kk 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 nn days. No garbage should be left after the nn-th day.
Input
The first line of the input contains two integers nn and kk (1≤n≤2⋅105,1≤k≤1091≤n≤2⋅105,1≤k≤109) — number of days to consider and bag's capacity. The second line contains nn space separated integers aiai (0≤ai≤1090≤ai≤109) — the number of units of garbage produced on the ii-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 nn-th day. In a day it is allowed to compose and drop multiple bags.
Examples
input
Copy
3 2
3 2 1
output
Copy
3
input
Copy
5 1
1000000000 1000000000 1000000000 1000000000 1000000000
output
Copy
5000000000
input
Copy
3 2
1 0 1
output
Copy
2
input
Copy
4 4
2 8 4 1
output
Copy
4
题意:倒垃圾,a[i]表示垃圾数目,必须每一个垃圾必须要用容量为k的垃圾袋装,i表示第i天,当天的垃圾最多只能留到第二天,求最少花费的垃圾袋数
思路:贪心,对于每一个垃圾袋而言,要尽量的装满,当天的垃圾能装满一个袋子就马上装好,剩下a[i]%k个垃圾可以第二天和第二天的垃圾一起装
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#include<unordered_set>
#define rg register ll
#define inf 2147483647
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define ll long long
#define maxn 300005
const double eps = 1e-6;
using namespace std;
inline ll read()
{
char ch = getchar(); ll s = 0, w = 1;
while (ch < 48 || ch>57) { if (ch == '-')w = -1; ch = getchar(); }
while (ch >= 48 && ch <= 57) { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
return s * w;
}
inline void write(ll x)
{
if (x < 0)putchar('-'), x = -x;
if (x > 9)write(x / 10);
putchar(x % 10 + 48);
}
ll n,k,a[maxn];
int main()
{
cin>>n>>k;
for(rg i=1;i<=n;i++)a[i]=read();
ll sum=0,temp=inf;
for(rg i=1;i<=n;i++)
{
if(temp<=k&&temp)
{
sum++;
a[i]-=k-temp;
if(a[i]<0)a[i]=0;
}
if(a[i]>=0)
{
sum+=a[i]/k;
temp=a[i]%k;
}
//cout<<temp<<" "<<sum<<" "<<a[i]<<endl;
}
cout<<sum+(temp!=0)<<endl;
return 0;
}
- 手工打造分布式爬虫
- (64) 常见文件类型处理: 属性文件/CSV/EXCEL/HTML/压缩文件 / 计算机程序的思维逻辑
- org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter与org.apache.struts2.dispatcher.
- Python协程演进过程
- Android TV开发简介
- (59) 文件和目录操作 / 计算机程序的思维逻辑
- Rdseed与SAC的安装
- Python爬虫大战京东商城
- (77) 异步任务执行服务 / 计算机程序的思维逻辑
- Unity Android Plugin开发指南
- Python还能做这个?真的好棒棒耶!
- java中Comparator的用法
- Python爬虫之豆瓣音乐及糗事百科
- oracle、mysql 分页查询比较
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 从源码角度看 PHP 字符串类型转换
- 静态链表
- 中流砥柱java的动态代理
- java的反射机制到底是做什么的?
- Java中是否直接可以使用enum进行传输
- PHP 恶意程序简单分析
- springboot之相关环境设置
- springboot之第一个springboot程序
- 「查缺补漏」巩固你的RocketMQ知识体系
- springboot之场景启动器
- ICLR2020 | 深度自适应Transformer
- springboot之自动配置
- golang--连接redis数据库并进行增删查改
- golang--redis连接池
- springboot配置之使用application.properties时编码问题