CodeForces 388A Fox and Box Accumulation(贪心)

Description
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.

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?

Input
The first line contains an integer n (1 ≤ n ≤ 100). The next line contains n integers x1, x2, …, xn (0 ≤ xi ≤ 100).

Output
Output a single integer — the minimal possible number of piles.

Sample Input
Input
3
0 0 10
Output
2
Input
5
0 1 2 3 4
Output
1
Input
4
0 0 0 0
Output
4
Input
9
0 1 0 2 0 1 1 2 10
Output
3

题意:
已知各箱子载重能力,即最多可以在这个箱子上面放几个箱子,求这些箱子最少需要垒成几落/堆。

分析:
当对箱子排序后,该题最优解来自于局部最优解,故应该用贪心的思路来看。
首先,箱子应该从上垒起,尽可能的把最小的放置于最上层;
因此,将箱子按载重能力进行排序后,对箱子载重量进行遍历:
1.0载重能力的箱子放最上方,而后用count数组记录每堆箱子已垒数目;
2.若箱子载重量大于堆箱子数目,则将该箱子置于该堆下方,如果不满足则找下一堆,若均不满足,则开辟新的一堆,直至该载重能力箱子用完为止;
3.重复2,直至所有载重能力箱子用完为止。

具体操作,详见代码注释:

/*
*Author : Flint_x 
*Created Time : 2015-02-26 12:36:43 
*File name : c.cpp 
*/

#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

int x[1000];
int main(){
    int n;
    int count[1000];
    int num[1000];
    while(cin >> n){
        int maxnum = 1;
        memset(num,0,sizeof(num));
//      memset(x,0,sizeof(x));
        memset(count,0,sizeof(count));
        for( int i = 0 ;i < n ; i++){
            cin >> num[i];
            x[num[i]]++;
        }
        sort(num,num + n);
        int k ;
        int i = 0;
        while(n > 0){
            k = 1; //保证箱子从第一堆开始试起 
            while(x[num[i]] > 0){//保证该载重能力箱子数目大于0 
                if(num[i] < count[k]) k++;//当箱子载重能力小于第k堆已有箱子时 ,开辟新的一堆 
                count[k]++;//将箱子放至第k堆 
                x[num[i]]--;//同一载重能力箱子数量减少 
                n--;//箱子数量减少 
            }
            i++;//开始遍历下一载重能力的箱子 
            if( k > maxnum) maxnum = k;
        }

        cout << maxnum << endl;

    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值