zoukankan      html  css  js  c++  java
  • 二分+查找 2016 ecfinal D

    题目: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

    3

    4 2

    1 2 3 4

    6 3

    1 1 2 2 4 4

    6 3

    1 1 2 2 3 4

    Sample Output

    Case #1: 2

    Case #2: 2

    Case #3: 1


    分析:二分找结果,贪心验证;本来以为是贪心但是不能证明正确;假如可以组成mid个冰激凌,则根据贪心思想,这mid个冰激凌的第一个球肯定是a数组排序过后的前mid个球,然后往下找就可以了。

     1 #include <cstdio>
     2 #include <cstring> 
     3 #include <algorithm>
     4 #include <cmath>
     5 using namespace std;
     6 #define maxn 300010
     7 
     8 int t,n,k;
     9 long long a[maxn];
    10 long long temp[maxn];
    11 
    12 int judge(int mid)
    13 {
    14     //printf("mm%d
    ",mid);
    15     int j=mid;
    16     for(int i = 0; i < mid;i++)
    17         temp[i] = a[i]; 
    18     for(int x = 2 ;x <= k;x++)
    19     {
    20         for(int i = 0; i < mid;i++)
    21         {
    22             while(temp[i]*2 > a[j] && j < n)j++;
    23             if(j >= n) return 0;
    24             temp[i] = a[j];
    25             //printf("%d %d %d
    ",i,j,temp[i]);
    26             j++;
    27         }
    28         
    29     }
    30     return 1;
    31 }
    32 
    33 int solve(int l,int r)
    34 {
    35     int mid;
    36     while(l < r)
    37     {
    38         mid = (l+r+1)/2;
    39         if(judge(mid))
    40         {
    41             l = mid;
    42         }else{
    43             r = mid-1; 
    44         }
    45     }
    46     return l;
    47 }
    48 
    49 int main()
    50 {
    51     scanf("%d",&t);
    52     int to = 1;
    53     while(t--)
    54     {
    55         memset(a,0,sizeof a);
    56         scanf("%d%d",&n,&k);
    57         for(int i = 0; i< n; i++)
    58         {
    59             scanf("%lld",&a[i]); 
    60         } 
    61         sort(a,a+n);
    62         printf("Case #%d: %d
    ",to++,solve(0,n/k));
    63     }
    64 }
  • 相关阅读:
    九.Protobuf3特殊类型
    八.Protobuf3更新消息类型(添加新的字段)
    七.Protobuf3 嵌套类型
    六.Protobuf3引入其他.proto文件
    五.Protobuf3 枚举
    四.Protobuf3 缺省值
    VC 在调用main函数之前的操作
    Windows下的代码注入
    C 堆内存管理
    VC++ 崩溃处理以及打印调用堆栈
  • 原文地址:https://www.cnblogs.com/inerbornthisway/p/7941379.html
Copyright © 2011-2022 走看看