zoukankan      html  css  js  c++  java
  • Problem D. Ice Cream Tower(2016 China-Final)

    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. 

    果然二分法很重要啊

    首先答案肯定在n/k ~ 0之间,通过二分确定答案,使用贪心验证当前是否可行

    #include <cstdio>
    #include <cmath>
    #include <cctype>
    #include <iostream>
    #include <cstring>
    #include <algorithm>
    #include <string>
    #include <stack>
    #include <vector>
    #include <map>
    #include <set>
    using namespace std;
    typedef long long LL;
    
    LL n,k,T;
    LL a[300005],b[300005];
    
    bool judge(int m){
      for (int i=0;i<m;i++){
        a[i] = b[i];
      }
      int p = m;
      for (int i=m;i<m*k;i++){
        while (b[p] < a[i-m] * 2 && p < n) p ++;
        if (p == n) return false;
        a[i] = b[p];
        p ++;
      }
    
      return true;
    }
    
    LL divide(LL l,LL r){
      while (l < r){
        LL mid = (l + r + 1) / 2;
        if (judge(mid)) l = mid;
        else r = mid - 1;
      }
      return l;
    }
    
    int main() {
      // freopen("test.in","r",stdin);
      ios::sync_with_stdio(false);
      cin >> T;
      for (int i=1;i<=T;i++){
        cin >> n >> k;
        for (int j=0;j<n;j++){
          cin >> b[j];
        }
        sort(b,b+n);
        cout << "Case #" << i << ": " << divide(0,n/k) << endl;
      }
    
      return 0;
    }
    View Code
  • 相关阅读:
    OpenCV运动检测跟踪(blob track)框架组成模块详解
    C# 控制台应用程序中输出彩色字体
    C#获取隐藏的文件
    用c#怎么比较两张图片的不同
    清除浏览器缓存
    C#判断系统是否已经连接上网络
    如何删除VS2005中显示的最近项目
    设计模式:模板方法模式(Template Method)
    【SQL】数据库对象中查找某一关键字
    【问答】.NET面试题
  • 原文地址:https://www.cnblogs.com/ToTOrz/p/7270368.html
Copyright © 2011-2022 走看看