Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; template <typename T> T power(T a, int n, int mod) { T res = 1; while (n) { if (n % 2 == 1) res = (res * a) % mod; n /= 2; a = (a * a) % mod; } return res; } int maxSeq(vector<int>& v) { int lv = v[0]; int li = 1; vector<int> v2; for (int i = 1; i < v.size(); i++) { if (v[i] >= lv && v[i] - li >= 0) { li++; lv = v[i]; } else { v2.push_back(v[i]); } } v = v2; return 1; } int main() { int n, a; vector<int> v; cin >> n; for (int i = 0; i < n; i++) { cin >> a; v.push_back(a); } sort(v.begin(), v.end()); int ans = 0; while (v.size()) { ans += maxSeq(v); } cout << ans; return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:2000000") #pragma comment(linker, "/HEAP:2000000") using namespace std; void print_width(long long x) { std::cout << std::fixed; std::cout << std::setprecision(x); } long long power(long long x, long long y, long long p = 1000000007) { long long res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } void printArr(long long a[], long long n) { for (long long i = 0; i < n; i++) cout << a[i] << " "; cout << '\n'; } void printVector(std::vector<long long> v) { for (long long i = 0; i < v.size(); i++) cout << v[i] << " "; cout << '\n'; } void printVectorPair(std::vector<pair<long long, long long>> v) { for (long long i = 0; i < v.size(); i++) cout << v[i].first << " " << v[i].second << '\n'; ; cout << '\n'; } long long Min(long long a, long long b, long long c) { if (a < b and a < c) return a; if (b < c) return b; return c; } void initialize(long long arr[], long long n) { for (long long i = 0; i <= n; i++) arr[i] = i; } long long root(long long arr[], long long i) { while (arr[i] != i) { arr[i] = arr[arr[i]]; i = arr[i]; } return i; } void Union(long long arr[], long long a, long long b) { long long root_a = root(arr, a); long long root_b = root(arr, b); arr[root_a] = root_b; } long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b, a % b); } long long power_wm(long long x, long long y) { long long res = 1; while (y > 0) { if (y & 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } long double dpower(long double x, long long y) { long double res = 1; while (y > 0) { if (y & 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } std::vector<long long> vsum(std::vector<long long> a) { std::vector<long long> s(a.size()); s[0] = a[0]; for (long long i = 1; i <= a.size() - 1; i++) { s[i] = s[i - 1] + a[i]; } return s; } bool comp(pair<long long, long long> a, pair<long long, long long> b) { long long x = a.second * b.first; long long y = a.first * b.second; if (x <= y) return false; else return true; } bool is_cap(char a) { if (a <= 'Z' and a >= 'A') return true; else return false; } bool is_small(char a) { if (a <= 'z' and a >= 'a') return true; else return false; } string findSum(string str1, string str2) { string str = ""; if (str1.size() > str2.size()) { swap(str1, str2); } int n1 = str1.size(), n2 = str2.size(); reverse(str1.begin(), str1.end()); reverse(str2.begin(), str2.end()); int carry; carry = 0; for (int i = 0; i < n1; i++) { int sum; sum = ((str1[i] - '0') + (str2[i] - '0') + carry); str.push_back(sum % 10 + '0'); carry = sum / 10; } for (int i = n1; i < n2; i++) { int sum; sum = ((str2[i] - '0') + carry); str.push_back(sum % 10 + '0'); carry = sum / 10; } if (carry) str.push_back(carry + '0'); reverse(str.begin(), str.end()); return str; } bool mcomp(long long a, long long b, long long c, long long d) { if (a == b and b == c and c == d) return true; else return false; } vector<vector<long long>> g(200001, vector<long long>(0)); long long dfs(long long x, std::vector<long long> &vis, std::vector<long long> &dfs_seq, std::vector<long long> &children) { dfs_seq.push_back(x); for (long long i = 0; i < g[x].size(); i++) { long long y = g[x][i]; if (vis[y] == 0) { vis[y] = 1; dfs(y, vis, dfs_seq, children); children[x] += 1 + children[y]; } } return 0; } long long c2(long long n) { return (n * (n - 1)) / 2; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n; cin >> n; std::vector<long long> a(n); for (long long i = 0; i < n; i++) cin >> a[i]; sort(a.begin(), a.end(), greater<long long>()); ; for (long long b = 1; b <= n; b++) { vector<vector<long long>> g(b + 1, vector<long long>(0)); ; long long p = 0; for (long long i = 0; i < n; i++) { g[p].push_back(a[i]); p = (p + 1) % b; } long long flag = 0; for (long long j = 0; j < b; j++) { for (long long i = 0; i < g[j].size(); i++) { if (g[j][i] >= (g[j].size() - (i + 1))) { } else { flag = 1; } } } if (flag == 0) { cout << b; return 0; } } return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; multiset<int> S; multiset<int>::iterator it; int main() { int n, x, res = 0; scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", &x), S.insert(x); while (!S.empty()) { ++res; int cur = *S.begin(), cnt = 1; S.erase(S.begin()); while (!S.empty()) { it = S.lower_bound(cnt); if (it == S.end()) break; ++cnt; S.erase(it); } } printf("%d", res); }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; const int MAX_N = 110; int x[MAX_N], N, nr[MAX_N]; int main() { cin >> N; for (int i = 1; i <= N; ++i) cin >> x[i]; sort(x + 1, x + N + 1); int l = 1, r = N, ans = 0; while (l <= r) { int mid = (l + r) / 2, aux = 0, curr = 1; bool ev = true; for (int i = N; i >= N - mid + 1; --i) nr[++aux] = x[i]; for (int i = N - mid; i >= 1 && ev; --i) { if (nr[curr] == 0) ev = false; else { --nr[curr]; if (x[i] < nr[curr]) nr[curr] = x[i]; } ++curr; if (curr > mid) curr = 1; } if (ev) { ans = mid; r = mid - 1; } else l = mid + 1; } cout << ans; return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.util.Arrays; import java.util.Scanner; public class FoxBoxAcc { static int x[]; static boolean taken[]; static int n; static void init() { Scanner sc = new Scanner(System.in); n = sc.nextInt(); x = new int[n]; taken = new boolean[n]; for(int i = 0; i < n; i++) x[i] = sc.nextInt(); Arrays.sort(x); } static boolean done() { for(int i = 0; i < n; i++) if(!taken[i]) return false; return true; } static void newPile() { int acc = 0; for(int i = 0; i < n; i++) { if(taken[i] || x[i] < acc) continue; taken[i] = true; acc++; } } static int countPiles() { int count = 0; while(!done()) { newPile(); count++; } return count; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); init(); System.out.println(countPiles()); } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; int main() { int i, j, t, n, ans, count = 0, a[105], b[105] = {0}, k; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; b[i]++; } sort(a, a + n); for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (b[i] <= a[j]) { b[j] = b[i] + b[j]; a[j] = a[i] < (a[j] - b[i]) ? a[i] : (a[j] - b[i]); b[i] = 0; break; } } if (j == n) count++; } cout << count << endl; return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import math class CodeforcesTask388ASolution: def __init__(self): self.result = '' self.n = 0 self.boxes = [] def read_input(self): self.n = int(input()) self.boxes = [int(x) for x in input().split(" ")] def process_task(self): counts = [self.boxes.count(x) for x in range(max(self.boxes) + 1)] constraints = [math.ceil(sum(counts[0:x + 1]) / (x + 1)) for x in range(len(counts))] self.result = str(max(constraints)) def get_result(self): return self.result if __name__ == "__main__": Solution = CodeforcesTask388ASolution() Solution.read_input() Solution.process_task() print(Solution.get_result())
PYTHON3
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import sys import math n = int(sys.stdin.readline()) xn = [int(x) for x in (sys.stdin.readline()).split()] xn.sort() k = [1] res = 1 for i in xn[1:]: flag = False for t in range(res): if(i >= k[t]): k[t] += 1 flag = True break if(flag == False): res += 1 k.append(1) print(res)
PYTHON3
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
__author__ = 'martslaaf' a = raw_input() table = map(int, raw_input().split(' ')) stops = 0 while table: top = min(table) table.remove(top) weight = 1 while True: sides = [x for x in table if x >= weight] if sides: elem = min(sides) table.remove(elem) weight += 1 else: stops += 1 break print stops
PYTHON
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
n = input() s = sorted(map(int,raw_input().split())) a = [] for i in s: if len(a)==0: a.append(1) else: if i<min(a): a.append(1) else: a[a.index(min(a))]+=1 print len(a)
PYTHON
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Soly { static final int INF = Integer.MAX_VALUE; static void mergeSort(int[] a,int [] c, int b, int e) { if(b < e) { int q = (b + e) >>1; mergeSort(a,c, b, q); mergeSort(a, c,q + 1, e); merge(a,c, b, q, e); } } static void merge(int[] a,int[]c, int b, int mid, int e) { int n1 = mid - b + 1; int n2 = e - mid; int[] L = new int[n1+1], R = new int[n2+1]; int[] L1 = new int[n1+1], R1 = new int[n2+1]; for(int i = 0; i < n1; i++) { L[i] = a[b + i]; L1[i] = c[b + i]; } for(int i = 0; i < n2; i++) { R[i] = a[mid + 1 + i]; R1[i] = c[mid + 1 + i]; } L[n1] = R[n2] = INF; for(int k = b, i = 0, j = 0; k <= e; k++) if(L[i] <= R[j]){ a[k] = L[i]; c[k] = L1[i++]; } else { a[k] = R[j]; c[k] = R1[j++]; } } static ArrayList<Integer>ss,hh; static int bs(int n){ int s=0,e=hh.size()-1,mid=0,ans=-1; while (s<=e){ mid=(e+s)>>1; if (hh.get(mid)>=n){ ans=mid; e=mid-1; }else s=mid+1; } return ans; } static ArrayList<Integer> div(int v) { ArrayList<Integer>ans= new ArrayList<>(); for (int i = 1; i*i <=v ; i++) { if (v%i==0){ ans.add(i); if ((v/i)!=i) ans.add(v/i); } } return ans; } public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); try (PrintWriter or = new PrintWriter(System.out)) { int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; ++i) { a[i] = in.nextInt(); } int[] count = new int[n]; Arrays.sort(a); for (int x : a) { for (int i = 0; i < n; ++i) if (count[i] <= x) { ++count[i]; break; } } int res = 0; for (int x : count) if (x != 0) ++res; or.println(res); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) { if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) { f *= 10; } } } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } } class coooo implements Comparable<coooo> { String st; int s,h; public coooo(String f,int s, int h) { this.st=f; this.s = s; this.h = h; //this.ind = ind; } @Override public int compareTo(coooo o) { return Long.compare(1l*o.s*h,1l*s*o.h); } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#coding=utf-8 n = int(raw_input()) x = map(int, raw_input().split()) x.sort() L = list() for i in x: flag = True for j in range(len(L)): if L[j] <= i: L[j] += 1 flag = False break if flag: L.append(1) print len(L)
PYTHON
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; int boxes[105]; bool used[105]; int main() { int piles = 0, n; cin >> n; for (int i = 0; i < n; i++) cin >> boxes[i]; sort(boxes, boxes + n); for (int i = 0; i < n; i++) { if (!used[i]) { piles++; int idx = i + 1; int sum = 1; while (idx < n) { if (used[idx]) { idx++; continue; } if (boxes[idx] >= sum) { used[idx] = true; sum++; } idx++; } } } cout << piles; return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; int n, arr[102], pile; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) { cin >> arr[i]; arr[i]++; } sort(arr, arr + n); for (int i = 0; i < n; i++) if (pile * arr[i] <= i) pile++; cout << pile << endl; return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; int main() { int n, a[100], k = 1; set<pair<int, int> > s; cin >> n; for (int i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n); pair<int, int> p; p = make_pair(1, 0); s.insert(p); for (int i = 1; i < n; i++) { if (a[i] >= s.begin()->first) { p = *s.begin(); p.first++; s.erase(*s.begin()); s.insert(p); } else { p = make_pair(1, k); s.insert(p); k++; } } cout << k; return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; int s[110], v[110]; int main() { int n, i, j, ans, sum, p; while (cin >> n) { for (i = 0; i < n; i++) { cin >> s[i]; } sort(s, s + n); memset(v, 0, sizeof(v)); sum = 0; ans = 0; while (sum < n) { ans++; p = 0; for (i = 0; i < n; i++) { if (v[i] == 0 && s[i] >= p) { v[i] = 1; p++; sum++; } } } cout << ans << endl; } return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; int arr[105]; int mark[105]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) cin >> arr[i]; sort(arr + 1, arr + n + 1); int res = 0; for (int i = 1; i <= n; i++) { if (mark[i] == true) { continue; } res++; int cnt = 1; for (int j = i + 1; j <= n; j++) { if (mark[j] == false && arr[j] >= cnt) { mark[j] = true; cnt++; } } } cout << res << endl; return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.io.*; import java.util.*; public final class code // public class Main // class code // public class Solution { static int n,x[]; static void solve()throws IOException { n=nextInt(); x=new int[n+1]; for(int i=1;i<=n;i++) x[i]=nextInt(); Arrays.sort(x); int low=1,high=n; int ans=0; while(low<=high) { int mid=(low+high)/2; if(noofboxes(mid)==n)//number of boxes that can be supported by mid piles { ans=mid; high=mid-1; } else low=mid+1; } out.println(ans); } static int noofboxes(int p) { PriorityQueue<Integer> pq=new PriorityQueue<>(p,Collections.reverseOrder()); for(int i=1;i<=p;i++) pq.add(inf); int i; for(i=n;i>=1 && !pq.isEmpty();i--) { int pile=pq.remove(); if(x[i]==0) continue; if(x[i]>=pile) { if(pile!=1) pq.add(pile-1); } else pq.add(x[i]); } return n-i; } /////////////////////////////////////////////////////////// static final long mod=(long)(1e9+7); static final int inf=(int)(1e9+1); static final int maxn=(int)(1e6); static final long lim=(long)(1e18); public static void main(String args[])throws IOException { br=new BufferedReader(new InputStreamReader(System.in)); out=new PrintWriter(new BufferedOutputStream(System.out)); solve(); // int t=nextInt(); // for(int i=1;i<=t;i++) // { // // out.print("Case #"+i+": "); // solve(); // } out.close(); } static int max(int ... a) { int ret=a[0]; for(int i=1;i<a.length;i++) ret=Math.max(ret,a[i]); return ret; } static int min(int ... a) { int ret=a[0]; for(int i=1;i<a.length;i++) ret=Math.min(ret,a[i]); return ret; } static void debug(Object ... a) { System.out.print("> "); for(int i=0;i<a.length;i++) System.out.print(a[i]+" "); System.out.println(); } static void debug(int a[]){debuga(Arrays.stream(a).boxed().toArray());} static void debug(long a[]){debuga(Arrays.stream(a).boxed().toArray());} static void debuga(Object a[]) { System.out.print("> "); for(int i=0;i<a.length;i++) System.out.print(a[i]+" "); System.out.println(); } static Random random; static BufferedReader br; static StringTokenizer st; static PrintWriter out; static String nextToken()throws IOException { while(st==null || !st.hasMoreTokens()) st=new StringTokenizer(br.readLine()); return st.nextToken(); } static String nextLine()throws IOException { return br.readLine(); } static int nextInt()throws IOException { return Integer.parseInt(nextToken()); } static long nextLong()throws IOException { return Long.parseLong(nextToken()); } static double nextDouble()throws IOException { return Double.parseDouble(nextToken()); } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.util.*; import java.math.*; import java.io.*; public class B{ static int[] dx={-1,1,0,0}; static int[] dy={0,0,1,-1}; static FastReader scan=new FastReader(); public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out)); static ArrayList<Pair>es; static LinkedList<Integer>edges[]; static boolean prime[]; static void sieve(int n) { prime = new boolean[n+1]; for(int i=0;i<n;i++) prime[i] = true; for(int p = 2; p*p <=n; p++) { if(prime[p] == true) { for(int i = p*p; i <= n; i += p) prime[i] = false; } } } public static int lowerBound(long[] array, int length, long value) { int low = 0; int high = length; while (low < high) { final int mid = (low + high) / 2; //checks if the value is less than middle element of the array if (value <= array[mid]) { high = mid; } else { low = mid + 1; } } return low; } public static int upperBound(long[] array, int length, long value) { int low = 0; int high = length; while (low < high) { final int mid = low+(high-low) / 2; if ( array[mid]>value) { high = mid ; } else { low = mid+1; } } return low; } static long mod(long x,long y) { if(x<0) x=x+(-x/y+1)*y; return x%y; } static boolean isPowerOfTwo(long n) { if (n == 0) return false; while (n != 1) { if (n % 2 != 0) return false; n = n / 2; } return true; } static boolean isprime(long x) { for(long i=2;i*i<=x;i++) if(x%i==0) return false; return true; } static int dist(int x1,int y1,int x2,int y2){ return Math.abs(x1-x2)+Math.abs(y1-y2); } static long cuberoot(long x) { long lo = 0, hi = 1000005; while(lo<hi) { long m = (lo+hi+1)/2; if(m*m*m>x) hi = m-1; else lo = m; } return lo; } public static int log2(int N) { // calculate log2 N indirectly // using log() method int result = (int)(Math.log(N) / Math.log(2)); return result; } static long gcd(long a, long b) { if(a!=0&&b!=0) while((a%=b)!=0&&(b%=a)!=0); return a^b; } static long LCM(long a,long b){ return (Math.abs(a*b))/gcd(a,b); } public static class comp1 implements Comparator<Pair>{ public int compare(Pair o1,Pair o2){ if(o2.x==o1.x) return (o2.y-o1.y)>0?1:-1; return (o2.x-o1.x)>0?1:-1; } } public static class comp2 implements Comparator<Pair>{ public int compare(Pair o1,Pair o2){ return (o2.ab-o1.ab)>0?1:-1; } } static boolean can(int m,int s) { return (s>=0&&s<=m*9); } static boolean collinear(long x1, long y1, long x2, long y2, long x3, long y3) { long a = x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2); if(a==0) return true; return false; } static int n; static int arr[]; static int dp[][]; static int rec(int i,int last) { if(i==n) return 0; if(dp[i][last]!=-1) return dp[i][last]; int max=Integer.MAX_VALUE; if(arr[i]==0) { max=1+rec(i+1,0); } else if(arr[i]==1) { if(last==1) max=1+rec(i+1,0); else { max=Math.min(rec(i+1,0)+1,rec(i+1,1)); } } else if(arr[i]==2) { if(last==2) { max=1+rec(i+1,0); } else max=Math.min(rec(i+1,0)+1,rec(i+1,2)); } else { if(last==1) max=Math.min(rec(i+1,0)+1,rec(i+1,2)); else if(last==2) max=Math.min(rec(i+1,0)+1,rec(i+1,1)); else max=Math.min(rec(i+1,1),Math.min(rec(i+1,2),rec(i+1,0)+1)); } return dp[i][last]=max; } public static void main(String[] args) throws Exception { //java.util.Scanner scan=new java.util.Scanner(new File("mootube.in")); //PrintWriter out = new PrintWriter (new FileWriter("mootube.out")); //scan=new FastReader("equal.in"); //out = new PrintWriter ("output.txt"); //System.out.println(3^2); //System.out.println(19%4); //StringBuilder news=new StringBuilder("ab"); //news.deleteCharAt(1); //news.insert(0,'c'); //news.deleteCharAt(0); //System.out.println(news); //System.out.println(can(2,15)); //System.out.println(LCM(2,2)); // System.out.println(3^2); int tt=1; //tt=scan.nextInt(); outer:while(tt-->0) { int n=scan.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) { arr[i]=scan.nextInt(); } Arrays.sort(arr); int count[]=new int[n]; for(int l:arr) { for(int i=0;i<n;i++) { if(count[i]<=l) { count[i]++; break; } } } int res=0; for(int i=0;i<n;i++) if(count[i]!=0) res++; out.println(res); } out.close(); } static long binexp(long a,long n,long mod) { if(n==0) return 1; long res=binexp(a,n/2,mod)%mod; res=res*res; if(n%2==1) return (res*a)%mod; else return res%mod; } static class special implements Comparable<special> { char x; int id; special(char x,int id) { this.id=id; this.x=x; } public int compareTo(special o) { return o.id-id; } } public static long pow(long b, long e) { long r = 1; while (e > 0) { if (e % 2 == 1) r = r * b ; b = b * b; e >>= 1; } return r; } private static void sort(int[] arr) { List<Integer> list = new ArrayList<>(); for (int object : arr) list.add(object); Collections.sort(list); for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i); } public static class FastReader { BufferedReader br; StringTokenizer root; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } FastReader(String filename)throws Exception { br=new BufferedReader(new FileReader(filename)); } String next() { while (root == null || !root.hasMoreTokens()) { try { root = new StringTokenizer(br.readLine()); } catch (Exception addd) { addd.printStackTrace(); } } return root.nextToken(); } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (Exception addd) { addd.printStackTrace(); } return str; } public int[] nextIntArray(int arraySize) { int array[] = new int[arraySize]; for (int i = 0; i < arraySize; i++) { array[i] = nextInt(); } return array; } } public static class Pair implements Comparable<Pair>{ int x; int y; long ab; int z; public Pair(){} public Pair(int x1, int y1,int z) { x=x1; y=y1; this.z=z; } public Pair(int x1, int y1) { x=x1; y=y1; this.ab=x+y; } @Override public int hashCode() { return (int)(x + 31 * y); } public String toString() { return x + " " + y; } @Override public boolean equals(Object o){ if (o == this) return true; if (o.getClass() != getClass()) return false; Pair t = (Pair)o; return t.x == x && t.y == y; } public int compareTo(Pair o) { return o.y-y; } } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
n=int(input()) l=list(map(int,input().split())) l.sort() ans=0 while len(l)!=0: A=[] k=1 ans+=1 for j in range(1,len(l)): if(l[j]!=0 and l[j]>=k): k+=1 else: A.append(l[j]) l=A print(ans)
PYTHON3
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; int arr[1000] = {0}; int book[1000] = {0}; int n = 0; int ct = 0; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> arr[i]; } sort(arr + 1, arr + n + 1); int num = 1000; while (1) { num = 0; for (int i = 1; i <= n; i++) { if (!book[i] && arr[i] >= num) { num++; book[i] = 1; } } if (num) ct++; else break; } cout << ct; return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
from itertools import * n = int(raw_input()) x = map(int, raw_input().split()) x = sorted(x, reverse=True) for p in xrange(1,n+1): bins = [200 for _ in xrange(p)] for e in x: _,i = max(zip(bins,xrange(p)), key=lambda a: a[0]) bins[i] = min(e,bins[i]-1) if len(filter(lambda b: b < 0, bins)) == 0: print p break
PYTHON
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashSet; import java.util.Set; import java.util.Stack; import java.util.StringTokenizer; public class Coder{ static class FastScanner{ BufferedReader s; StringTokenizer st; public FastScanner(){ st = new StringTokenizer(""); s = new BufferedReader(new InputStreamReader(System.in)); } public int nextInt() throws IOException{ if(st.hasMoreTokens()) return Integer.parseInt(st.nextToken()); else{ st = new StringTokenizer(s.readLine()); return nextInt(); } } public String nextString() throws IOException{ if(st.hasMoreTokens()) return st.nextToken(); else{ st = new StringTokenizer(s.readLine()); return nextString(); } } public String readLine() throws IOException{ return s.readLine(); } public void close() throws IOException{ s.close(); } } public static void main(String[] args) throws IOException{ FastScanner s = new FastScanner(); PrintWriter ww = new PrintWriter(new OutputStreamWriter(System.out)); int n = s.nextInt(); int[] arr = new int[n]; for(int i=0;i<n;i++) arr[i] = s.nextInt(); int cn = 0; int d = 0; int ans=0; Arrays.sort(arr); boolean[] v = new boolean[n]; while(cn < n){ d = 0; for(int i=0;i<n;i++){ if(!v[i]){ if(d <= arr[i]){ d++; cn++; v[i] = true; } } } ans++; } ww.println(ans); s.close(); ww.close(); } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.*; import java.io.IOException; public class Main { public static void main(String[] args) { OutputStream outputStream = System.out; myScanner in = new myScanner(); PrintWriter out = new PrintWriter(outputStream); Solution solver = new Solution(); solver.solve(1, in, out); out.close(); } static class Solution { public void solve(int testNumber, myScanner in, PrintWriter out) { int n = in.nextInt(); int[] boxes = new int[n]; boolean[] visited = new boolean[n]; Arrays.fill(visited, false); int count = 0, arrCount = 0, maxCount = 0; for(int i = 0; i < n; ++i) { boxes[i] = in.nextInt(); } Arrays.sort(boxes); for(int i = 0; i < n; ++i) { if(visited[i]) { continue; } if(arrCount == n) { break; } int box = boxes[i]; visited[i] = true; arrCount++; count = 1; for(int j = i+1; j < n; ++j) { if(!visited[j] && boxes[j] >= count) { visited[j] = true; arrCount++; count++; } } maxCount++; } out.println(maxCount); } } static class myScanner { BufferedReader br; StringTokenizer st; public myScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } boolean ready() throws IOException { return br.ready(); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.util.*; public class A { public static void main(String ar[]) { Scanner s=new Scanner(System.in); int n=s.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=s.nextInt(); Arrays.sort(a); int piles=0; int left=n; while(left>0) { int h=0; piles++; for(int i=0;i<n;i++) if(a[i]>=h) { h++; a[i]=-1; left--; } } System.out.println(piles); } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.List; import java.util.AbstractCollection; import java.util.StringTokenizer; import java.io.BufferedReader; import java.util.LinkedList; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); LinkedList<Integer> x = new LinkedList<>(); for (int i = 0; i < n; i++) { x.add(in.nextInt()); } x.sort((Integer a, Integer b) -> a - b); int ans = 0; while (!x.isEmpty()) { ans++; LinkedList<Integer> pail = new LinkedList<>(); LinkedList<Integer> newX = new LinkedList<>(); for (int i : x) { if (i >= pail.size()) { pail.add(i); } else { newX.add(i); } } x = newX; } out.println(ans); } } static class InputReader { private StringTokenizer tokenizer; private BufferedReader reader; public InputReader(InputStream inputStream) { reader = new BufferedReader(new InputStreamReader(inputStream)); } private void fillTokenizer() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (Exception e) { throw new RuntimeException(e); } } } public String next() { fillTokenizer(); return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; int M = 1e9 + 7; void solve() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (int num = 1; num <= n; num++) { int count[num], flag = 0; for (int index = 0; index < num; index++) count[index] = 1e9; for (int i = n - 1; i >= 0; i--) { if (count[i % num] == 0) { flag++; break; } count[i % num] = min(count[i % num] - 1, a[i]); } if (flag == 0) { cout << num << "\n"; return; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; while (t--) { solve(); } return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.io.*; import java.math.*; import java.util.*; import java.lang.*; public class Main{ public static InputStream inputStream = System.in; public static OutputStream outputStream = System.out; public static FastReader in = new FastReader(inputStream); public static PrintWriter out = new PrintWriter(outputStream); public static void main(String[] args)throws java.lang.Exception{ new Main().run(); out.close(); } int n; int m; int t; int[] a; void run()throws java.lang.Exception{ // int mm = (int)(Math.random()*10); // int[] bb = new int[mm]; // for(int i=0; i<mm; i++){ // bb[i] = (int)(Math.random()*5); // } // Arrays.sort(bb); // for(int i=0, j=mm-1; i<=j; i++, j--){ // int t = bb[i]; // bb[i] =bb[j]; // bb[j] = t; // } // System.out.println(Arrays.toString(bb)); n = in.nextInt(); a = new int[n]; for(int i=0; i<n; i++){ a[i] = in.nextInt(); if(a[i]==0){ zeros++; } } // if(zeros>0){ // System.out.println(zeros); // }else{ // System.out.println(1); // } // System.exit(0); Arrays.sort(a); for(int i=0, j=n-1; i<=j; i++, j--){ int t = a[i]; a[i] = a[j]; a[j] = t; } // System.out.println(Arrays.toString(a)); int last = n-1; int[] sz = new int[n]; Arrays.fill(sz, 1); boolean[] pl = new boolean[n]; int tm = 0; while(true){ if(tm>100){ System.exit(0); } tm++; boolean d = false; // System.out.println("\n ITER " + last); for(int i=last-1; i>=0; i--){ // System.out.println(i + " " + a[i] + " >= " + a[last]); if(a[i] >= sz[last]){ // System.out.println("place " + last + " on " + i); // System.out.println(a[i] + "> " + sz[last]); sz[i] += sz[last]; a[i] = Math.min(a[last], a[i]-sz[last]); // System.out.println("szi " + sz[i] + " ai " + a[i]); pl[last] = true; last--; d = true; break; } } if(!d){ last--; } if(last<=0){ int ans = 0; for(int i=0; i<n; i++){ if(!pl[i]){ ans++; } } System.out.println(ans); break; } } } int zeros = 0; int iter = 0; int place(int max, int from){ // System.out.println(max + " " +from); iter++; if(iter>50){ System.exit(0); } if(max==0){ // System.out.println("ret1"); return from; } if(from>=n-1){ // System.out.println("ret2"); return n; } if(a[from+1] == max){ // System.out.println("eq"); return place(max-1, from+1); }else{ // System.out.println("smaller"); return place(a[from], from+1); } } int gcd(int a, int b){ // System.out.println(a + " " + b); if(b==0){ return a; } return gcd(b, a%b); } } class Obj implements Comparable<Obj>{ int x; int y; public Obj(int a, int b){ x = a; y = b; } public int compareTo(Obj that){ return Integer.valueOf(x).compareTo(that.x); } } class FastReader{ private boolean finished = false; private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public FastReader(InputStream stream){ this.stream = stream; } public int read(){ if (numChars == -1){ throw new InputMismatchException (); } if (curChar >= numChars){ curChar = 0; try{ numChars = stream.read (buf); } catch (IOException e){ throw new InputMismatchException (); } if (numChars <= 0){ return -1; } } return buf[curChar++]; } public int peek(){ if (numChars == -1){ return -1; } if (curChar >= numChars){ curChar = 0; try{ numChars = stream.read (buf); } catch (IOException e){ return -1; } if (numChars <= 0){ return -1; } } return buf[curChar]; } public int nextInt(){ int c = read (); while (isSpaceChar (c)) c = read (); int sgn = 1; if (c == '-'){ sgn = -1; c = read (); } int res = 0; do{ if(c==','){ c = read(); } if (c < '0' || c > '9'){ throw new InputMismatchException (); } res *= 10; res += c - '0'; c = read (); } while (!isSpaceChar (c)); return res * sgn; } public long nextLong(){ int c = read (); while (isSpaceChar (c)) c = read (); int sgn = 1; if (c == '-'){ sgn = -1; c = read (); } long res = 0; do{ if (c < '0' || c > '9'){ throw new InputMismatchException (); } res *= 10; res += c - '0'; c = read (); } while (!isSpaceChar (c)); return res * sgn; } public String nextString(){ int c = read (); while (isSpaceChar (c)) c = read (); StringBuilder res = new StringBuilder (); do{ res.appendCodePoint (c); c = read (); } while (!isSpaceChar (c)); return res.toString (); } public boolean isSpaceChar(int c){ if (filter != null){ return filter.isSpaceChar (c); } return isWhitespace (c); } public static boolean isWhitespace(int c){ return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0(){ StringBuilder buf = new StringBuilder (); int c = read (); while (c != '\n' && c != -1){ if (c != '\r'){ buf.appendCodePoint (c); } c = read (); } return buf.toString (); } public String nextLine(){ String s = readLine0 (); while (s.trim ().length () == 0) s = readLine0 (); return s; } public String nextLine(boolean ignoreEmptyLines){ if (ignoreEmptyLines){ return nextLine (); }else{ return readLine0 (); } } public BigInteger nextBigInteger(){ try{ return new BigInteger (nextString ()); } catch (NumberFormatException e){ throw new InputMismatchException (); } } public char nextCharacter(){ int c = read (); while (isSpaceChar (c)) c = read (); return (char) c; } public double nextDouble(){ int c = read (); while (isSpaceChar (c)) c = read (); int sgn = 1; if (c == '-'){ sgn = -1; c = read (); } double res = 0; while (!isSpaceChar (c) && c != '.'){ if (c == 'e' || c == 'E'){ return res * Math.pow (10, nextInt ()); } if (c < '0' || c > '9'){ throw new InputMismatchException (); } res *= 10; res += c - '0'; c = read (); } if (c == '.'){ c = read (); double m = 1; while (!isSpaceChar (c)){ if (c == 'e' || c == 'E'){ return res * Math.pow (10, nextInt ()); } if (c < '0' || c > '9'){ throw new InputMismatchException (); } m /= 10; res += (c - '0') * m; c = read (); } } return res * sgn; } public boolean isExhausted(){ int value; while (isSpaceChar (value = peek ()) && value != -1) read (); return value == -1; } public String next(){ return nextString (); } public SpaceCharFilter getFilter(){ return filter; } public void setFilter(SpaceCharFilter filter){ this.filter = filter; } public interface SpaceCharFilter{ public boolean isSpaceChar(int ch); } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int str[n]; for (int i = 0; i < n; i++) cin >> str[i]; sort(str, str + n); int piles = 1; for (int boxseen = 1; boxseen < n; boxseen++) { if (str[boxseen] < (boxseen / piles)) piles++; } cout << piles; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; int n, y, cnt = 0; vector<pair<int, int> > x; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &y); x.push_back(make_pair(y, 0)); } sort((x.begin()), (x.end())); for (int j = 1; j < n; j++) { int i = j - 1; while (i >= 0) { if (x[i].first != -1) { y = x[i].second + 1; if (y <= x[j].first) { x[j].second = y; x[i].first = -1; break; } } i--; } } for (int i = 0; i < n; i++) if (x[i].first != -1) cnt++; printf("%d ", cnt); return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n) { int v[n]; for (int i = 0; i < n; i++) cin >> v[i]; sort(v, v + n); int c = 0; for (int i = 0; i < n; i++) { if (v[i] == -1) continue; int p = 1; c++; for (int j = i + 1; j < n; j++) { if (v[j] >= p) { p++; v[j] = -1; } } } cout << c << endl; } return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.PriorityQueue; import java.util.StringTokenizer; import java.util.TreeSet; public class Main { /** * @param args */ BufferedReader in; PrintWriter out = new PrintWriter(System.out); StringTokenizer tok; String readToken() throws IOException { // reads the token; to read a full line use in.readLine() while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); // sometimes it's better to use nextToken(String) method here } int readInt() throws IOException { // write readDouble(), readLong(), readlong() methods if necessary return Integer.parseInt(readToken()); } long readLong() throws IOException { // write readDouble(), readLong(), readlong() methods if necessary return Long.parseLong(readToken()); } public String readLine() throws IOException{ return in.readLine(); } double readDouble() throws IOException { // write readDouble(), readLong(), readlong() methods if necessary return Double.parseDouble(readToken()); } public static void main(String[] args) throws IOException { new Main().run(); } void run() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); solve(); out.flush(); tok=null; } void solve() throws IOException{ int n=readInt(); int[] x=new int[n]; int[] max=new int[n]; for (int i=0;i<n;i++){ x[i]=readInt(); } Arrays.sort(x); int cS=1; int t=0; PriorityQueue<Integer> pq=new PriorityQueue<Integer>(); pq.add(1); for (int i=1;i<n;i++){ int min=pq.peek(); if (x[i]>=min){ pq.poll(); pq.add(min+1); }else{ pq.add(1); } } out.println(pq.size()); } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
from collections import Counter as cs n=int(input()) ls=[int(a) for a in input().split()] ls.sort() ls1=dict(cs(ls)) ctr1,ctr2=1,0 for i in range(n): if ls1[ls[i]]: ls1[ls[i]]-=1 for j in range(i,n): if i!=j and ls[j]>=ctr1 and ls1[ls[j]]: ctr1+=1 ls1[ls[j]]-=1 ctr2+=1 ctr1=1 print(ctr2)
PYTHON3
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.io.*; import java.util.*; public class cf388a { static FastIO in = new FastIO(), out = in; public static void main(String[] args) { int n = in.nextInt(); int[] v = new int[n]; for(int i=0; i<n; i++) v[i] = in.nextInt(); Arrays.sort(v); int lo = 1, hi = n; while(hi - lo > 2) { int mid = (hi+lo)/2; if(ok(mid, v)) hi = mid; else lo = mid; } while(!ok(lo, v)) lo++; out.println(lo); out.close(); } static boolean ok(int x, int[] v) { for(int i=0; i<v.length; i++) if(v[i] < i/x) return false; return true; } static class FastIO extends PrintWriter { BufferedReader br; StringTokenizer st; public FastIO() { this(System.in, System.out); } public FastIO(InputStream in, OutputStream out) { super(new BufferedWriter(new OutputStreamWriter(out))); br = new BufferedReader(new InputStreamReader(in)); scanLine(); } public void scanLine() { try { st = new StringTokenizer(br.readLine().trim()); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } } public int numTokens() { if (!st.hasMoreTokens()) { scanLine(); return numTokens(); } return st.countTokens(); } public String next() { if (!st.hasMoreTokens()) { scanLine(); return next(); } return st.nextToken(); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; int x[102], n, k; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> x[i]; sort(x, x + n); for (int i = 0; i < n; i++) { if ((x[i] + 1) * k <= i) k++; } cout << k; return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; import java.util.StringTokenizer; /** * Created with IntelliJ IDEA. * User: AUtemuratov * Date: 07.04.14 * Time: 15:43 * To change this template use File | Settings | File Templates. */ public class Main { static FastReader fr = new FastReader(new BufferedReader(new InputStreamReader(System.in))); static PrintWriter pw = new PrintWriter(System.out); static int max = Integer.MIN_VALUE; static int min = Integer.MAX_VALUE; static int n,cnt,ind,m; static boolean ok = false; static int maxn = (int)1e3 + 111; static ArrayList<Pair> a = new ArrayList<Pair>(); public static void main(String[] args) throws Exception { n = fr.nextInt(); for (int i=1; i<=n; i++) { a.add(new Pair(fr.nextInt(),0)); } Collections.sort(a); for (int i=0; i<a.size(); i++) { for (int j=i+1; j<a.size(); j++) { //if (i==j) continue; if (a.get(i).y+1 <= a.get(j).x) { // pw.println(i + " " + j); int cur = a.get(j).y; a.set(j, new Pair(Math.min(a.get(i).x, a.get(j).x-a.get(i).y+1), cur+a.get(i).y+1)); a.remove(i); i--; break; } } } pw.println(a.size()); pw.close(); } } class Pair implements Comparable<Pair>{ int x,y; public Pair (int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair p) { if (this.x>p.x) return 1; else if (this.x==p.x)return 0; else if (this.x<p.x) return -1; return 0; } } class FastReader { BufferedReader bf; StringTokenizer tk = null; String DELIM = " "; public FastReader(BufferedReader bf) { this.bf = bf; } public String nextToken() throws Exception { if(tk==null || !tk.hasMoreTokens()) { tk = new StringTokenizer(bf.readLine(),DELIM); } return tk.nextToken(); } public int nextInt() throws Exception { return Integer.parseInt(nextToken()); } public int nextLong() throws Exception { return Integer.parseInt(nextToken()); } public int nextDouble() throws Exception { return Integer.parseInt(nextToken()); } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
# -*- coding: utf-8 -*- n=int(raw_input()) s=map(int,raw_input().split()) s.sort() piles=0 while s.count(-1)<n: h=0 found=0 for i,t in enumerate(s): if t>=h: s[i]=-1 h+=1 piles+=1 print piles
PYTHON
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; const long long dx[4] = {-1, 1, 0, 0}; const long long dy[4] = {0, 0, -1, 1}; long long XX[] = {-1, -1, -1, 0, 0, 1, 1, 1}; long long YY[] = {-1, 0, 1, -1, 1, -1, 0, 1}; const long long N = (long long)(6 * 1e5 + 10); const long long M = 1e9 + 7; long long fact[N], invfact[N]; long long pow(long long a, long long b, long long m) { long long ans = 1; while (b) { if (b & 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans; } long long modinv(long long k) { return pow(k, (long long)998244353 - 2, (long long)998244353); } void precompute() { fact[0] = fact[1] = 1; for (long long i = 2; i < N; i++) fact[i] = fact[i - 1] * i, fact[i] %= (long long)998244353; invfact[N - 1] = modinv(fact[N - 1]); for (long long i = N - 2; i >= 0; i--) invfact[i] = invfact[i + 1] * (i + 1), invfact[i] %= (long long)998244353; } long long nCr(long long x, long long y) { if (y > x) return 0; long long num = fact[x]; num *= invfact[y]; num %= (long long)998244353; num *= invfact[x - y]; num %= (long long)998244353; return num; } int32_t main() { ; ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long n, i; long long a[1010]; cin >> n; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); long long ans = 0; long long x; for (i = 0; i < n; i++) { x = 0; if (a[i] != -1) { ans++; x++; a[i] = -1; for (long long j = i + 1; j < n; j++) { if (a[j] != -1 && a[j] >= x) { a[j] = -1; x++; } } } } cout << ans << endl; return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
# python2 import sys, threading, os.path import collections, heapq, math,bisect import string sys.setrecursionlimit(10**6) # max depth of recursion threading.stack_size(2**27) def is_valid(i,j,n): if i >=0 and j >=0 and i<n and j<n: return True else: return False def main(): if os.path.exists('input.txt'): input = open('input.txt', 'r') else: input = sys.stdin #--------------------------------INPUT--------------------------------- n = int(input.readline()) lis = list(map(int, input.readline().split())) lis.sort() res = [[lis[0]]] j=0 for i in range(1,n): tem = res[len(res)-1] for li in res: if len(li) <= lis[i]: li.append(lis[i]) break else: res.append([lis[i]]) output = len(res) #-------------------------------OUTPUT---------------------------------- if os.path.exists('output.txt'): open('output.txt', 'w').writelines(str(output)) else: sys.stdout.write(str(output)) threading.Thread(target=main).start()
PYTHON
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
n = input() arr = map(int,raw_input().split()) arr.sort() # arr=arr[::-1] piles=[0]*101 s=0 c=0 pile=[1] for i in arr[1:]: # print i f=False for j in range(len(pile)): if pile[j]<=i: pile[j]+=1 f=True break if f==False: pile.append(1) # pile.sort() # pile=pile[::-1] print len(pile)
PYTHON
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.util.*; import java.io.*; public class Fox_and_Box_Accumulation { public static void main(String args[]) throws Exception { BufferedReader f=new BufferedReader(new InputStreamReader(System.in)); // BufferedReader f=new BufferedReader(new FileReader("Fox_and_Box_Accumulation.in")); int runs=Integer.parseInt(f.readLine()); int[] arr=new int[runs]; StringTokenizer st=new StringTokenizer(f.readLine()); for(int x=0;x<runs;x++) arr[x]=Integer.parseInt(st.nextToken()); Arrays.sort(arr); boolean[] vis=new boolean[runs]; int counter=0; for(int x=0;x<runs;x++) { if(vis[x]) continue; int num=1; vis[x]=true; for(int y=x+1;y<runs;y++) if(arr[y]>=num&&!vis[y]) { vis[y]=true; num++; } counter++; } System.out.println(counter); } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
n=int(input()) a=list(map(int,input().split())) st=0 a.sort() while len(a)!=0: wt=1;a[0]='a' for i in range(len(a)): if a[i]!='a' and a[i]>=wt: wt+=1;a[i]='a' for i in range(a.count('a')): a.remove('a') st+=1 print(st)
PYTHON3
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ //package fox.and.box.accumulation; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Writer; import java.util.Arrays; import java.util.InputMismatchException; /** * * @author jigsaw */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { InputStream inputstream = System.in; OutputStream outputstream = System.out; InputReader in = new InputReader(inputstream); OutputWriter outt = new OutputWriter(outputstream); int N = in.readInt(); int arr[] = new int[N]; for(int i=0;i<N;i++) arr[i] = in.readInt(); Arrays.sort(arr); int count = N, noOfPiles = 0; boolean flag[] = new boolean[N]; while(count>0){ int y = 0; int z = 0; while(y<N){ if(!flag[y]&&arr[y]>=z){ flag[y] = true; z++; count--; } y++; } noOfPiles++; } outt.printLine(noOfPiles); outt.close(); } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuffer res = new StringBuffer(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object...objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object...objects) { print(objects); writer.println(); } public void close() { writer.close(); }}
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
INF = 1000000 if __name__ == '__main__': n = int(raw_input()) a = sorted([int(x) for x in raw_input().split()], key=lambda x: -x) for num_piles in range(1, n + 1): piles = [INF] * num_piles i = 0 j = 0 while i < n: for k in range(num_piles): if piles[j] > 0: piles[j] = min(piles[j] - 1, a[i]) i, j = i + 1, (j + 1) % num_piles break j = (j + 1) % num_piles else: break else: print num_piles break
PYTHON
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
t = [] n = int(input()) a = sorted(list(map(int , input().split()))) for i in range(n): t = sorted(t)[::-1] flg = True; for j in range(len(t)): if(a[i] >= t[j]): t[j] += 1;flg = False;break if(flg):t.append(1) print(len(t))
PYTHON3
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; bool used[200]; int x[200]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> x[i]; sort(x, x + n); int ans = 0; for (int i = 0; i < n; i++) { if (used[i]) continue; ans++; used[i] = true; int remain = 1; for (int j = i + 1; j < n; j++) { if (used[j]) continue; if (x[j] >= remain) { used[j] = true; remain++; } } } cout << ans << endl; return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; long long gcd(long long x, long long y) { if (y == 0) return x; else return gcd(y, x % y); } long long expo(long long n, long long m) { long long r = 1; while (m > 0) { if (m % 2) r = (r * n) % 1000000007; n = (n * n) % 1000000007; m = m / 2; } return r % 1000000007; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) cin >> a[i]; sort(a.begin(), a.end()); for (long long i = 1; i <= 150; i++) { long long c = i; for (long long j = n - 1; j >= n - i; j--) { long long k = j - i, cap = a[j]; while (k >= 0 && cap > 0) { c++; cap = min(a[k], cap - 1); k -= i; } } if (c == n) { cout << i; return 0; } } return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Ideone { public static void main (String[] args) throws java.lang.Exception { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); Arrays.sort(a); ArrayList<Integer> pileSize = new ArrayList<>(); for(int i = 0; i < n; i++){ boolean inserted = false; for(int p = 0; p < pileSize.size(); p++){ if(a[i] >= pileSize.get(p)){ pileSize.set(p, pileSize.get(p)+1); inserted = true; break; } } if(!inserted) pileSize.add(1); } System.out.println(pileSize.size()); } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; int a[105]; bool kt[105]; int n, kq; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); sort(a + 1, a + 1 + n); memset(kt, 1, sizeof(kt)); for (int i = 1; i <= n; i++) if (kt[i]) { kq++; int d = 1; int t = a[i]; int j = i + 1; while (j <= n) { if (a[j] >= d && kt[j] && a[j] >= t) { t = a[j]; kt[j] = 0; d++; } j++; } } printf("%d", kq); return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Random; import java.util.StringTokenizer; import java.util.TreeSet; public class C { static int N; static int[] a; public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); N = sc.nextInt(); a = new int[N]; for (int i = 0; i < N; i++) a[i] = sc.nextInt(); Arrays.sort(a); boolean ch = true; int ans = 0; boolean[] vis = new boolean[N]; while (ch) { ch = false; for (int i = 0; i < N; i++) { if (!vis[i]) { ch = true; int sz = 0; for (int j = i; j < N; j++) { if (!vis[j] && a[j] >= sz) { vis[j] = true; sz++; } } ans++; } } } System.out.println(ans); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public int[] nextIntArray1(int n) throws IOException { int[] a = new int[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextInt(); return a; } public int[] nextIntArraySorted(int n) throws IOException { int[] a = nextIntArray(n); Random r = new Random(); for (int i = 0; i < n; i++) { int j = i + r.nextInt(n - i); int t = a[i]; a[i] = a[j]; a[j] = t; } Arrays.sort(a); return a; } public long[] nextLongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public long[] nextLongArray1(int n) throws IOException { long[] a = new long[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextLong(); return a; } public long[] nextLongArraySorted(int n) throws IOException { long[] a = nextLongArray(n); Random r = new Random(); for (int i = 0; i < n; i++) { int j = i + r.nextInt(n - i); long t = a[i]; a[i] = a[j]; a[j] = t; } Arrays.sort(a); return a; } } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.util.Arrays; import java.util.Scanner; public class FoxAndBox { public static void main(String[] args) throws Throwable { Scanner in = new Scanner(System.in); int N = in.nextInt(); int[] B = new int[N]; boolean[] used = new boolean[N]; for (int i = 0; i < N; i++) { B[i] = in.nextInt(); } Arrays.sort(B); int ans = 0; for (int x = 0; x < N; x++) { if (!used[x]) { ans += 1; used[x] = true; int curr = 1; for (int i = x + 1; i < N; i++) { if (!used[i]) { if (B[i] >= curr) { used[i] = true; curr++; } } } } } System.out.println(ans); in.close(); } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.util.Scanner; public class FoxandBoxAccumulation228 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a[] = new int[101]; for (int i = 0; i < n; i++) { a[sc.nextInt()]++; } boolean end = false; int count = 0; while (!end) { int curr = 0; end = true; if (a[0] > 0) { a[0]--; end = false; curr++; } for (int j = 1; j < a.length; j++) { if (a[j] != 0) { end = false; while (curr <= j && a[j] > 0) { // System.out.println(a[j]+" "+j); curr++; a[j]--; } } } count++; } System.out.println(count - 1); } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.io.*; import java.util.*; import java.lang.*; import java.math.*; public class Main { public static int GCD(int a, int b) { if (b==0) return a; return GCD(b,a%b); } public static void main(String args[]) throws IOException { try{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); OutputWriter out = new OutputWriter(outputStream); int n = in.nextInt(); int a[] = new int[n]; for(int i=0;i<n;i++) { a[i] = in.nextInt(); } Arrays.sort(a); int ans = 1; for(int i=0;i<n;i++) { if(a[i]<i/ans) ans++; } out.println(ans); out.close(); }catch(Exception e){return;} } } class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } public Long readLong() { return Long.parseLong(readString()); } public Double readDouble() { return Double.parseDouble(readString()); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import sys input = sys.stdin.readline ''' ''' #from heapq import heapify, heappop, heappush li = lambda: list(map(int, input().split())) n = int(input()) x = li() x.sort() def solve(n, x, num_piles): piles = [x[-i] for i in range(1, num_piles+1)] for i in reversed(range(n-num_piles)): xi = x[i] best_pile = -1 best_index = None for i, pile in enumerate(piles): if pile > best_pile and pile > 0: best_pile = pile best_index = i if best_index == None: return False else: piles[best_index] = min(xi, best_pile - 1) return True for i in range(1, n): if solve(n, x, i): print(i) sys.exit() print(n)
PYTHON3
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.io.*; import java.util.Arrays; import java.util.Vector; public class Main { public static Reader in = new Reader(); public static Writer out = new Writer(); public static void main(String[] args) { int n = in.readInt(); int[] x = new int[n]; for(int i=0; i<n; i++) { x[i] = in.readInt(); } Vector<Integer> piles = new Vector<Integer>(); piles.add(0); int last = -1; int p = -1; Arrays.sort(x); for(int i=0; i<n; i++) { if(x[i] > last) { p = 0; last = x[i]; } int value = piles.get(p); while(value > last) { if(p == piles.size() - 1) { piles.add(0); } value = piles.get(++p); } piles.set(p, value + 1); } out.println(piles.size()); out.close(); } } class Reader { private static final int SIZE = 1<<13; private static InputStream in = System.in; private int offSet; private int bufferSize; private byte[] buffer; private char[] charBuffer; public Reader() { buffer = new byte[SIZE]; charBuffer = new char[SIZE]; } public int readInt() { int number = 0; boolean neg = false; try { if(offSet == bufferSize) { offSet = 0; bufferSize = in.read(buffer); } for(; offSet < bufferSize && buffer[offSet] != '-' && buffer[offSet] < '0'; offSet++) { if(offSet == bufferSize-1) { offSet = -1; bufferSize = in.read(buffer); } } if(buffer[offSet] == '-') { neg = true; offSet++; if(offSet == bufferSize) { offSet = 0; bufferSize = in.read(buffer); } } for(; offSet < bufferSize && buffer[offSet] >= '0' && buffer[offSet] <= '9'; offSet++) { number = number * 10 - '0' + buffer[offSet]; if(offSet == bufferSize-1) { offSet = -1; bufferSize = in.read(buffer); } } offSet++; } catch(IOException io) { io.printStackTrace(); System.exit(0); } return neg ? -number : number; } public long readLong() { long number = 0; boolean neg = false; try { if(offSet == bufferSize) { offSet = 0; bufferSize = in.read(buffer); } for(; offSet < bufferSize && buffer[offSet] != '-' && buffer[offSet] < '0'; offSet++) { if(offSet == bufferSize-1) { offSet = -1; bufferSize = in.read(buffer); } } if(buffer[offSet] == '-') { neg = true; offSet++; if(offSet == bufferSize) { offSet = 0; bufferSize = in.read(buffer); } } for(; offSet < bufferSize && buffer[offSet] >= '0' && buffer[offSet] <= '9'; offSet++) { number = number * 10 - '0' + buffer[offSet]; if(offSet == bufferSize-1) { offSet = -1; bufferSize = in.read(buffer); } } offSet++; } catch(IOException io) { io.printStackTrace(); System.exit(0); } return neg ? -number : number; } public double readDouble() { return Double.parseDouble(readWord()); } public String readLine() { StringBuffer sb = new StringBuffer(); try { if(offSet == bufferSize) { offSet = 0; offSet = in.read(buffer); } for(; offSet < bufferSize && buffer[offSet] < ' '; offSet++) { if(offSet == bufferSize-1) { offSet = -1; bufferSize = in.read(buffer); } } if(bufferSize == -1) { return null; } int count = 0; for(; offSet < bufferSize && buffer[offSet] != '\r' && buffer[offSet] != '\n'; offSet++) { charBuffer[count] = (char)buffer[offSet]; count++; if(count == SIZE) { count = 0; sb.append(charBuffer, 0, count); } if(offSet == bufferSize-1) { offSet = -1; bufferSize = in.read(buffer); } } offSet++; sb.append(charBuffer, 0, count); } catch(IOException io) { io.printStackTrace(); System.exit(0); } return sb.toString(); } public String readWord() { StringBuffer sb = new StringBuffer(); try { if(offSet == bufferSize) { offSet = 0; offSet = in.read(buffer); } for(; offSet < bufferSize && buffer[offSet] < ' '; offSet++) { if(offSet == bufferSize-1) { offSet = -1; bufferSize = in.read(buffer); } } if(bufferSize == -1) { return null; } int count = 0; for(; offSet < bufferSize && buffer[offSet] != ' ' && buffer[offSet] != '\r' && buffer[offSet] != '\n'; offSet++) { charBuffer[count] = (char)buffer[offSet]; count++; if(count == SIZE) { count = 0; sb.append(charBuffer, 0, count); } if(offSet == bufferSize-1) { offSet = -1; bufferSize = in.read(buffer); } } offSet++; sb.append(charBuffer, 0, count); } catch(IOException io) { io.printStackTrace(); System.exit(0); } return sb.toString(); } public char[] readWord(int n) { char[] word = new char[n]; try { if(offSet == bufferSize) { offSet = 0; offSet = in.read(buffer); } for(; offSet < bufferSize && buffer[offSet] < ' '; offSet++) { if(offSet == bufferSize-1) { offSet = -1; bufferSize = in.read(buffer); } } if(bufferSize == -1) { return null; } for(int wi=0; offSet < bufferSize && buffer[offSet] != '\r' && buffer[offSet] != '\n'; offSet++) { word[wi++] = (char)buffer[offSet]; if(offSet == bufferSize-1) { offSet = -1; bufferSize = in.read(buffer); } } offSet++; } catch(IOException io) { io.printStackTrace(); System.exit(0); } return word; } } class Writer { private BufferedWriter output; public Writer() { output = new BufferedWriter(new OutputStreamWriter(System.out)); } public void println() { try { output.append("\n"); } catch(IOException io) { io.printStackTrace(); System.exit(0);} } public void print(Object o) { try { output.append(o.toString()); } catch(IOException io) { io.printStackTrace(); System.exit(0);} } public void println(Object o) { try { output.append(o.toString()+"\n"); } catch(IOException io) { io.printStackTrace(); System.exit(0);} } public void printf(String format, Object... args) { try { output.append(String.format(format, args)); } catch(IOException io) { io.printStackTrace(); System.exit(0);} } public void printfln(String format, Object... args) { try { output.append(String.format(format, args)+"\n"); } catch(IOException io) { io.printStackTrace(); System.exit(0);} } public void flush() { try { output.flush(); } catch(IOException io) { io.printStackTrace(); System.exit(0);} } public void close() { try { output.close(); } catch(IOException io) { io.printStackTrace(); System.exit(0);} } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; public class P388A { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(in, out); out.close(); } static class Task { public void solve(InputReader in, PrintWriter out) { int n = in.nextInt(); int[] a = new int[101]; for (int i = 0; i < n; i++) a[in.nextInt()]++; int pilecnt = 0; while (true) { int i = 0; int boxcnt = 0; while (i <= 100) { if (a[i] > 0 && i >= boxcnt) { a[i] -= 1; boxcnt++; } else i++; } if (boxcnt == 0) break; else pilecnt++; } out.println(pilecnt); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } public int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.util.*;import java.io.*;import java.math.*; public class CF389C { static final Random random=new Random(); static boolean memory = true; static void ruffleSort(int[] a) { int n = a.length; for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } ArrayList<Integer> lst = new ArrayList<>(); for(int i : a) lst.add(i); Collections.sort(lst); for(int i = 0; i < n; i++) a[i] = lst.get(i); } static void ruffleSort(long[] a) { int n = a.length; for (int i=0; i<n; i++) { int oi=random.nextInt(n); long temp=a[oi]; a[oi]=a[i]; a[i]=temp; } ArrayList<Long> lst = new ArrayList<>(); for(long i : a) lst.add(i); Collections.sort(lst); for(int i = 0; i < n; i++) a[i] = lst.get(i); } public static void process()throws IOException { int n = ni(); int[] a = nai(n); int ans = 1; ruffleSort(a); for(int i = 1; i < n; i++){ if(a[i] < (i/ans)) ans++; } pn(ans); } static AnotherReader sc; static PrintWriter out; public static void main(String[] args) throws Exception{ if(memory)new Thread(null, new Runnable() {public void run(){try{new CF389C().run();}catch(Exception e){e.printStackTrace();System.exit(1);}}}, "1", 1 << 28).start(); else new CF389C().run(); } void run()throws Exception { boolean oj = System.getProperty("ONLINE_JUDGE") != null; if(oj){sc=new AnotherReader();out=new PrintWriter(System.out);} else{sc=new AnotherReader(100);out=new PrintWriter("output.txt");} long s = System.currentTimeMillis(); int t=1; //t=ni(); //int k = t; while(t-->0) {/*p("Case #"+ (k-t) + ": ")*/;process();} out.flush(); System.err.println(System.currentTimeMillis()-s+"ms"); out.close(); } static long power(long k, long c, long mod){ long y = 1; while(c > 0){ if(c%2 == 1) y = y * k % mod; c = c/2; k = k * k % mod; } return y; } static int power(int x, int y, int p){ int res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static int log2(int N) { int result = (int)(Math.log(N) / Math.log(2)); return result; } static void pn(Object o){out.println(o);} static void pn(int[] a){for(int i : a)out.print(i+" ");pn("");} static void pn(long[] a){for(long i : a)out.print(i+" ");pn("");} static void p(Object o){out.print(o);} static void YES(){out.println("YES");} static void NO(){out.println("NO");} static void yes(){out.println("yes");} static void no(){out.println("no");} static void p(int[] a){for(int i : a)out.print(i+" ");} static void p(long[] a){for(long i : a)out.print(i+" ");} static void pni(Object o){out.println(o);out.flush();} static int ni()throws IOException{return sc.nextInt();} static long nl()throws IOException{return sc.nextLong();} static double nd()throws IOException{return sc.nextDouble();} static String nln()throws IOException{return sc.nextLine();} static String ns()throws IOException{return sc.next();} static int[] nai(int N)throws IOException{int[]A=new int[N];for(int i=0;i!=N;i++){A[i]=ni();}return A;} static long[] nal(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;} static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));} ///////////////////////////////////////////////////////////////////////////////////////////////////////// static class AnotherReader{BufferedReader br; StringTokenizer st; AnotherReader()throws FileNotFoundException{ br=new BufferedReader(new InputStreamReader(System.in));} AnotherReader(int a)throws FileNotFoundException{ br = new BufferedReader(new FileReader("input.txt"));} String next()throws IOException{ while (st == null || !st.hasMoreElements()) {try{ st = new StringTokenizer(br.readLine());} catch (IOException e){ e.printStackTrace(); }} return st.nextToken(); } int nextInt() throws IOException{ return Integer.parseInt(next());} long nextLong() throws IOException {return Long.parseLong(next());} double nextDouble()throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException{ String str = ""; try{ str = br.readLine();} catch (IOException e){ e.printStackTrace();} return str;}} ///////////////////////////////////////////////////////////////////////////////////////////////////////////// }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; int n, a[101]; bool can(int mid) { for (int i = 0; i < n; ++i) if (i / mid > a[i]) return false; return true; } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", a + i); sort(a, a + n); for (int i = 1; i < 101; ++i) if (can(i)) { printf("%d\n", i); return 0; } return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; public class hals { public static void main(String[] args)throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int[] arr = new int[n]; boolean[] taken = new boolean[n]; String[] in = br.readLine().split(" "); for(int i=0;i<n;i++) arr[i]=Integer.parseInt(in[i]); Arrays.sort(arr); int piles=1,length=1; taken[0]=true; while(true){ int ind = -1; for(int i=1;i<n;i++){ if(!taken[i] && arr[i]>=length){ ind = i; break; } } if(ind!=-1){ taken[ind]=true; length++; } else{ boolean flag =false; for(int i=0;i<n;i++){ if(!taken[i]){ flag=true; break; } } if(!flag)break; else{ length=0; piles++; } } } System.out.println(piles); } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws NumberFormatException, IOException { InputReader in = new InputReader(System.in); int n = in.nextInt(); int[] a = new int[n]; for(int i = 0; i < n; i++) a[i] = in.nextInt(); Arrays.sort(a); ArrayList<Integer> piles = new ArrayList<Integer>(); for(int i = 0; i < n; i++) { boolean added = false; for(int j = 0; j < piles.size() && !added; j++) if(piles.get(j) <= a[i]) { piles.set(j, piles.get(j) + 1); added = true; } if(!added) piles.add(1); } System.out.println(piles.size()); } } class InputReader { StringTokenizer st; BufferedReader br; public InputReader(InputStream stream){ br = new BufferedReader(new InputStreamReader(stream));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException {return Integer.parseInt(next());} public String nextLine() throws IOException {return br.readLine();} public boolean ready() throws IOException {return br.ready();} }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; int a[105], n; bool vis[105]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n); int sum = n; int ans = 0; int now = 0; while (sum) { for (int i = 0; i < n; i++) { if (!vis[i]) { if (a[i] >= now) { now++; vis[i] = 1; sum--; } } } ans++; now = 0; } cout << ans << endl; return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
def main(): n = int(raw_input()) B = sorted( int(x) for x in raw_input().split() ) res = solve(B,n) print res def solve(B,n): count = 0 piles = [] for x in B: placed = False for p in xrange(count): if x>=piles[p]: piles[p] += 1 placed = True break if not placed: piles.append(1) count += 1 return count main()
PYTHON
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; long long GCDFast(long long a, long long b) { while (b) b ^= a ^= b ^= a %= b; return a; } int dx[8] = {-1, 0, 1, 0, -1, 1, 1, -1}; int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; long long a[100005], b[105]; int main() { long long n, i, j, k, temp, m; long long x, y, z, sum, test_case = 0, length, ans, test = 0, l, w, num; string str, str1; i = 1; while (cin >> n) { for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); memset(b, 0, sizeof b); k = 1; for (i = 0; i < n; i++) { int flag = 0; for (j = 0; j < k; j++) { if (b[j] <= a[i]) { flag = 1; b[j]++; break; } } if (flag == 0) { b[k]++; k++; } } cout << k << "\n"; } return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
from math import inf as inf from math import * from collections import * import sys input=sys.stdin.readline t=1 while(t): t-=1 n=int(input()) a=list(map(int,input().split())) a.sort() re=a[0] s=0 for i in range(n): if(s*(a[i]+1)<=i): s+=1 print(s)
PYTHON3
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.util.*; public class GoodWork { static Scanner in =new Scanner(System.in); static int max=1,l=1; public static void main(String[] args) { int n=in.nextInt(); int x[]=new int[101],p[]=new int [101]; for(int i=0;i<n;i++){int o=in.nextInt();x[o]++;} for(int i=0;i<=100;i++){int o=x[i],u=0; while(o>0){ if(p[u]<=i) p[u]++; else{p[u+1]++;++u;} o--; if(u+1>max)max=u+1;} } System.out.println(max); } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; const double EPS = -1e8; const double Pi = acos(-1); bool inline equ(double a, double b) { return fabs(a - b) < EPS; } const int MAXN = 110; int n; vector<int> in; int main() { ios_base::sync_with_stdio(0); cin >> n; in.resize(n); for (int i = (0); i <= (n - 1); i++) cin >> in[i]; sort((in).begin(), (in).end()); int acc = 0, ans = 0; while ((int)(in).size()) { ans++; acc = 0; for (int i = 0; i < (int)(in).size(); i++) if (in[i] >= acc) { acc++; in.erase(in.begin() + i); i--; } } cout << ans << '\n'; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> std::multiset<int> x; std::multiset<int>::iterator it; int n; int kutije[102]; bool Moze(int broj) { for (int i = 0; i < n; ++i) x.insert(kutije[i]); int brojac = 0; while (!x.empty()) { for (int i = 0; i < broj; ++i) { if (x.lower_bound(brojac) != x.end()) x.erase(x.lower_bound(brojac)); else if (x.empty()) return true; else return false; } ++brojac; } return true; } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", &kutije[i]); std::sort(kutije, kutije + n); for (int i = 1; i < 101; ++i) { x.clear(); if (Moze(i)) { printf("%d", i); return 0; } } }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import os import sys from io import BytesIO, IOBase from collections import Counter BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) def gcd(a, b): if a == 0: return b return gcd(b % a, a) def lcm(a, b): return (a * b) / gcd(a, b) def main(): n=int(input()) a=list(map(int, input().split())) a.sort() ans=[] for i in range(1, n+1): ans=[float('inf') for j in range(i)] ind=i-1 ff=1 for j in range(n-1, -1, -1): #print(ans) maxx=-1 maxind=-1 for ind in range(i): if ans[ind]>maxx: maxx=ans[ind] maxind=ind ind=maxind if ans[ind]: ans[ind]=min(ans[ind]-1, a[j]) else: ff=0 break #print(ans) if ff: break print(len(ans)) return if __name__ == "__main__": main()
PYTHON3
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
n=int(input()) boxes=[int(x) for x in input().split()] boxes.sort(reverse=True) piles=[] while boxes: box=boxes.pop() for pile in piles: if box>=len(pile): pile.append(box) break else: piles.append([box]) print(len(piles))
PYTHON3
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
n = input() x = sorted(map(int, raw_input().split())) ans = 1 for i in range(n): if x[i] < i / ans: ans += 1 print ans
PYTHON
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> v; for (int i = 0; i < n; ++i) { int curr; cin >> curr; v.push_back(curr); } sort(v.begin(), v.end()); vector<vector<int>> piles; for (int curr : v) { bool placed = false; for (auto &pile : piles) { if ((int)pile.size() <= curr) { placed = true; pile.push_back(curr); break; } } if (!placed) { vector<int> newpile; newpile.push_back(curr); piles.push_back(newpile); } } std::cout << piles.size() << std::endl; return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; int n, x[200], ans, cur; bool vis[200]; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> x[i]; sort(x, x + n); for (int i = 0; i < n; i++) { if (vis[i]) continue; ans++; for (int j = i + 1; j < n; j++) { if (x[j] > cur && !vis[j]) vis[j] = true, cur++; } cur = 0; } cout << ans; return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
n=int(input()) a=list(map(int,input().split())) a.sort() dp = [0] for i in a: add = False for j in range(len(dp)): if(dp[j]<=i): dp[j]+=1 add = True break if not add : dp.append(1) print(len(dp))
PYTHON3
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } int ans = 0; sort(arr, arr + n); vector<int> v; for (int i = 0; i < n; i++) { if (v.empty()) v.push_back(1); else { if (v[0] <= arr[i]) v[0]++; else v.push_back(1); } sort(v.begin(), v.end()); } cout << v.size(); }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; int main() { int n, tmp; cin >> n; multiset<int> a; for (int i = 0; i < n; i++) { cin >> tmp; a.insert(tmp); } int ans = 0; while (!a.empty()) { int h = 0; while (a.lower_bound(h) != a.end()) { a.erase(a.lower_bound(h)); h++; } if (h) ans++; } cout << ans << endl; return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
n = int(input()) arr = list(map(int,input().split())) arr.sort(reverse = True) vis = [False]*n counter = 0 ans = 0 while counter<n: ans+=1 init = 0 for i in range(n-1,-1,-1): if not vis[i] and arr[i]>=init: vis[i] = True counter+=1 init+=1 print(ans)
PYTHON3
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.io.*; import java.math.*; import java.util.*; import java.lang.*; public class Main{ public static InputStream inputStream = System.in; public static OutputStream outputStream = System.out; public static FastReader in = new FastReader(inputStream); public static PrintWriter out = new PrintWriter(outputStream); static long bigmod(long a,long b,long m) { //(b^p)%m long result = 1; a%=m; while (b!=0){ if ((b & (long)1)!=0){ result *= a; result %= m; } b >>=1 ; a *= a; a%=m; } return result; } public static void main(String[] args)throws java.lang.Exception { new Main().run(); out.close(); } int N; int C; int M; int T; int A; int B; int K; char[][] arr; final long mod =1000000007; void run()throws java.lang.Exception { int n = in.nextInt(); TreeMap<Integer,Integer> m = new TreeMap<Integer,Integer>(); for(int i=0;i<n;i++) { int temp = in.nextInt(); if(m.containsKey(temp)) m.put(temp,m.get(temp)+1); else m.put(temp,1); } int cnt =0; while(m.size()!=0) { int height = 0; Set<Integer> q= new TreeSet<Integer>(); for(int x : m.keySet()) q.add(x); //out.println(q); for(int x : q) { while(x>=height) { int val = m.get(x); if(val==0) { m.remove(x); break; } else { m.put(x,val-1); height++; } } if(m.containsKey(x) && m.get(x)==0) m.remove(x); } cnt++; } out.println(cnt); } } /* nextInt() nextLong(); nextString() nextLine() nextCharacter()//reads non whitespace char nextDouble() nextBigInteger() */ class FastReader{ private boolean finished = false; private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public FastReader(InputStream stream){ this.stream = stream; } public int read(){ if (numChars == -1){ throw new InputMismatchException (); } if (curChar >= numChars){ curChar = 0; try{ numChars = stream.read (buf); } catch (IOException e){ throw new InputMismatchException (); } if (numChars <= 0){ return -1; } } return buf[curChar++]; } public int peek(){ if (numChars == -1){ return -1; } if (curChar >= numChars){ curChar = 0; try{ numChars = stream.read (buf); } catch (IOException e){ return -1; } if (numChars <= 0){ return -1; } } return buf[curChar]; } public int nextInt(){ int c = read (); while (isSpaceChar (c)) c = read (); int sgn = 1; if (c == '-'){ sgn = -1; c = read (); } int res = 0; do{ if(c==','){ c = read(); } if (c < '0' || c > '9'){ throw new InputMismatchException (); } res *= 10; res += c - '0'; c = read (); } while (!isSpaceChar (c)); return res * sgn; } public long nextLong(){ int c = read (); while (isSpaceChar (c)) c = read (); int sgn = 1; if (c == '-'){ sgn = -1; c = read (); } long res = 0; do{ if (c < '0' || c > '9'){ throw new InputMismatchException (); } res *= 10; res += c - '0'; c = read (); } while (!isSpaceChar (c)); return res * sgn; } public String nextString(){ int c = read (); while (isSpaceChar (c)) c = read (); StringBuilder res = new StringBuilder (); do{ res.appendCodePoint (c); c = read (); } while (!isSpaceChar (c)); return res.toString (); } public boolean isSpaceChar(int c){ if (filter != null){ return filter.isSpaceChar (c); } return isWhitespace (c); } public static boolean isWhitespace(int c){ return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0(){ StringBuilder buf = new StringBuilder (); int c = read (); while (c != '\n' && c != -1){ if (c != '\r'){ buf.appendCodePoint (c); } c = read (); } return buf.toString (); } public String nextLine(){ String s = readLine0 (); while (s.trim ().length () == 0) s = readLine0 (); return s; } public String nextLine(boolean ignoreEmptyLines){ if (ignoreEmptyLines){ return nextLine (); }else{ return readLine0 (); } } public BigInteger nextBigInteger(){ try{ return new BigInteger (nextString ()); } catch (NumberFormatException e){ throw new InputMismatchException (); } } public char nextCharacter(){ int c = read (); while (isSpaceChar (c)) c = read (); return (char) c; } public double nextDouble(){ int c = read (); while (isSpaceChar (c)) c = read (); int sgn = 1; if (c == '-'){ sgn = -1; c = read (); } double res = 0; while (!isSpaceChar (c) && c != '.'){ if (c == 'e' || c == 'E'){ return res * Math.pow (10, nextInt ()); } if (c < '0' || c > '9'){ throw new InputMismatchException (); } res *= 10; res += c - '0'; c = read (); } if (c == '.'){ c = read (); double m = 1; while (!isSpaceChar (c)){ if (c == 'e' || c == 'E'){ return res * Math.pow (10, nextInt ()); } if (c < '0' || c > '9'){ throw new InputMismatchException (); } m /= 10; res += (c - '0') * m; c = read (); } } return res * sgn; } public boolean isExhausted(){ int value; while (isSpaceChar (value = peek ()) && value != -1) read (); return value == -1; } public String next(){ return nextString (); } public SpaceCharFilter getFilter(){ return filter; } public void setFilter(SpaceCharFilter filter){ this.filter = filter; } public interface SpaceCharFilter{ public boolean isSpaceChar(int ch); } } class Obj implements Comparable<Obj>{ char c; int size; public Obj(char c, int size){ this.c = c; this.size = size; } public int compareTo(Obj that){ if(this.size>that.size){ return -1; }else if(this.size<that.size){ return 1; }else{ if(this.c < that.c){ return -1; }else if(this.c > that.c){ return 1; }else{ return 0; } } } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Stack; import java.util.StringTokenizer; public class Main { /** * @param args */ public static void main(String[] args) { FastScanner fs = new FastScanner(); int n = fs.nextInt(); ArrayList<Stack<Integer>> pile = new ArrayList<Stack<Integer>>(); pile.add(new Stack<Integer>()); int[] arr = new int[n]; for(int i=0;i<n;i++) { arr[i] = fs.nextInt(); } Arrays.sort(arr); pile.get(0).push(arr[0]); for(int i=1;i<n;i++) { boolean pushed = false; for(Stack<Integer> stack : pile) { if(stack.peek()<=arr[i] && stack.size()<=arr[i]) { stack.push(arr[i]); pushed = true; break; } } if(!pushed) { Stack<Integer> st = new Stack<Integer>(); st.push(arr[i]); pile.add(st); } } System.out.println(pile.size()); } } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int n, a[102] = {0}, i, k; cin >> n; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (k = 1; k <= n; k++) { bool ans = true; for (i = 0; i < n; i++) { ans = ans && (a[i] >= (i / k)); } if (ans) break; } cout << k; return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import math n=int(input()) box=[int(x) for x in input().split()] box.sort() k=0 for i in range(n): if k*(box[i]+1)<=i: k+=1 print(k)
PYTHON3
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; int fx[] = {0, 1, 1, 1, 0, -1, -1, -1}; int fy[] = {1, 1, 0, -1, -1, -1, 0, 1}; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, i, j, k, l, f = 0; cin >> n; int ara[n]; for (i = 0; i < n; i++) { cin >> ara[i]; } sort(ara, ara + n); vector<stack<int> > v; for (i = 0; i < n; i++) { f = 0; for (j = 0; j < v.size(); j++) { if (v[j].size() <= ara[i]) { v[j].push(ara[i]); f = 1; break; } } if (f == 0) { stack<int> st; st.push(ara[i]); v.push_back(st); st.pop(); } } cout << v.size() << endl; return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
def arr(): return map(int,raw_input().split()) n=int(raw_input()) a=arr() a=sorted(a) count=0 piles=0 flag=True while flag: flag=False count=0 for i in range(n): if a[i]==-1: continue if a[i]>=count: count+=1 a[i]=-1 else: flag=True piles+=1 print piles ##def main(): ## n=int(raw_input()) ## a=[] ## for i in range(n): ## a.append(list(raw_input())) ## ## for i in range(n): ## if '#' in a[i]: ## for j in range(n): ## if a[i][j]=='#': ## a[i][j]='.' ## if i+2<n and a[i+1][j-1:j+2] == ['#']*3 and a[i+2][j]=='#': ## a[i+1][j-1]='.' ## a[i+1][j]='.' ## a[i+1][j+1]='.' ## #list(a[:j-1]+'.'*3+a[j+2:]) ## a[i+2][j]='.' ## else: ## #for k in a: ## # print k ## print 'NO' ## return 0 ## print 'YES' ## return 1 ##main() ##def gcd(a, b): ## while b: ## a, b = b, a % b ## return a ## ##def lcm(a, b): ## return a * b // gcd(a, b) ## ##def lcmm(*args): ## return reduce(lcm, args) ##n=int(raw_input()) ##a=arr() ###a=sorted(a) ###print a ###print lcm ##print n*reduce(gcd,a)
PYTHON
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; int a[101], vis[101] = {0}; int main() { ios_base::sync_with_stdio(0); ; int i, n; cin >> n; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); int ans = 0, boxes = 0; while (boxes < n) { ans++; int c = 0; for (i = 0; i < n; i++) { if (a[i] >= c && vis[i] == 0) { boxes++; c++; vis[i] = 1; } } } cout << ans; return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import heapq #br = open('a.in') f = lambda: map(int, raw_input().strip().split()) n = f()[0] a = sorted(f()) b = [] for i in a: h = 0 for j, k in enumerate(b): if k <= i: b[j] += 1 h = 1 break if h == 0: heapq.heappush(b, 1) print len(b)
PYTHON
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.*; public class TaskC { Scanner in; PrintWriter out; int n; ArrayList<Integer> elements = new ArrayList<Integer>(); int ans; public static void main(String[] args) { TaskC mainTest = new TaskC(); mainTest.readData(); mainTest.solve(); mainTest.outputData(); } public TaskC() { in = new Scanner(System.in); out = new PrintWriter(new OutputStreamWriter(System.out)); } public void readData() { n = in.nextInt(); for (int i = 0; i < n; i++) { elements.add(in.nextInt()); } } public void solve() { ans = 0; Collections.sort(elements); while (!elements.isEmpty()) { ans++; ArrayList<Integer> copy = new ArrayList<Integer>(); int cnt = 0; for (int i = 0; i < elements.size(); i++) { if (elements.get(i) >= cnt) { cnt++; } else { copy.add(elements.get(i)); } } elements = copy; } } public void outputData() { System.out.println(ans); } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class Main2 { public static void main(String args[]) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String[] s = br.readLine().split(" "); int[] a = new int[n]; for(int i = 0 ; i < n ; i++){ a[i] = Integer.parseInt(s[i]); } Arrays.sort(a); int count = 0; int pile = 0; while(count != n){ int stdc = 0; for(int i = 0 ; i < n ; i++){ if(a[i] != -1 && stdc <= a[i]){ a[i] = -1; count++; stdc++; } } pile++; } System.out.println(pile); } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.awt.*; import java.io.*; import java.util.*; public class Main { static Main.MyScanner sc = new Main.MyScanner(); static PrintWriter out = new PrintWriter(System.out); // static PrintStream out = System.out; public static void main(String[] args) { int n = sc.nextInt() ; PriorityQueue<Integer> pq = new PriorityQueue<Integer>(),temp = new PriorityQueue<Integer>() ; for(int i = 0 ; i < n ; i++) pq.add(sc.nextInt()) ; int ans = 0 ; while (!pq.isEmpty()) { int count = 0 ; while (!pq.isEmpty()) { int now = pq.poll() ; if(count > now) temp.add(now) ; else count++ ; } ans++ ; pq.addAll(temp) ; temp.clear(); } out.print(ans); out.close(); } private static String next(String time) { int h = Integer.parseInt(time.charAt(0)+""+time.charAt(1)),m = Integer.parseInt(time.charAt(2)+""+time.charAt(3)) ; m++ ; if(m > 59) { h++ ; m = 0 ; } String ans = "" ; if(h < 10) ans += "0" ; ans += h ; if(m < 10) ans += "0" ; ans += m ; return ans ; } private static boolean isPalin(String s) { if(s.charAt(0) == s.charAt(3) && s.charAt(1) == s.charAt(2)) return true ; return false ; } private static int get(char a, char b) { if( a > b) { char temp = a; a = b; b = temp ; } return Math.min(b-a, 26 - (b-a) ) ; } static private class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } public int mod(long x) { // TODO Auto-generated method stub return (int) (x % 1000000007); } public int mod(int x) { return x % 1000000007; } boolean hasNext() { if (st.hasMoreElements()) return true; try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.hasMoreTokens(); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public long nextLong() { return Long.parseLong(next()); } } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; long long int pile[1000001], a[1000001], n; int ok(long long int x) { memset(pile, 0, sizeof pile); if (n <= x) return 1; for (long long int i = 1; i <= x; i++) pile[i] = a[i]; for (long long int i = x + 1; i <= n; i++) { long long int no; no = i % x; if (!no) no = x; if (!pile[no]) return 0; pile[no]--; pile[no] = min(pile[no], a[i]); } return 1; } int main() { long long int lo, mid, hi, i; cin >> n; for (i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + n + 1); reverse(a + 1, a + n + 1); lo = 0; hi = 10000000; while (hi - lo > 1) { mid = (lo + hi) / 2; if (ok(mid)) hi = mid; else lo = mid; } cout << hi; return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.InputMismatchException; public class B { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub InputReader s = new InputReader(System.in); PrintWriter p = new PrintWriter(System.out); int n = s.nextInt(); ArrayList<Integer> list = new ArrayList<>(); int ans = 1; for (int i = 0; i < n; i++) list.add(s.nextInt()); Collections.sort(list); int curr = 1; for (int i = 1; i < n; i++) { int least = i / curr; if (list.get(i) < least) curr++; } p.println(curr); p.flush(); p.close(); } public static int search(ArrayList<Integer> list, int val) { int low = 0; int high = list.size() - 1; int ans = -1; while (low <= high) { int mid = (low + high) / 2; if (list.get(mid) < val) { ans = Math.max(ans, mid); low = mid + 1; } else { high = mid - 1; } } return ans; } public static int search2(ArrayList<Integer> list, int val) { int low = 0; int high = list.size() - 1; int ans = -1; while (low <= high) { int mid = (low + high) / 2; if (list.get(mid) <= val) { ans = Math.max(ans, mid); low = mid + 1; } else { high = mid - 1; } } return ans; } public static long GCD(long a, long b) { if (a == 0) return b; return GCD(b % a, a); } public static long LCM(long a, long b) { return (a * b) / GCD(a, b); } static class pair implements Comparable<pair> { Integer x, y; pair(int x, int y) { this.x = x; this.y = y; } public int compareTo(pair o) { int result = x.compareTo(o.x); if (result == 0) result = y.compareTo(o.y); return result; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x - x == 0 && p.y - y == 0; } return false; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); } } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class CodeX { public static void sort(long arr[]) { merge_sort(arr, 0, arr.length - 1); } private static void merge_sort(long A[], long start, long end) { if (start < end) { long mid = (start + end) / 2; merge_sort(A, start, mid); merge_sort(A, mid + 1, end); merge(A, start, mid, end); } } private static void merge(long A[], long start, long mid, long end) { long p = start, q = mid + 1; long Arr[] = new long[(int) (end - start + 1)]; long k = 0; for (int i = (int) start; i <= end; i++) { if (p > mid) Arr[(int) k++] = A[(int) q++]; else if (q > end) Arr[(int) k++] = A[(int) p++]; else if (A[(int) p] < A[(int) q]) Arr[(int) k++] = A[(int) p++]; else Arr[(int) k++] = A[(int) q++]; } for (int i = 0; i < k; i++) { A[(int) start++] = Arr[i]; } } } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import math n=int(input()) arr=[int(x) for x in input().split()] arr.sort() k=0 for i in range(n): if k*(arr[i]+1)<=i: k+=1 print(k)
PYTHON3
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; bool cmp(int a, int b) { return a > b; } int m[200], n; int mx[200]; bool isok(int r) { for (int i = 0; i < r; ++i) mx[i] = m[i]; int j = 0; for (int i = r; i < n; ++i) { if (mx[j]) { --mx[j]; mx[j] = min(mx[j], m[i]); ++j; if (j == r) j = 0; } else return false; } return true; } int main() { cin >> n; for (int i = 0; i < n; ++i) cin >> m[i]; sort(m, m + n, cmp); int L = 0, R = n; while (L < R) { int mid = (L + R) / 2; if (isok(mid)) R = mid; else L = mid + 1; } cout << L << endl; return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
n=int(input()) a=[int(x) for x in input().split()] a.sort() x=[] for i in range(n): c=0 for j in range(len(x)): if x[j]<=a[i]: c=1 x[j]+=1 break if not c: x.append(1) #print(x) print(len(x))
PYTHON3
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> int main(void) { int n; scanf("%d", &n); int x[101] = {0}; for (int i = 0, temp; i < n; i++) { scanf("%d", &temp); x[temp]++; } int ans = x[0], remain = 0; for (int i = 1; i <= 100; i++) { remain += ans; if (x[i] <= remain) remain -= x[i]; else { int diff = x[i] - remain; if (diff % (i + 1) == 0) { ans += diff / (i + 1); remain = 0; } else { ans += (diff / (i + 1)) + 1; remain += (i + 1) * ((diff / (i + 1)) + 1); remain -= x[i]; } } } printf("%d\n", ans); return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
# coding: utf-8 n = int(raw_input()) values = [int(s) for s in raw_input().split()] values.sort() used = [False for _ in range(n)] res = 0 for i in range(n): if used[i]: continue total = 1 used[i] = True res += 1 for k in range(n-i): j = k+i if used[j]: continue if values[j] >= total: used[j] = True total += 1 print res
PYTHON
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.util.*; public class C{ public static void main(String... args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); ArrayList<Integer> list = new ArrayList<Integer>(); while(n-->0) list.add(sc.nextInt()); int pile = 0; while(list.size()>0){ pile++; Collections.sort(list); list.remove(0); int value = 1; for(int i=0;i<list.size();i++){ if(list.get(i)>=value){ list.remove(i); value++; i--; } } } System.out.print(pile); } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; const long long BIGER = 1000000000000000; const int BIG = 1000000000; int n; int a[101]; int f[101]; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } sort(a + 1, a + n + 1); int s = 0; int g = 0; int t = 0; while (s < n) { t = 0; for (int i = 1; i <= n; i++) { if (a[i] >= t && f[i] == 0) { f[i] = 1; t++; s++; } } g++; } cout << g; return 0; }
CPP
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.io.*; import java.math.BigDecimal; import java.math.BigInteger; import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Solution { static PrintWriter fop = new PrintWriter(System.out); public static void main(String[] args) { FastScanner fsca = new FastScanner(); int n = fsca.nextInt(); int a[] = fsca.readArray(n) ; ruffleSort(a); int temp = 0 ; int cnt = 0 ; while (temp < n){ cnt++ ; int curr = 0 ; for (int i = 0; i <n ; i++) { if (a[i] >= curr){ a[i] = -1 ; curr++ ; temp++ ; } } } System.out.println(cnt); fop.flush(); fop.close(); } /*-----------------------------------------------------------------------------------------------------------------------------------------------*/ static long gcd(long a, long b) { return (b == 0) ? a : gcd(b, a % b); } ; static int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } static final Random random = new Random(); static void ruffleSort(int[] a) { int n = a.length;//shuffle, then sort for (int i = 0; i < n; i++) { int oi = random.nextInt(n), temp = a[oi]; a[oi] = a[i]; a[i] = temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { int n = a.length;//shuffle, then sort for (int i = 0; i < n; i++) { int oi = random.nextInt(n); long temp = a[oi]; a[oi] = a[i]; a[i] = temp; } Arrays.sort(a); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[][] readMat(int n, int m) { int a[][] = new int[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) a[i][j] = nextInt(); return a; } char[][] readCharMat(int n, int m) { char a[][] = new char[n][m]; for (int i = 0; i < n; i++) { String s = next(); for (int j = 0; j < m; j++) a[i][j] = s.charAt(j); } return a; } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } static void print(int a[], int n) { for (int i = 0; i < n; i++) fop.append((a[i]) + " "); // fop.append("\n") ; } static void print(long a[], int n) { for (int i = 0; i < n; i++) fop.append((a[i]) + " "); // fop.append("\n") ; } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.util.*; import java.io.*; public class C { Reader in; PrintWriter out; int i = 0, j = 0; void solve() { //START// int n = in.nextInt(); int freq[] = new int[101]; int cur = 0; for (i = 0; i < n; i++) { cur = in.nextInt(); freq[cur]++; } int stacks = 0; int left = n; while (left > 0) { int currentBooks = 0; for (i = 0; i < 101; i++) { if (freq[i] > 0) { while (currentBooks <= i && freq[i] > 0) { freq[i]--; left--; currentBooks++; } } } stacks++; } out.println(stacks); //END } void runIO() { in = new Reader(); out = new PrintWriter(System.out, false); solve(); out.close(); } public static void main(String[] args) { new C().runIO(); } // input/output static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public final String next() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.append((char) c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int nextInt() { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') while ((c = read()) >= '0' && c <= '9') ret += (c - '0') / (div *= 10); if (neg) return -ret; return ret; } public int[] readIntArray(int size) { int[] arr = new int[size]; for (int i = 0; i < size; i++) arr[i] = nextInt(); return arr; } public long[] readLongArray(int size) { long[] arr = new long[size]; for (int i = 0; i < size; i++) arr[i] = nextInt(); return arr; } private void fillBuffer() { try { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); } catch (IOException e) { } if (bytesRead == -1) buffer[0] = -1; } private byte read() { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.util.*; import static java.util.Arrays.*; import static java.lang.Math.*; public class C { Scanner sc = new Scanner(System.in); void doIt() { int n = sc.nextInt(); int [] x = new int[n]; boolean [] used = new boolean[n]; // false for(int i = 0; i < n; i++) x[i] = sc.nextInt(); sort(x); int cnt = 0; int ans = 0; while(cnt < n) { int pile = 0; for(int i = 0; i < n; i++) { if(used[i] == false && x[i] >= pile) { used[i] = true; pile++; cnt++; } } ans++; } System.out.println(ans); } /** * @param args */ public static void main(String[] args) { new C().doIt(); } }
JAVA
389_C. Fox and Box Accumulation
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. <image> 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. Examples 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 Note 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. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
n = int(raw_input()) f = raw_input().split(' ') for i in range(0, n): f[i] = int(f[i]) f.sort() ans = 0; while(len(f)): c = 0 i = 0 while(True): if i >= len(f): break; if f[i] >= c: f.pop(i) c = c + 1 else: i = i + 1 ans = ans + 1 print ans
PYTHON