Codeforces 789A Anastasia and pebbles(数学,思维题)
A. Anastasia and pebbles
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output
Anastasia loves going for a walk in Central Uzhlyandian Park. But she became uninterested in simple walking, so she began to collect Uzhlyandian pebbles. At first, she decided to collect all the pebbles she could find in the park.
She has only two pockets. She can put at most k pebbles in each pocket at the same time. There are n different pebble types in the park, and there are wi pebbles of the i-th type. Anastasia is very responsible, so she never mixes pebbles of different types in same pocket. However, she can put different kinds of pebbles in different pockets at the same time. Unfortunately, she can't spend all her time collecting pebbles, so she can collect pebbles from the park only once a day.
Help her to find the minimum number of days needed to collect all the pebbles of Uzhlyandian Central Park, taking into consideration that Anastasia can't place pebbles of different types in same pocket.
Input
The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 109) — the number of different pebble types and number of pebbles Anastasia can place in one pocket.
The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 104) — number of pebbles of each type.
Output
The only line of output contains one integer — the minimum number of days Anastasia needs to collect all the pebbles.
Examples
Input
3 2
2 3 4
Output
3
Input
5 4
3 1 8 9 7
Output
5
Note
In the first sample case, Anastasia can collect all pebbles of the first type on the first day, of second type — on the second day, and of third type — on the third day.
Optimal sequence of actions in the second sample case:
- In the first day Anastasia collects 8 pebbles of the third type.
- In the second day she collects 8 pebbles of the fourth type.
- In the third day she collects 3 pebbles of the first type and 1 pebble of the fourth type.
- In the fourth day she collects 7 pebbles of the fifth type.
- In the fifth day she collects 1 pebble of the second type.
题目链接:http://codeforces.com/contest/789/problem/A
思路:开始用暴力直接搜,然后太复杂了,然后WA了,看了下别人的题解,发现好像有个这样的计算公式:
设每种石子的数量分别为x(用循环来做),每个口袋可以装的石子数为k
计算每种石子装满一个口袋需要的天数sum=(k+x-1)/k(容易看出它这样做是为了向下取整);
每次将天数进行累加,最终得到的天数sum=(sum+1)/2(同样是向上取整);
下面给出AC代码:
1 #include <bits/stdc++.h>
2 using namespace std;
3 int main()
4 {
5 int n,k,x;
6 while(scanf("%d%d",&n,&k)!=EOF)
7 {
8 int sum=0;
9 while(n--)
10 {
11 scanf("%d",&x);
12 sum+=(x+k-1)/k;
13 }
14 printf("%dn",(sum+1)/2);
15 }
16 return 0;
17 }
- datalist标签小结
- Java常用工具类之删除文件
- Java常用工具类之Excel导出
- js行内式遇到的一些问题 DOM对象和jq对象转换的问题
- JavaWeb(三)servlet
- PL/SQL 编程(三 )程序包和包体,触发器,视图,索引
- 一次关于js事件出发机制反常的解决记录
- Java综合题目
- Ubuntu 16.04下为TITAN 1080 显卡安装驱动及Gpu版TensorFlow|深度学习
- 给新生成的节点(动态生成节点)绑定事件方法总结
- JavaWeb(四)JDBC操作Oracle
- JavaWeb(六)Listener监听器
- JSP引入 - UEditor 富文本编辑器
- JavaWeb(五)Filter过滤器
- 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 数组属性和方法
- Android中menu使用详解
- SimpleCommand框架介绍以及简单使用(一)
- Android开发中使用achartengine绘制各种图表的方法
- Android开发中Listview动态加载数据的方法示例
- Android自定义实现顶部粘性下拉刷新效果
- Android开发使用自定义view实现ListView下拉的视差特效功能
- Android打造炫酷进度条效果
- Android开发实现自定义新闻加载页面功能实例
- Android下Activity间通信序列化过程中的深浅拷贝浅析
- Android升级支持库版本遇到的两个问题详解
- Android开发使用Drawable绘制圆角与圆形图案功能示例
- Android开发中自定义ProgressBar控件的方法示例
- Android 使用自定义RecyclerView控件实现Gallery效果
- Android开发中GridView用法示例
- React Native中Android物理back键按两次返回键即退出应用