题目:

  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. 加载jQuery库

    使用google <head> <script type="text/javascript" src="http://ajax.googleapis.c ...

  2. c# IP从192.168.1.1转成int类型

    找了一些资料,总结如下: 方法1 .net提供的方法转换IP地址 //字符串转换为数字 System.Net.IPAddress ipaddress = System.Net.IPAddress.Pa ...

  3. ZOJ 3955 Saddle Point 校赛 一道计数题

    ZOJ3955 题意是这样的 给定一个n*m的整数矩阵 n和m均小于1000 对这个矩阵删去任意行和列后剩余一个矩阵为M{x1,x2,,,,xm;y1,y2,,,,,yn}表示删除任意的M行N列 对于 ...

  4. 如何给自己的博客上添加个flash宠物插件

    最近在一些博主的博客上看到一些小宠物的挂件,很有趣,访客到了网站后可以耍耍小宠物,增加网站的趣味性,在功能强大的博客系统上看到有这样的小宠物挂件还是蛮有趣的. 多次差找资料后,终于在http://ww ...

  5. Line: 220 - com/opensymphony/xwork2/spring/SpringObjectFactory.java:220:-1

    转自:http://blog.51cto.com/alinazh/1276363 在启动tomcat的时候出现错误: Line: 220 - com/opensymphony/xwork2/sprin ...

  6. (分治)51NOD 1019 逆序数

    在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序.一个排列中逆序的总数就称为这个排列的逆序数.   如2 4 3 1中,2 1,4 3,4 1,3 1是 ...

  7. sourcetree跳过注册方法

    很人用git命令行不熟练,那么可以尝试使用sourcetree进行操作. 然鹅~~sourcetree又一个比较严肃的问题就是,很多人不会跳过注册或者操作注册. 废话不多,我们直接开始跳过注册阶段的操 ...

  8. 树形DP URAL 1039 Anniversary Party

    题目传送门 /* 题意:上司在,员工不在,反之不一定.每一个人有一个权值,问权值和最大多少. 树形DP:把上司和员工的关系看成根节点和子节点的关系,两者有状态转移方程: dp[rt][0] += ma ...

  9. CircuitBreaker design pattern---reference

    It's common for software systems to make remote calls to software running in different processes, pr ...

  10. H5活动的一些事

    ISUX团队镇楼:https://isux.tencent.com/nine-question-of-swipe-html5-page.html IE6.7.8支持html5新元素 : http:// ...