Fox Ciel has n boxes in her room. They have the same size and weight, but they might have different strength. The i-th box can hold at most xi boxes on its top (we'll call xi the strength of the box).
Since all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For example, imagine Ciel has three boxes: the first has strength 2, the second has strength 1 and the third has strength 1. She cannot put the second and the third box simultaneously directly on the top of the first one. But she can put the second box directly on the top of the first one, and then the third box directly on the top of the second one. We will call such a construction of boxes a pile.
![](https://i-blog.csdnimg.cn/blog_migrate/adeb0718bdbce666678cc6a313d45083.png)
Fox Ciel wants to construct piles from all the boxes. Each pile will contain some boxes from top to bottom, and there cannot be more than xi boxes on the top of i-th box. What is the minimal number of piles she needs to construct?
The first line contains an integer n (1 ≤ n ≤ 100). The next line contains n integers x1, x2, ..., xn (0 ≤ xi ≤ 100).
Output a single integer — the minimal possible number of piles.
3 0 0 10
2
5 0 1 2 3 4
1
4 0 0 0 0
4
9 0 1 0 2 0 1 1 2 10
3
In example 1, one optimal way is to build 2 piles: the first pile contains boxes 1 and 3 (from top to bottom), the second pile contains only box 2.
![](https://i-blog.csdnimg.cn/blog_migrate/9c0da56eab673148604e374e89278920.png)
In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom).
![](https://i-blog.csdnimg.cn/blog_migrate/eced48c37199c4e50013b96b3e683802.png)
假如有相同大小的,我们仍先选择v,v-1,v-2...1。当其不连续的时候,若此序列只有n个,那我们必定可以插入v-n个箱子进去,显然我们要插入v-n个较大的。当其连续时,可以证明这样仍然是最优的,因为如果把其中v,v-1...v',v'-1...1换成v,v-1...v',v'...1的话,个数不能增加,但是剩下的选择的空间变小了。
经过一轮放置后,若没有放完,则继续按照这样的方式放置。
这道题显然用堆实现最方面,但是当时没有想到。
#include <cstdio>
#include <functional>
#include <algorithm>
using std::sort;
using std::greater;
int ptr[110];
int num[110];
int start[110];
int min(int a,int b,int c)
{
if (a < b && a < c)
return a;
if (b < c)
return b;
return c;
}
int main()
{
int n;
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
scanf("%d",num+i);
}
sort(num+1,num+1+n,greater<int>());
num[0] = 0x3f3f3f3f;
num[n+1] = 0x3f3f3f3f;
for (int i=1;i<=n;i++)
{
if (num[i]!=num[i-1])
{
ptr[++ptr[0]] = i;
start[ptr[0]] = i;
}
}
start[ptr[0]+1] = n+1;
int grp = 0;
int used= 0;
while (1)
{
if (used == n)
break;
int left = -0x3f3f3f3f;
grp ++;
for (int i=1;i<=ptr[0];i++)
{
if (ptr[i] == -1)
continue;
if (left == -0x3f3f3f3f)
left = num[ptr[i]];
else
left --;
used ++;
if (num[ptr[i]]==num[ptr[i]+1])
ptr[i] ++;
else
ptr[i] = -1;
}
if (left <= 0)
continue;
for (int i=ptr[0];left>0 && i>=1;i--)
{
if (ptr[i] == -1)
continue;
int chose = min(num[ptr[i]],start[i+1]-ptr[i],left);
left -= chose;
used += chose;
if (num[ptr[i]]==num[ptr[i]+chose])
ptr[i] += chose;
else
ptr[i] = -1;
}
}
printf("%d",grp);
return 0;
}