2016 China-Final Ice Cream Tower(二分)

原题链接

题意:有n个冰淇凌球,要做k层的冰淇凌且下层的至少是上一层的两倍大,问最多能做多少个。

思路:二分能做成的个数,加入当前二分值为m,则这m个冰激凌的第一层一定选前m个冰欺凌球,然后一层层地找,直到不能找到满足值或者冰激凌都达到k层为止。对每一个冰激凌二分找第一个大于等于上一层二倍的值,找到后标记位置并记录当前值(方便寻找下一层),再从标记位置起对下一个冰激凌进行同样的操作。


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
#define mem(a,b) memset(a,b,sizeof(a))
const int maxn = 1e6+5;
const int ff = 0x3f3f3f3f;
const double esp = 1e-7;

int n,k;
ll b[maxn];
ll p[maxn];

bool solve(int x,int l,int r,int cnt)
{
	if(cnt == 0)
		return 1;
	
	int i;
	for(i = 1;i<= x;i++)
	{
		if(l> r)
			break;
		int tmp = lower_bound(b+l,b+r,2*p[i])-b;//此处很迷,注意lower的用法
		if(b[tmp]>= 2*p[i])
			p[i] = b[tmp];
		else
			break;
		l = tmp+1;//边界更新
	}
	if(i<= x)
		return 0;
	if(solve(x,l,r,cnt-1))
		return 1;
	return 0;
}

int main()
{
	int t;
	cin>>t;
	int tt = 0;
	while(t--)
	{
		scanf("%d %d",&n,&k);
		for(int i = 1;i<= n;i++)
			scanf("%lld",&b[i]);
		sort(b+1,b+n+1);
		int l = 1,r = n;
		int mid,ans = 0;
		while(l<= r)
		{
			mid = (l+r)>>1;
			for(int i = 1;i<= mid;i++)
				p[i] = b[i];
			if(solve(mid,mid+1,n,k-1))
				l = mid+1,ans = mid;
			else
				r = mid-1;
		}
		printf("Case #%d: %d\n",++tt,ans);
	}
	return 0;
}

Ice Cream Tower

Input file: Standard Input

Output file: Standard Ouptut

Time limit: 6 seconds

Mr. Panda likes ice cream very muchespecially the ice cream tower. An ice cream tower consists

of K ice cream balls stacking up as atower. In order to make the tower stable, the lower ice cream

ball should be at least twice as large asthe 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 thestreet and found a shop selling ice cream balls. There

are N ice cream balls on sell and the sizesare B0, B1, B2, · · · , BN−1. Mr. Panda was wondering

the maximal number of ice cream towerscould be made by these balls.

Input

The first line of the input gives thenumber of test cases, T. T test cases follow. Each test

case starts with a line consisting of 2integers, N the number of ice cream balls in shop and K

the number of balls needed to form an icecream tower. The next line consists of N integers

representing the size of ice cream balls inshop.

Output

For each test case, output one linecontaining “Case #x: y”, where x is the test case number

(starting from 1) and y is the maximalnumber 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

 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值