zoukankan      html  css  js  c++  java
  • Codeforces Round #407 (Div. 2) A. 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.

     题目大意:一个人去公园捡石头,他有两个口袋,每个口袋最多能装k个石头。总共有n种石头,每种石头有wi个,如果这个人每天去公园捡一次石头,最少多少天捡完?

     题目解析:枚举第i种石头需要几天才能拿完,有可能需要“半天”,最后不要忘了把这个半天加上

    #include <bits/stdc++.h>
    
    using namespace std;
    
    int main()
    {
        int n, k, left = 0, cnt = 0,  s;
        scanf("%d%d", &n, &k);
        s = 2*k;
        for(int i = 0, w; i < n; i++){
            scanf("%d", &w);
             if(left) {
                cnt++;
                left = 0;
                w -= k;
                if(w <= 0) continue;
            }
            cnt += w/s;
            left = w%s;
            if(left == 0) continue;
            if(k < left && left < s) {
                left = 0;
                cnt++;
            }else left = 1;
        }
        printf("%d
    ", cnt+left);
        return 0;
    }
    View Code
  • 相关阅读:
    java回调函数这样说,应该明确了吧!
    Aamazon Web Service EC2 Ubuntu 新建用户而且用ssh连接host
    Html animation by css(Sequence Js Tutorial)
    shell脚本一键安装mysql5.7.x
    HDU 4544 湫湫系列故事――消灭兔子
    简明python教程 --C++程序员的视角(八):标准库
    简明python教程 --C++程序员的视角(七):异常
    简明python教程 --C++程序员的视角(六):输入输出IO
    简明python教程 --C++程序员的视角(五):面向对象的编程
    简明python教程 --C++程序员的视角(四):容器类型(字符串、元组、列表、字典)和参考
  • 原文地址:https://www.cnblogs.com/cshg/p/6647349.html
Copyright © 2011-2022 走看看