2017-08-18 21:53:38

writer:pprp

题意如下:

Problem D. Ice Cream Tower
Input file: Standard Input
Output file: Standard Ouptut
Time limit: seconds
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−, then A0 × ≤ A1, A1 × ≤ 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−. 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 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 ) 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 Sample Output
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
Page 5 of 21

题意:给你一个n代表有多少重量的冰激凌块,再给你要求的冰激凌塔的层数,冰激凌塔的规则是下一个必须等于或者大于上一层的二倍

然后给你n个重量,问你最多可以完成需要的多少个冰激凌塔

答案有二分性质,所以先进行二分,然后判断该答案是否符合要求,这部分用贪心的思想验证

其中验证答案是否符合要求这部分不是很好实现,借鉴大佬的代码,研究了半天,一步一步手动推到,

发现用的是双指针写的,的确可以使代码变得很简练

/*
prob: Ice Cream Tower
writer:pprp
date:2017/8/18
declare:二分+贪心
*/ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
int N, K;
const int maxn = ;
typedef long long ll;
ll a[maxn], b[maxn];
int tmp; int judge(int m) //检验m个冰淇淋能否做出来
{
for(int i = ; i < m; i++)//仅仅初始化前m个
b[i] = a[i]; //双指针移动
int p = m; //p指针指向数组a for(int i = m; i < m * K; i++) //i 指针指向 数组b
{
while(a[p] < b[i - m] * && p < N) //通过移动指针p,找到恰好大于二倍的点
p++; if(p == N) //边界条件,如果指针p移动到数组a的末尾,那么证明没有找到相应的值,则出错
return ; b[i] = a[p]; //将数组a中满足的点都加入到数组b中 p++; //指针p进行下一步移动
} return ;
} int bisearch(int l, int r)
{
while(l < r)
{
int mid = (l + r + ) >> ;//如果是 l + r就会变成死循环
if(judge(mid))//如果成功
{
l = mid;
}
else
{
r = mid - ;
}
}
return l;
} int main()
{
int T;
cin >> T; for(int j = ; j <= T ; j++)
{
cin >> N >> K;
memset(a,,sizeof(a)); for(int i = ; i < N ; i++)
cin >> a[i]; sort(a, a + N); cout <<"Case #" << j << ": "<< bisearch(, N/K) << endl;
}
return ;
}

Ice Cream Tower的更多相关文章

  1. 2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 二分+贪心

    /** 题目:2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 链接:http://codeforces.com/gym/101194 题意:给n个木块,堆 ...

  2. Ice Cream Tower(The 2016 ACM-ICPC Asia China-Final Contest 二分&贪心)

    题目: Mr. Panda likes ice cream very much especially the ice cream tower. An ice cream tower consists ...

  3. Gym 101194D Ice Cream Tower

    被一道数位DP折磨得欲仙欲死之后,再做这道题真是如同吃了ice cream一样舒畅啊 #include<bits/stdc++.h> using namespace std; #defin ...

  4. The 2016 ACM-ICPC Asia China-Final D. Ice Cream Tower 二分 + 贪心

    题目大意: 对于给出的n个冰激凌球的大小,满足下面的球的大小是上一个的至少2倍,对于给出的k(由k的冰激凌球才能算作一个冰激凌塔),问n个冰激凌球可以最多堆出多少个高度为k的冰激凌塔 题目分析: 对于 ...

  5. Gym 101194D / UVALive 7900 - Ice Cream Tower - [二分+贪心][2016 EC-Final Problem D]

    题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...

  6. Gym 101194E / UVALive 7901 - Ice Cream Tower - [数学+long double][2016 EC-Final Problem E]

    题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...

  7. Ice Cream Tower Gym - 101194D (贪心 + 二分 )

    题目链接 : https://cn.vjudge.net/problem/Gym-101194D 题目大意 : 给你n个冰激凌球,让你用这些冰激凌球去垒冰激凌,要求是下面的这一个必须是他上面一个的两倍 ...

  8. Problem D. Ice Cream Tower

    题解:二分加贪心,,,二分答案,然后进行判断,判断的时候,首先给每一组配一个最大的球,然后在向每一组里面填球,注意填球的时候要按组进行,每一组先填一个,然后更新每一组内的最小值,方便下一次寻找. #i ...

  9. HackerRank Ice Cream Parlor

    传送门 Ice Cream Parlor Authored by dheeraj on Mar 21 2013 Problem Statement Sunny and Johnny together ...

随机推荐

  1. 安装canal

    一.安装前准备 配置Mysql: [mysqld] log-bin=mysql-bin #添加这一行就ok binlog-format=ROW #选择row模式 server_id=1 #配置mysq ...

  2. 19.Delete Documents-官方文档摘录

    1 插入例子 db.inventory.insertMany( [ { item: "journal", qty: 25, size: { h: 14, w: 21, uom: & ...

  3. JSON 序列化与反序列化(二)使用TypeReference 构建类型安全的异构容器

    1. 泛型通常用于集合,如Set和Map等.这样的用法也就限制了每个容器只能有固定数目的类型参数,一般来说,这也确实是我们想要的. 然而有的时候我们需要更多的灵活性,如数据库可以用任意多的Column ...

  4. (0.2.2)如何下载mysql数据库(二进制、RPM、源码、YUM源)

    目录 1.基于Linux平台的Mysql项目场景介绍 2.mysql数据库运行环境准备-最优配置 3.如何下载mysql数据库 3.1. 二进制文件包 3.2.RPM文件 3.3.源码包 3.4.yu ...

  5. 使用CocoaPods管理第三方类库[效率]

    项目文件夹   加入第三方框架后的项目文件夹例如以下图 为什么要用Cocoapods?   iOS开发中经常使用的第三方库,比方: 1.FMDB:在使用SQLite是仅仅须要加入libsqlite3. ...

  6. 汇智课堂 Node.js相关课程

    Node.js入门 Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台, 用来方便地搭建快速的 易于扩展的网络应用· Node.js 借助事件驱动, 非阻塞I/O 模型 ...

  7. Redis vs Mongo vs mysql

    Redis 和 Mongo 都属于 No-SQL类型的数据库,他们的区别,联系是什么呢?看了一些文章,特总结如下. Redis 最大的特点是,快!为什么快,因为他将大量的东西存储在了memory中.但 ...

  8. Spring框架第一篇之Spring的第一个程序

    一.下载Spring的jar包 通过http://repo.spring.io/release/org/springframework/spring/地址下载最新的Spring的zip包,当然,如果你 ...

  9. Python 函数的使用小结

    函数的好处:提高代码复用性,简化代码,代码可扩展. 函数只有调用的时候才会被执行. 1.参数: 形参&实参:位置参数,属于必填参数:默认值参数,为非必填参数,没有传值时使用默认值:关键字参数: ...

  10. Django restframwork实现自定义数据格式的分页与搜索

    最近因为在做分页时遇到的问题很多,页浪费了好多时间,所以记录一下.以后如遇到可用省去不必要的麻烦 restframwork中的官方文档对分页和搜索页进行了详细的介绍,但是我公司需要的return的js ...