题目链接:
http://codeforces.com/problemset/problem/789/A
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.
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.
The only line of output contains one integer — the minimum number of days Anastasia needs to collect all the pebbles.
3 2 2 3 4
3
5 4 3 1 8 9 7
5
题目大意:
一个人每天都要经过一个公园,公园里有鹅卵石,他每天都拿去鹅卵石(一共有n种),但是他只有两个口袋,每个口袋最多能装k个同杨种类的鹅卵石,问最少几天能够把鹅卵石全部装完带走。注意:每个口袋只能装相同种类的鹅卵石。就按照第一组测试数据说吧:输入n和k(分别代表鹅卵石的种类数,每个口袋最多能够装的个数),下面输入n个整数(每个整数表示该种鹅卵石的数目)。
解题思路:
如果按两个口袋算的话比较麻烦。可以假设只有一个口袋,然后算出来的结果如果是偶数,那么对应两只口袋的结果就是ans/2,如果是奇数,那么对用两只口袋的结果是ans/2+1。
代码:
#include<iostream>
using namespace std;
int main()
{
int n,k;
while(cin>>n>>k)
{
int ans=0,s;
for(int i=0;i<=n-1;i++)
{
cin>>s;
if(s%k==0) //能整除的话,说明这类鹅卵石刚好能够装s/k次
ans=ans+s/k;
else
ans=ans+s/k+1; //不能够够整除的话,这种鹅卵石得多装一次(虽然最后一次口袋没有装满)
}
if(ans%2==0) //对应两只口袋结果是ans/2
cout<<ans/2<<endl;
else //对应两只口袋结果是ans/2+1
cout<<ans/2+1<<endl;
}
return 0;
}