codeforces-388A-Fox and Box Accumulation

codeforces-388A-Fox and Box Accumulation

                time limit per test1 second    memory limit per test256 megabytes

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.
codeforces-388A-Fox and Box Accumulation_第1张图片

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.

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

题目链接:cf-388A

题目思路:记录给个数字出现的次数,从0~101遍历,计算当前这个数字和之前数字最少需要的块数。
计算方法:ret = (int) ceil (sum * 1.0 / (i + 1)); //sum是记录小于等于当前数字i的数字个数

以下是代码:

#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
int a[200];
int main(){
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        int num;
        cin >> num;
        a[num]++;
    }
    int ans = 0,ret = 0,sum = 0;
    for (int i = 0; i < 101; i++)
    {
        sum += a[i];
        ret = (int)ceil(sum * 1.0 / (i + 1));
        ans = max(ans,ret);
    }  
    cout << ans << endl;
    return 0;
}

你可能感兴趣的:(codeforces)