codeforces 789A(数学)
发布日期:2021-06-29 21:39:34 浏览次数:2 分类:技术文章

本文共 2862 字,大约阅读时间需要 9 分钟。

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 22 3 4
output
3
input
5 43 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.
  • 题意:有n堆种类各不相同的东西需要移动位置,现在一件衣服上面有两个口袋,每个口袋最多放k件东西,并且要求一个口袋里面的东西必须是种类相同的。问最少需要的移动次数是多少?
  • 思路:分为几种情况,数量小于等于k的,数量在大于k并且小于2*k之间的,和数量大于等于2*k的,为什么这样分,先说小于k的,是不是只需要一个口袋,然后大于k并且小于2*k是不是需要两个口袋,数量大于等于2*k的,就要看是不是能被2*k整除了,如果不能那么又会产生第一和第二种情况。
  • 代码:
  • #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1e5+5;
    typedef long long LL;
    int w[maxn];
    int main()
    {
        int n,k;
        while(scanf("%d%d",&n,&k)!=EOF)
        {
            for(int i=0;i<n;i++)
            {
                scanf("%d",&w[i]);
            }
            int num1=0,num2=0;
            int ans=0;
            int count1=0,count2=0;
            for(int i=0;i<n;i++)
            {
                int t=k*2;
               if(w[i]<=k)
               {
                   num1++;
                   continue;
               }
               if(w[i]>=t)
                {
                    count1=w[i]/t;
                    count2=w[i]%t;
                    ans+=count1;
                    if(count2<=k&&count2!=0) num1++;
                    if(count2>k&&count2<t) ans++;
                }
                if(w[i]>k&&w[i]<t)
                {
                    ans++;
                    continue;
                }
            }
           num2=(int)num1/2.0+0.5;
           ans+=num2;
           printf("%d\n",ans);
        }
        return 0;
    }

转载地址:https://dh-butterfly.blog.csdn.net/article/details/69568593 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:2017第八届蓝桥杯C/C++ B组省赛题解
下一篇:codeforces 789B(分类讨论)

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月30日 19时22分33秒