Anastasia and pebbles (Codeforces-789A)

解决一个关于鹅卵石收集的问题,一个人每天经过公园收集不同类型的鹅卵石,并使用两个口袋来携带,每个口袋能装一定数量的同类型鹅卵石。任务是计算收集所有鹅卵石所需的最少天数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:

http://codeforces.com/problemset/problem/789/A

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 ≤ 1051 ≤ 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

题目大意:

一个人每天都要经过一个公园,公园里有鹅卵石,他每天都拿去鹅卵石(一共有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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值