zoukankan      html  css  js  c++  java
  • Ice Cream Tower

    2017-08-18 21:53:38

    writer:pprp

    题意如下:

    Problem D. Ice Cream Tower
    Input file: Standard Input
    Output file: Standard Ouptut
    Time limit: 6 seconds
    Mr. Panda likes ice cream very much especially the ice cream tower. An ice cream tower consists
    of K ice cream balls stacking up as a tower. In order to make the tower stable, the lower ice cream
    ball should be at least twice as large as the ball right above it. In other words, if the sizes of the
    ice cream balls from top to bottom are A0, A1, A2, · · · , AK−1, then A0 × 2 ≤ A1, A1 × 2 ≤ A2,
    etc.
    One day Mr. Panda was walking along the street and found a shop selling ice cream balls. There
    are N ice cream balls on sell and the sizes are B0, B1, B2, · · · , BN−1. Mr. Panda was wondering
    the maximal number of ice cream towers could be made by these balls.
    Input
    The first line of the input gives the number of test cases, T. T test cases follow. Each test
    case starts with a line consisting of 2 integers, N the number of ice cream balls in shop and K
    the number of balls needed to form an ice cream tower. The next line consists of N integers
    representing the size of ice cream balls in shop.
    Output
    For each test case, output one line containing “Case #x: y”, where x is the test case number
    (starting from 1) and y is the maximal number of ice cream towers could be made.
    Limits
    • 1 ≤ T ≤ 100.
    • 1 ≤ N ≤ 3 × 105
    .
    • 1 ≤ K ≤ 64.
    • 1 ≤ Bi ≤ 1018
    .
    Sample input and output
    Sample Input Sample Output
    3
    4 2
    1 2 3 4
    6 3
    1 1 2 2 4 4
    6 3
    1 1 2 2 3 4
    Case #1: 2
    Case #2: 2
    Case #3: 1
    Page 5 of 21

    题意:给你一个n代表有多少重量的冰激凌块,再给你要求的冰激凌塔的层数,冰激凌塔的规则是下一个必须等于或者大于上一层的二倍

    然后给你n个重量,问你最多可以完成需要的多少个冰激凌塔

    答案有二分性质,所以先进行二分,然后判断该答案是否符合要求,这部分用贪心的思想验证

    其中验证答案是否符合要求这部分不是很好实现,借鉴大佬的代码,研究了半天,一步一步手动推到,

    发现用的是双指针写的,的确可以使代码变得很简练

    /*
    prob: Ice Cream Tower
    writer:pprp
    date:2017/8/18
    declare:二分+贪心
    */
    
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    int N, K;
    const int maxn = 300005;
    typedef long long ll;
    ll a[maxn], b[maxn];
    int tmp;
    
    int judge(int m) //检验m个冰淇淋能否做出来
    {
        for(int i = 0; i < m; i++)//仅仅初始化前m个
            b[i] = a[i];
    
        //双指针移动
        int p = m; //p指针指向数组a
    
        for(int i = m; i < m * K; i++) //i 指针指向 数组b
        {
            while(a[p] < b[i - m] * 2 && p < N) //通过移动指针p,找到恰好大于二倍的点   
                p++;
            
            if(p == N)   //边界条件,如果指针p移动到数组a的末尾,那么证明没有找到相应的值,则出错
                return 0;
            
            b[i] = a[p]; //将数组a中满足的点都加入到数组b中
            
            p++; //指针p进行下一步移动
        }
        
        return 1;
    }
    
    int bisearch(int l, int r)
    {
        while(l < r)
        {
            int mid = (l + r + 1) >> 1;//如果是 l + r就会变成死循环
            if(judge(mid))//如果成功
            {
                l = mid;
            }
            else
            {
                r = mid - 1;
            }
        }
        return l;
    }
    
    int main()
    {
        int T;
        cin >> T;
        
        for(int j = 1 ; j <= T ; j++)
        {
            cin >> N >> K;
            memset(a,0,sizeof(a));
    
            for(int i = 0 ; i < N ; i++)
                cin >> a[i];
    
            sort(a, a + N);
    
            cout <<"Case #" << j << ": "<< bisearch(0, N/K) << endl;
        }
        return 0;
    }
  • 相关阅读:
    hdu 5119 Happy Matt Friends
    hdu 5128 The E-pang Palace
    hdu 5131 Song Jiang's rank list
    hdu 5135 Little Zu Chongzhi's Triangles
    hdu 5137 How Many Maos Does the Guanxi Worth
    hdu 5122 K.Bro Sorting
    Human Gene Functions
    Palindrome(最长公共子序列)
    A Simple problem
    Alignment ( 最长上升(下降)子序列 )
  • 原文地址:https://www.cnblogs.com/pprp/p/7392427.html
Copyright © 2011-2022 走看看