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 o...
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("%d\n",(sum+1)/2);
15     }
16     return 0;
17 }

 

 

目录
相关文章
|
7月前
|
机器学习/深度学习 算法
[第三章]数学与简单dp
[第三章]数学与简单dp
61 1
|
人工智能 决策智能
数学基础之博弈论
数学基础之博弈论
93 0
|
人工智能
[蓝桥杯] 数学与简单DP问题
蓝桥杯比赛中常见的还有一类数学问题,这些数学问题有的是有公式计算,有的是考察的思维逻辑。我们具体来看例题。
56 0
|
算法
Plant(快速幂+数学分析(没想到吧,数学无处不在))
Plant(快速幂+数学分析(没想到吧,数学无处不在))
68 0
|
算法
基础算法练习200题11、鸡兔同笼
基础算法练习200题11、鸡兔同笼
147 0
基础算法练习200题11、鸡兔同笼
|
机器学习/深度学习 Java
灰暗而空虚的景色β(数学思维题)
“雪啊。” “雪是红色的。” 像坏掉的复读机一样,梓川咲太只能把闪烁的思绪断断续续的说出来。
100 0
灰暗而空虚的景色β(数学思维题)
|
机器学习/深度学习 程序员
程序员的数学【概率论】(三)
本文其实值属于:程序员的数学【AIoT阶段二】 的一部分内容,本篇把这部分内容单独截取出来,方便大家的观看,本文介绍 概率论
154 0
程序员的数学【概率论】(三)
|
程序员
程序员的数学【概率论】(二)
本文其实值属于:程序员的数学【AIoT阶段二】 的一部分内容,本篇把这部分内容单独截取出来,方便大家的观看,本文介绍 概率论
243 0
程序员的数学【概率论】(二)