题目:

  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
*/

最新文章

  1. java spring 邮件发送
  2. jython安装
  3. YOURPHP的分页完整版
  4. 【java基础】内存分析
  5. Java面向对象概述
  6. DBCC TRACEON/TRACEOFF/TRACESTATUS
  7. 程序员书单_J2EE专题
  8. [jobdu]树中两个结点的最低公共祖先
  9. windows下pycharm远程调试pyspark
  10. 解决Nuget:https://api.nuget.org/v3/index.json 访问不了的问题
  11. jQuery对象与js对象互相转换
  12. SQLserver查询库中包含某个字段的表
  13. .NET 内存分配笔记
  14. python汉诺塔问题的递归理解
  15. js中函数对象创建的总结
  16. sublime-代码提示
  17. 如何用html把文本框外观格式设为只显示底部的横线
  18. Mongodb集群搭建之 Sharding+ Replica Sets集群架构
  19. 010.MySQL-Keepalived搭配脚本04
  20. linux numastat的理解

热门文章

  1. ubuntu tftp【转】
  2. 在MAC端查看win7
  3. Ural 1382 2SAT
  4. JEECG框架使用Tomcat启动报ClassNotFound
  5. springboot(二)整合mybatis,多数据源和事务管理
  6. Ubuntu服务器WDCP可视化界面搭建注意
  7. mvn 配置
  8. [Usaco2011 Jan]道路和航线
  9. ACM_数数有多少(第二类Stirling数-递推dp)
  10. 279 Perfect Squares 完美平方数