题目:

  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.

题意:现在给出N个冰激凌球的尺寸和k个球才能摞起一个冰激凌,为冰激凌的稳定性,要求下边的球的尺寸必须至少是上边球尺寸的两倍大。问最多能摞几个冰激凌。

思路:先对给出的尺寸从小到大排一下序,然后从0到n/k进行二分最多能摞x个,二分的答案用贪心来进行检验x能不能得出。

贪心:要想得到当前答案下的最优解,那么排序后的最前边的x个一定是作为冰激凌的顶层球的,而摞成x个冰激凌需要k*x个球,所以循环x*k次看看能不能每次都能找出来,如果每次都能找出来,x就成立,反之不成立。

ps:二分搜索的写法好多门道啊,还得深入学习啊!!

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 3e5+;
ll a[maxn],b[maxn];
int n,k; bool judge(int x)
{
for(int i = ; i<x; i++)
b[i] = a[i];
int p = x;
for(int i = x; i<x*k; i++)//总共需要x*k个球,循环这些次,看每次是不是都能找到
{
while(a[p]<b[i-x]* && p<n)p++;
if(p==n)return false;//没有凑齐x*k个
b[i] = a[p];
p++;
}
return true;
} int main()
{
int T,cnt=;
scanf("%d",&T);
while(T--)
{
memset(a,,sizeof(a));
scanf("%d%d",&n,&k);
for(int i = ; i<n; i++)
scanf("%lld",&a[i]);
int l = , r = n/k;
sort(a,a+n);
while(l < r)
{
int mid = (l+r+)/;
if(judge(mid))
l = mid;
else
r = mid-;
}
printf("Case #%d: %d\n",cnt++,l);
}
return ;
}
/*
样例输入:
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
*/

Ice Cream Tower(The 2016 ACM-ICPC Asia China-Final Contest 二分&贪心)的更多相关文章

  1. 2016 ACM/ICPC Asia Regional Shenyang Online 1003/HDU 5894 数学/组合数/逆元

    hannnnah_j’s Biological Test Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K ...

  2. 2016 ACM/ICPC Asia Regional Qingdao Online 1001/HDU5878 打表二分

    I Count Two Three Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  3. 2016 ACM/ICPC Asia Regional Shenyang Online 1009/HDU 5900 区间dp

    QSC and Master Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  4. 2016 ACM/ICPC Asia Regional Shenyang Online 1007/HDU 5898 数位dp

    odd-even number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  5. 2016 ACM/ICPC Asia Regional Dalian Online 1002/HDU 5869

    Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  6. 2016 ACM/ICPC Asia Regional Dalian Online 1006 /HDU 5873

    Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  7. HDU 5874 Friends and Enemies 【构造】 (2016 ACM/ICPC Asia Regional Dalian Online)

    Friends and Enemies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  8. HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)

    Barricade Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  9. HDU 5875 Function 【倍增】 (2016 ACM/ICPC Asia Regional Dalian Online)

    Function Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  10. HDU 5873 Football Games 【模拟】 (2016 ACM/ICPC Asia Regional Dalian Online)

    Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

随机推荐

  1. 怎么查询数据库中第30到40条记录呢? 通过ID,查询当前第30-40条记录 注意,ID不是顺序的

    http://blog.csdn.net/lee576/article/details/5812347 http://bbs.csdn.net/topics/190070614 http://www. ...

  2. dpdpdpdp~~~!!!

    dpdpdpdpdpdp D你妹个P!  妈的劳资就不信征服不了你!!哼!!

  3. Python Tricks(十九)—— switch 的实现

    python 原生语法不支持 switch,体现了 Python 大道至简的设计思路,有时为了避免啰嗦的 if elif等判断语句,我们可以用字典来代替 switch 的各分支,也即建立表达式和操作的 ...

  4. 洛谷 P1236 算24点

    题目描述 几十年前全世界就流行一种数字游戏,至今仍有人乐此不疲.在中国我们把这种游戏称为"算24点".您作为游戏者将得到4个1~9之间的自然数作为操作数,而您的任务是对这4个操作数 ...

  5. MySQL5.7 windows二进制安装

    200 ? "200px" : this.width)!important;} --> 介绍 1.下载解压 下载地址:http://dev.mysql.com/get/Dow ...

  6. PCB .NET Reactor 加密工具(NecroBit加密技术)

    在PCB行业中,我们使用的软件或脚本绝大多数都用非编译型语言写程序,而从一开始选择这种编程语言,就注定了程序的源码有被曝露风险.我们PCB工程系统用.NET语言编写,C#编译后会被转换为IL代码的中间 ...

  7. git 删除本地仓库

    更新: 2017/06/27 修改格式,备注mac下的命令没测试过   windows: rm .git/ mac: sudo rm -rf .git/ 没验证

  8. eccharts-gl 3D立体柱状图

    echarts-gl继承于echarts echarts-gl官方实例https://echarts.baidu.com/examples/index.html#chart-type-globe 代码 ...

  9. ACM_输出格式

    输出格式 Time Limit: 2000/1000ms (Java/Others) Problem Description: 某水比参加了XX杯,但是他太水,所以三等都木有,所以他决定出一道水题水一 ...

  10. kafka启动时出现FATAL Fatal error during KafkaServer startup. Prepare to shutdown (kafka.server.KafkaServer) java.io.IOException: Permission denied错误解决办法(图文详解)

    首先,说明,我kafk的server.properties是 kafka的server.properties配置文件参考示范(图文详解)(多种方式) 问题详情 然后,我启动时,出现如下 [hadoop ...