Disk Storage

描述

这里写图片描述
Little Hi and Little Ho have a disk storage. The storage’s shape is a truncated cone of height H. R+H is radius of top circle and R is radius of base circle.
Little Ho buys N disks today. Every disk is a cylinder of height 1. Little Ho wants to put these disk into the storage under below constraints:

  1. Every disk is placed horizontally. Its axis must coincide with the axis of storage.
  2. Every disk is either place on the bottom surface or on another disk.
  3. Between two neighboring disks in the storage, the upper one’s radius minus the lower one’s radius must be less than or equal to M.

Little Ho wants to know how many disks he can put in the storage at most.

题目分析

由于容器是个倒梯形的,因此摆放时小的放下面,大的放上面。
因此我们考虑将所有的disk按升序排序。
排序后我们发现,disk会因为第三个条件分成多段,不同段是不能放在一起的,比如在M= 1时
1, 2, 3, 5, 6, 7, 9, 10,
就被分成了三段:1, 2, 3# 5, 6, 7# 9, 10 各段内部满足条件3,因此可以从各段中选择一段放置在容器内。
对于小于R的盘,可以按照倒序的方式扣在已经放好的盘上,因此还要加上小于R且没有被使用的盘。
代码:
下面代码的思路是,先在R所在段出能够放置的disk数,然后加上小于R的disk数。

#include <cstdio>
#include <algorithm>
using namespace std;
enum {maxn = 100000+5};
int a[maxn];

int main()
{
    int n, m, h, r;
    scanf("%d %d %d %d", &n, &m, &h, &r);
    for (int i=0; i<n; i++)
        scanf("%d", a+i);
    sort(a, a+n);
    if (a[0] > r)
    {
        printf("0\n");
        return 0;
    }
    int br1 = 0;
    int i;
    for(i=1; i<n && a[i]<=r; i++)
    {
        if (a[i]- a[i-1] > m)
            br1 = i;
    }
    int push = i - br1 -1;
    for (int j= 1; i<n ; i++, j++)
    {
        if (a[i]- a[i-1] > m)
            break;
        if (a[i] > r+j+push)
            break;
    }
    printf("%d\n", min(i, h));
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值