gym101194 china final Problem D. Ice Cream Tower(二分)

这是一个关于ACM-ICPC亚洲中国决赛的问题,题目要求利用二分法找到最大的冰淇淋塔数量。给定一定数量的冰淇淋球和构建每个塔所需的球数,需要确保下层球的大小至少是上层球的两倍。通过对球的大小进行二分查找和贪心策略,可以确定最多可以构建的冰淇淋塔的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

Problem D. Ice Cream Tower

 Input file:
Output file:
 Time limit:
Standard Input
Standard Ouptut
6 seconds

The 2016 ACM-ICPC Asia China-Final Contest

page6image3496

Mr. Panda likes ice cream very much especially the ice cream tower. An ice cream tower consistsof K ice cream balls stacking up as a tower. In order to make the tower stable, the lower ice creamball should be at least twice as large as the ball right above it. In other words, if the sizes of theice cream balls from top to bottom are A0, A1, A2, ···, AK1, 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. Thereare N ice cream balls on sell and the sizes are B0, B1, B2, ···, BN1. Mr. Panda was wonderingthe 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 testcase starts with a line consisting of 2 integers, N the number of ice cream balls in shop and Kthe number of balls needed to form an ice cream tower. The next line consists of N integersrepresenting 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.

Limits

1T 100.
1 N 3 × 105.1 K 64.
1Bi 1018.

Sample input and output

Sample Input

Sample Output

3
42
1234
6311224463112234

Case #1: 2
Case #2: 2
Case #3: 1


题意:

给出n个冰淇淋球,做一个冰淇淋需要k个冰淇淋球,并且规定对于上下相邻的两个球,下面的球的质量大于等于其上面的那个球质量的两倍。

后面一行n个数,给出n个球的质量,问最多能做出多少个冰淇淋?


题解:

这题可以二分,假设能做x个冰淇淋,那么先安排最上面那个球,贪心的思想,肯定取最小的x个,然后后面的类似取,线性扫一遍n,判断能否取x个。


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int maxn=3e5+100;
ll a[maxn],b[maxn];
int n,k;
bool check(int x)
{
    if(!x) return true;
    int j=x+1;
    rep(i,1,x+1)
    {
        b[i]=a[i];
    }
    int i;
    for(i=x+1;i<=n;)
    {
        while(a[i]<b[j-x]*2&&i<=n) i++;
        if(i>n) break;
        b[j++]=a[i];
        if((j-1)/x>=k) return true;
        i++;
    }
    if((j-1)/x>=k) return true;
    else return false;
}
int main()
{
    int cas;
    scanf("%d",&cas);
    rep(kk,1,cas+1)
    {
        scanf("%d%d",&n,&k);
        rep(i,1,n+1) scanf("%lld",&a[i]);
        if(n<k)
        {
            printf("Case #%d: 0\n",kk);
            continue;
        }
        int l=0,r=n/k;
        int ans=0;
        sort(a+1,a+n+1);
        while(l<=r)
        {
            int m=(l+r)/2;
            if(check(m)) ans=m,l=m+1;
            else r=m-1;
        }
        printf("Case #%d: %d\n",kk,ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值