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 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 二分&贪心)的更多相关文章
- 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 ...
- 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 ...
- 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) ...
- 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 ...
- 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 ( ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- ios33--线程通信
// // ViewController.m // 06-掌握-线程间通信 // // atomic:为set方法加锁,线程安全,需要消耗资源. // nonatomic:不为set方法加锁,非线程安 ...
- 关于hive
这两天在研究了hbase,hadoop,hive,spark 由于spark.py不支持clust(jar才支持,但是太麻烦了>_<) 所以最终决定使用hive 在hive中用create ...
- [翻译]NUnit---Condition Asserts && Comparisons Asserts && Type Asserts (五)
网址:http://www.cnblogs.com/kim01/archive/2013/03/31/2991597.html Condition Asserts 测试指定条件的方法称作条件测试,测试 ...
- luogu 1121 环状最大两段子段和
题目大意: 一个序列看做一个环 选两段数使它们和最大 思路: 定义一个dp数组i j 0/1 表示前i个取了连续的j段 0/1表示取不取第i个 但是因为看做一个环 首尾相接的情况可以看做是选三段,其中 ...
- python 两个文件夹里的文件名对比
比如需要一个xml对应一个jpg时,有时候会不小心少了其中一个文件,这时可以用以下代码比较缺少的是哪个文件: # -*- coding: utf-8 -*- import os path1 = r'. ...
- cisco secure ACS服务器搭建
网上下载Cisco Secure Access Control System 5.2.iso的镜像,总共有两部分 http://pan.baidu.com/disk/home#path=%252FCi ...
- bzoj 1637: [Usaco2007 Mar]Balanced Lineup【瞎搞】
我是怎么想出来的-- 把种族为0的都变成-1,按位置x排升序之后,s[i]表示种族前缀和,想要取(l,r)的话就要\( s[r]-s[l-1]==0 s[r]==s[l-1] \),用一个map存每个 ...
- web自动化测试—selenium游览器多窗口操作
# coding=utf-8'''web游览器多窗口操作: 获取当前窗口句柄 current_window_handle 获取所有窗口句柄 window_handles 切换窗口 switch_to_ ...
- Sql 主键设置
1.开发数据库时常用命名规范 1>.使用不包含数字的单数表名,如果必要的话可以增加模块名前缀. 2>.对于主键的命名,使用表名+ID的形式. 3>.作为外键的列的名字应该与它们所对应 ...
- 数学/思维 UVA 11300 Spreading the Wealth
题目传送门 /* 假设x1为1号给n号的金币数(逆时针),下面类似 a[1] - x1 + x2 = m(平均数) 得x2 = x1 + m - a[1] = x1 - c1; //规定c1 = a[ ...