您的位置:首页 > 其它

Disk Storage

2015-11-15 21:20 381 查看

描述



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:

Every disk is placed horizontally. Its axis must coincide with the axis of storage.

Every disk is either place on the bottom surface or on another disk.

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hiho