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
import static java.lang.Math.*; import static java.util.Arrays.*; import static java.lang.System.out; import static java.util.Collections.*; import java.io.*; import java.math.*; import java.util.*; public class Main { static boolean LOCAL = System.getSecurityManager() == null; Scanner in = new Scanner(System.in); void run() { // while (in.hasNext()) solve(); } void solve() { int n = in.nextInt(); int[] is = new int[n]; for (int i = 0; i < n; i++) is[i] = in.nextInt(); sort(is); boolean[] vis = new boolean[n]; int ans = 0; for (int i = 0; i < n; i++) if (!vis[i]) { ans++; vis[i] = true; int k = 1; for (int j = i + 1; j < n; j++) if (!vis[j]) { if (is[j] >= k) { vis[j] = true; k++; } } } out.println(ans); } void debug(Object... os) { System.err.println(deepToString(os)); } public static void main(String[] args) { if (LOCAL) { try { System.setIn(new FileInputStream("./../../in.txt")); // System.setOut(new PrintStream("./../../out")); } catch (Throwable e) { LOCAL = false; } } long start = 0; if (LOCAL) start = System.nanoTime(); new Main().run(); if (LOCAL) System.err.printf("[Time : %.6f s]%n", (System.nanoTime() - start) * 1e-9); } } class Scanner { BufferedReader br; StringTokenizer st; Scanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); eat(""); } void eat(String s) { st = new StringTokenizer(s); } String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } boolean hasNext() { while (!st.hasMoreTokens()) { String s = nextLine(); if (s == null) return false; eat(s); } return true; } String next() { hasNext(); return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(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 n, a[101]; void scan() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n); } void out() { int ans = 0, k = 0, c; while (1) { c = 0; for (int i = 0; i < n; i++) { if (a[i] != -1) { if (a[i] >= k) { ++k; a[i] = -1; } } else if (a[i] == -1) ++c; } if (c == n) break; ++ans; k = 0; } cout << ans << '\n'; } int main() { scan(); out(); 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, i, j, sum, p[110], q[110]; while (cin >> n) { sum = 0; for (i = 0; i < n; i++) cin >> p[i]; sort(p, p + n); memset(q, 0, sizeof(q)); for (i = 0; i < n; i++) if (!q[i]) { sum++; int ans = 0; for (j = 0; j < n; j++) { if (q[j]) continue; if (p[j] >= ans) { q[j] = 1; ans++; } } } cout << sum << endl; } }
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 class A { public void solve() { int n = in.nextInt(); int[] x = new int[n]; for (int i = 0; i < n; i++) { x[i] = in.nextInt(); } Arrays.sort(x); List<Integer> curW = new ArrayList<>(); for (int i = 0; i < n; i++) { int min = Integer.MAX_VALUE; for (int j : curW) { min = Math.min(min, j); } if (min <= x[i]) { curW.remove((Integer) min); curW.add(min + 1); } else { curW.add(1); } } out.println(curW.size()); } FastScanner in; PrintWriter out; public void run() { try { in = new FastScanner(); out = new PrintWriter(System.out); solve(); out.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastScanner(String name) { try { br = new BufferedReader(new FileReader(name)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } } public static void main(String[] args) { new A().run(); } }
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; void solve() { int n; cin >> n; map<int, int> m; for (int i = 0; i < n; i++) { int x; cin >> x; m[x]++; } int cnt = 0; while (!m.empty()) { cnt++; int tmp = 1; int val = m.begin()->first; m[val]--; if (m[val] == 0) m.erase(val); while (!m.empty()) { auto it = m.lower_bound(tmp); if (it == m.end()) { break; } val = it->first; m[val]--; if (m[val] == 0) m.erase(val); tmp++; } } cout << cnt << '\n'; } 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
n = input() h = [0] A = list(map(int, input().split())) A.sort() for x in A: if x < min(h): h += [1] else: h[h.index(min(h))]+=1 print(len(h))
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; template <class T> inline bool read(T &x) { int c = getchar(); int sgn = 1; while (~c && c<'0' | c> '9') { if (c == '-') sgn = -1; c = getchar(); } for (x = 0; ~c && '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0'; x *= sgn; return ~c; } struct debugger { template <typename T> debugger &operator,(const T &v) { cerr << v << " "; return *this; } } dbg; template <class T> void __stl_print__(T &x) { cerr << "["; for (__typeof((x).end()) i = (x).begin(); i != (x).end(); ++i) cerr << (i != x.begin() ? ", " : "") << *i; cerr << "]" << endl; } template <class T, class U> inline T max(T &a, U &b) { return a > b ? a : b; } template <class T, class U> inline T min(T &a, U &b) { return a < b ? a : b; } template <class T, class U> inline T swap(T &a, U &b) { T tmp = a; a = b; b = tmp; } const long long INF = (1ll) << 50; const int mx = 1e5 + 7; const int mod = 1000000007; const double pi = 2 * acos(0.0); int EQ(double d) { if (fabs(d) < 1e-7) return 0; return d > 1e-7 ? 1 : -1; } int arr[102]; bool flag[102]; int main() { int n = ({ int a; read(a); a; }); for (__typeof((n)-1) i = (0); i <= ((n)-1); ++i) arr[i] = ({ int a; read(a); a; }); sort(arr, arr + n); int cnt = 0; for (__typeof((n)-1) i = (0); i <= ((n)-1); ++i) { if (flag[i]) continue; flag[i] = 1; cnt++; int take = 1; for (__typeof(n - 1) j = (i + 1); j <= (n - 1); ++j) { if (!flag[j] && arr[j] >= take) { take++; flag[j] = 1; } } } cout << cnt << endl; }
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; cin >> n; int box[101]; for (int i = 0; i < n; i++) cin >> box[i]; sort(box, box + n); bool used[101]; memset(used, false, sizeof(used)); int ai = 0, count = 0, piles = 0; for (int i = 0; i < n; i++) { if (!used[i]) { ai = box[i]; count = 1; used[i] = true; for (int j = i + 1; j < n; j++) { if (box[j] >= count && !used[j]) { used[j] = true; count++; } } piles++; } } 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 arr[2000], cap[2000]; int main() { int a, b, c, i, j, k, n, m; while (scanf("%d", &n) != EOF) { for (i = 0; i < n; i++) scanf("%d", &arr[i]); sort(arr, arr + n); for (i = 0; i < n; i++) cap[i] = 0; m = 0; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if (cap[j] <= arr[i]) { cap[j]++; break; } } if (j == m) { cap[m] = 1; m++; } } printf("%d\n", m); } 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(inp): n = int(inp()) arr = split_inp_int(inp) c = Counter(arr) num_of_pile = 0 box_built = 0 while box_built < n: current_height = 0 for i in range(101): while i >= current_height and i in c and c[i] > 0: c[i] -= 1 box_built += 1 current_height += 1 num_of_pile += 1 print(num_of_pile) def split_inp_int(inp): return list(map(int, inp().split())) def use_fast_io(): import sys class InputStorage: def __init__(self, lines): lines.reverse() self.lines = lines def input_func(self): if self.lines: return self.lines.pop() else: return "" input_storage_obj = InputStorage(sys.stdin.readlines()) return input_storage_obj.input_func from collections import Counter, defaultdict from functools import reduce import operator import math def product(arr_): return reduce(operator.mul, arr_, 1) if __name__ == "__main__": main(use_fast_io())
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; template <class _T> inline string tostr(const _T& a) { ostringstream os(""); os << a; return os.str(); } int in[110]; int used[110]; int main() { int n; while (cin >> n) { fill_n(used, n, 0); for (int(i) = (0); (i) < (n); ++(i)) { scanf("%d", in + i); } sort(in, in + n); int resp = 0; while (true) { int pile = 0; for (int(i) = (0); (i) < (n); ++(i)) { if (used[i]) continue; if (in[i] >= pile) { used[i] = 1; pile++; } } if (pile == 0) break; else resp++; } cout << resp << 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, a[101]; bool vis[101]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); sort(a + 1, a + n + 1); int ans = 0; for (int i = 1; i <= n; i++) if (!vis[i]) { ans++; int now = 1; vis[i] = 1; for (int j = i + 1; j <= n; j++) if (!vis[j]) { if (a[j] >= now) { vis[j] = 1; now++; } } } 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
#include <bits/stdc++.h> using namespace std; int main(void) { int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } sort(arr, arr + n); int ans = 1; int height; for (int i = 1; i < n; i++) { height = i / ans; while (arr[i] < height) ans++, height = i / 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
import bisect n = int(input()) xi = list(sorted(map(int, input().split()))) s = set(xi) li = [] while len(xi) > 0: li.append([xi.pop(0)]) i = 0 while i < len(xi): if xi[i] >= len(li[-1]): li[-1].append(xi.pop(i)) else: i += 1 # print(li) print(len(li))
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 FoxAndBoxAccumulation { public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); int n, t; int[] boxes; ArrayList<LinkedList<Integer>> piles = new ArrayList<LinkedList<Integer>>(); for (int i = 0; i < 1; i++) { n = in.nextInt(); boxes = new int[n]; for ( t = 0; t < n; t++) boxes[t]=in.nextInt(); Arrays.sort(boxes); piles.clear(); for ( t = 0; t < 100; t++) piles.add(new LinkedList<Integer>()); for (int j = 0; j < n; j++) if(boxes[j] >= piles.get(0).size()) piles.get(0).add(boxes[j]); else for ( t = 1; ; t = (t+1)%piles.size()) if(boxes[j] >= piles.get(t).size()){ piles.get(t).add(boxes[j]); break; } for (t = 0; t < piles.size(); t++) if(piles.get(t).size() == 0) break; System.out.println(t); } } }
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=sorted(list(map(int,input().split()))) piles=[None]*100 for i in range(1,100): piles[i]=[] piles[0]=[l[0]] i=1 for j in range(1,n): flag=0 for k in range(0,i): if l[j] >= len(piles[k]): piles[k].append(l[j]) flag=1 break if flag==0: piles[i]=[l[j]] i+=1 print(i) #print(piles[:i])
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
input();x=sorted(map(int,input().split()));print(max([((ind+1)//(i+1)+((ind+1)%(i+1)!=0)) for ind,i in enumerate(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
n = int(input()) a = list(map(int, input().split())) a.sort() counter = 0 while len(a) != 0: numbers = {-1} for elements in a: numbers.add(elements) numbers.remove(-1) for values in numbers: a.remove(values) j = 0 s = 0 for values in numbers: if len(a) == 0: break for x in range(values-s): if len(a) == 0: break i = 0 while i < len(a): if a[i] >= j : a.remove(a[i]) j +=1 break else: i +=1 j += 1 s = j counter += 1 print(counter)
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.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; public class Solution { static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } 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; } } static void print(Object s){ System.out.print(s); } static void println(Object s){ System.out.println(s); } public static void main(String[] args) { FastReader sc = new FastReader(); 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); } print(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; struct pairvar { int a; int b; int c; int max; }; bool isPrime(long long x) { if (x == 1) { return false; } long long a = (long long)x; for (int i = 2; i <= (long long)sqrt((double)a); i++) { if (a % i == 0) { return false; } } return true; } int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); } long long factorial(long long x) { long long sum = 1; for (long long i = 1; i <= x; i++) { sum *= i; } return sum; } string tostr(int x) { string s = ""; while (x != 0) { s += (x % 10) + '0'; x /= 10; } string t = ""; for (int i = s.length() - 1; i >= 0; i--) { t += s[i]; } return t; } bool cmp(const pairvar &a, const pairvar &b) { if (a.a == b.a) return a.b < b.b; return a.a < b.a; } int lcm(int a, int b) { return (a * b) / gcd(a, b); } int a[1000]; int mark[1000]; int main() { int n; cin >> n; a[n]; mark[n]; for (int i = 0; i < n; i++) { cin >> a[i]; mark[i] = 0; } int ans = 0; sort(a, a + n); for (int i = 0; i < n; i++) { if (mark[i] == 0) { ans++; mark[i] = 1; int j = 1; for (int e = 0; e < n; e++) { if (mark[e] == 0) { if (j <= a[e]) { j++; mark[e] = 1; } } } } } cout << ans; }
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 n, m, x, y, z, k, sol, sum, ans, l, r, xx, yy, a[1000000], b[1000000]; vector<long long> v; vector<long long> v1; vector<pair<long long, long long>> v2; pair<long long, pair<long long, long long>> pp[1000000]; pair<long long, long long> p[1000000]; map<long long, long long> ma; string s1, s2, s; char c; bool ok(long long k) { for (int i = 0; i < k; i++) { long long num = v[i]; for (int q = i + k; q < n; q += k) { if (num == 0) { return 0; } num = min(num - 1, v[q]); } } return 1; } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> x; v.push_back(x); } sort(v.begin(), v.end()); reverse(v.begin(), v.end()); long long mid = 0, l = 1, r = n; while (l <= r) { mid = (l + r) / 2; if (ok(mid)) { sol = mid; r = mid - 1; } else { l = mid + 1; } } cout << sol << 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 a[105]; int cnt[105]; int find(int x) { for (int i = x - 1; i >= 0; i--) if (cnt[i]) return i; return -1; } int main() { int n; memset(cnt, 0, sizeof cnt); scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); cnt[a[i]]++; } bool isok = false; sort(a + 1, a + 1 + n); int ans = 0; for (int i = 1; i <= n; i++) { if (a[i] == -1) continue; int pos = 1; a[i] = -1; for (int j = 1; j <= n; j++) { if (a[j] >= pos) { a[j] = -1; pos++; } } ans++; } 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
#include <bits/stdc++.h> using namespace std; void solve2() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } sort(a.begin(), a.end()); ; vector<bool> use(n, false); int lo = 0; int ans = 0; while (lo < n) { ans++; int top = 0; for (int i = 0; i < n; ++i) { if (!use[i] && a[i] >= top) { top++; use[i] = true; lo++; } } } cout << ans << "\n"; } void solve() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } sort(a.begin(), a.end()); vector<int> cnt(n); for (auto first : a) { for (int i = 0; i < n; ++i) { if (cnt[i] <= first) { cnt[i]++; break; } } }; int ans = 0; for (int i = 0; i < n; ++i) { if (cnt[i] != 0) { ans++; } } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); 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 math def ok(pile): for i in range(len(pile)): if(len(pile) - i-1 > pile[i]): return False return True def solve(sbox, p): ap = []; for i in range(p): pile = [sbox[0]] sbox.pop(0); ap.append(pile); currentPile = 0; full = [False]*p while sbox != []: ap[currentPile].append(sbox[0]) if(not ok(ap[currentPile])): ap[currentPile].pop(); full[currentPile] = True; else: sbox.pop(0); if(sbox == []): return True; if(reduce(lambda x, y: x and y, full)): return False; currentPile += 1; currentPile %= p; while(full[currentPile]): currentPile += 1; currentPile %= p; return True; n = input() boxes = sorted(map(int, raw_input().split()), reverse = True); for i in range(1,n+1): if(solve(list(boxes), i)): print i 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
from sys import stdin inp = [l.split() for l in stdin.readlines()] p = [int(a) for a in inp[1]] p = sorted(p) s = [0]*(len(p)+5) #print [i for i in p] for i in range(len(p)): for j in range(len(p)): if p[i] >= s[j]: s[j] +=1 break print s.index(0)
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.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.ListIterator; import java.util.PriorityQueue; import java.util.Set; import java.util.StringTokenizer; import java.util.TreeSet; public class CopyOfCopyOfSolution { public static void main(String[] args) throws NumberFormatException, IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); int n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } Arrays.sort(arr); int[] count = new int[n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (count[j] <= arr[i]) { count[j]++; break; } } } int c = 0; for (int i = 0; i < n; i++) { if (count[i] > 0) { c++; } } out.println(c); out.close(); } private static void print(int[][] m) { // TODO Auto-generated method stub int n = m.length; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.print(m[i][j]); } System.out.println(); } } private static boolean mark(int[][] m, int a, int b) { if (m[a][b] == 0 || m[a][b] == 2) { return true; } // a and b are topmost m[a][b] = 2; if (b < m.length - 1 && a < m.length - 2 && m[a + 1][b + 1] == 1 && m[a + 1][b] == 1 && b > 0 && m[a + 1][b - 1] == 1 && m[a + 2][b] == 1) { m[a + 1][b + 1] = 2; m[a + 1][b] = 2; m[a + 1][b - 1] = 2; m[a + 2][b] = 2; return true; } // Left most if (b < m.length - 2 && a > 0 && a < m.length - 1 && m[a][b + 1] == 1 && m[a][b + 2] == 1 && m[a + 1][b + 1] == 1 && m[a - 1][b + 1] == 1) { m[a][b + 1] = 2; m[a][b + 2] = 2; m[a - 1][b + 1] = 2; m[a + 1][b + 1] = 2; return true; } return false; } private static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); 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()); } } }
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; int a[100 + 10]; bool used[100 + 10]; map<int, int> cc; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); int rs = 0; for (int i = 0; i < n; i++) { if (used[i]) continue; rs++; int k = 1; for (int j = i + 1; j < n; j++) { if (not used[j] and a[j] >= k) { used[j] = true; k++; } } } cout << rs << "\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
import static java.lang.System.in; import static java.lang.System.out; import java.io.*; import java.util.*; public class Main { static final double EPS = 1e-10; static final double INF = 1 << 31; static final double PI = Math.PI; public static Scanner sc = new Scanner(in); StringBuilder sb = new StringBuilder(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public void run() throws IOException { String input; String[] inputArray; input = br.readLine(); inputArray = input.split(" "); int []a = new int[101]; int n = Integer.valueOf(inputArray[0]); input = br.readLine(); inputArray = input.split(" "); for (int i=0; i<n; i++) a[Integer.valueOf(inputArray[i])]++; PriorityQueue<Integer> Q = new PriorityQueue<Integer>(); Q.add(0); //int ans = 0; for (int i=0; i<=100; i++) for (int j=0; j<a[i]; j++){ int x = Q.poll(); if (x<=i) Q.add(x+1); else { Q.add(1); Q.add(x); } } sb.append(Q.size()); ln(sb); } public static void main(String[] args) throws IOException { new Main().run(); } public static void ln(Object obj) { out.println(obj); } }
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.io.PrintWriter; import java.util.StringTokenizer; /** * @author Don Li */ public class FoxBoxAccumulation { void solve() { int n = in.nextInt(); int[] x = new int[n]; for (int i = 0; i < n; i++) x[i] = in.nextInt(); int[] cnt = new int[101]; for (int i = 0; i < n; i++) cnt[x[i]]++; int ans = 0; for (int i = 0; i <= 100; ) { if (cnt[i] == 0) { i++; } else { int cur = 1; cnt[i]--; for (int j = i; j <= 100; j++) { if (cnt[j] > 0 && j >= cur) { int delta = Math.min(cnt[j], j + 1 - cur); cnt[j] -= delta; cur += delta; } } ans++; } } out.println(ans); } public static void main(String[] args) { in = new FastScanner(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(System.out); new FoxBoxAccumulation().solve(); out.close(); } static FastScanner in; static PrintWriter out; static class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public 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() { int n, t; while (scanf("%d", &n) == 1) { int a[105]; bool vis[105] = {false}; memset(a, 0, sizeof(a)); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } sort(a, a + n); int ans = 0; for (int i = 0; i < n; i++) { if (!vis[i]) { vis[i] = true; ans++; int cnt = 1; for (int j = i + 1; j < n; j++) { if (!vis[j] && a[j] >= cnt) { vis[j] = true; cnt++; } } } } cout << ans << endl; } }
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
//http://codeforces.com/contest/389/problem/C import java.util.*; public class FoxBox { /* * find top box, find smallest box that can go below that, keep going until 1 stack is done * recurse on remaining list of boxes */ public static void magic(ArrayList<Integer> v, int c) { if (v.size() == 0) { System.out.println(c); return; } int count = 1; v.remove(0); ArrayList<Integer> n = new ArrayList<Integer>(); for (int i = 0; i < v.size(); i ++) { if (v.get(i) >= count) { count += 1; } else { n.add(v.get(i)); } } magic(n, c + 1); } public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); int n = in.nextInt(); ArrayList<Integer> v = new ArrayList<Integer>(); for (int i = 0; i < n; i ++) { v.add(in.nextInt()); } Collections.sort(v); magic(v, 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.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class C228 { /** * @param args */ public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } Arrays.sort(arr); ArrayList<Integer>[] lists = (ArrayList<Integer>[]) new ArrayList[101]; for(int i=0;i<101;i++){ lists[i]=new ArrayList<>(); } lists[0].add(arr[0]); for (int i = 1; i < n; i++) { for (int r = 0; r < lists.length; r++) { if (lists[r].isEmpty()) { lists[r].add(arr[i]); break; } else if (lists[r].size() <= arr[i]){ lists[r].add(arr[i]); break; } } } for(int i=0;i<101;i++){ if(lists[i].isEmpty()){ System.out.println(i); break; } } } }
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 int N = 1e5 + 5; int n, ans; multiset<int> setik; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { int x; cin >> x; setik.insert(x); } while (setik.size()) { int cur = 0; while (true) { auto it = setik.lower_bound(cur); if (it == setik.end()) break; setik.erase(it); ++cur; } ++ans; } cout << ans; }
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> int comp(const void* a, const void* b) { return *(int*)a - *(int*)b; } int main() { int n, i, j, t, count, p; int s[105]; while (scanf("%d", &n) != EOF) { t = n; for (i = 0; i < n; i++) scanf("%d", &s[i]); qsort(s, n, sizeof(int), comp); while (t) { for (i = 0, p = 0; i < n; i++) { if (s[i] < 0) continue; s[i] = -1; t--; for (j = i + 1, count = 1, p++; j < n; j++) { if (s[j] < 0) continue; if (s[j] >= count) { s[j] = -1; t--; count++; } } } } printf("%d\n", p); } 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 ans = 0; int n, tmp; bool vis[106]; int a[106]; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); vis[i] = true; } sort(a, a + n); for (int i = 0; i < n; i++) { if (vis[i]) { ans++; tmp = 1; for (int j = i + 1; j < n; j++) { if (vis[j] && a[j] >= tmp) { tmp++; vis[j] = false; } } vis[i] = false; } } 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
#include <bits/stdc++.h> using namespace std; int main() { int a[105] = {0}; int n; ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; int temp, mx = 0; for (int i = 0; i < n; ++i) { cin >> temp; a[temp]++; mx = max(mx, temp); } for (int i = 1; i < mx + 1; ++i) a[i] += a[i - 1]; for (int i = 0; i < mx + 1; ++i) { if ((a[i] % (i + 1)) == 0) a[i] /= (i + 1); else a[i] = (a[i] / (i + 1)) + 1; } int res = *max_element(a, a + 101); cout << res << "\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
n = int(input()) a = [int(x) for x in input().split()] v = [True]*n a.sort() ans = 0 prev = 0 i = 0 cnt = 0 while(cnt<n): if a[i]>=prev and v[i]: cnt += 1 v[i] = False prev += 1 i += 1 if i==n: i = 0 prev = 0 ans += 1 if prev != 0: ans += 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
#include <bits/stdc++.h> using namespace std; int box_strength[111], piles[111]; int main() { int num, i, j; cin >> num; for (i = 0; i <= num - 1; i++) cin >> box_strength[i]; sort(box_strength, box_strength + num); int pile_index = 0, min = INT_MAX, min_index; for (i = 0; i <= num - 1; i++) { min = INT_MAX; if (i == 0) piles[pile_index++] = 1; else { if (piles[0] <= box_strength[i]) piles[0]++; else piles[pile_index++] = 1; } for (j = 0; j <= pile_index - 1; j++) if (min > piles[j]) min = piles[j], min_index = j; swap(piles[0], piles[min_index]); } cout << pile_index << 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.util.*; //Scanner; import java.io.PrintWriter; //PrintWriter public class R228_Div2_C { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); solve(in, out); out.close(); in.close(); } public static void solve(Scanner in, PrintWriter out) { int n = in.nextInt(); int[] d = new int[101]; int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); d[a[i]]++; } int lastInd = -1; int[] b = new int[n]; for (int i = 0; i < 101; i++) { if (d[i] > 0) { int cnt = d[i]; for (int j = 0; j <= lastInd && cnt > 0; j++) while (b[j] <= i && cnt > 0) { b[j]++; cnt--; } while (cnt > 0) { lastInd++; while (b[lastInd] <= i && cnt > 0) { b[lastInd]++; cnt--; } } } } out.println(lastInd+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
/** * Created with IntelliJ IDEA. * User: den * Date: 2/3/14 * Time: 6:12 PM * To change this template use File | Settings | File Templates. */ import java.io.*; import java.util.Arrays; import java.util.Collections; import java.util.StringTokenizer; public class TaskC extends Thread { public static int ans; public static boolean[] checked; public int getGrowingSeq(Integer[] a) { int currentSeqSize; int i = 0; while (checked[i] && i < a.length) { i++; } if (i == a.length) { return 0; } else { checked[i] = true; currentSeqSize = 1; } int j = i+1; while (j < a.length) { if (checked[j]){ j++; continue; } if (a[j] >= a[i] && a[j] >= currentSeqSize) { checked[j] = true; currentSeqSize++; } j++; } return currentSeqSize; } private void solve() throws IOException { int n = _int(); Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = _int(); ans = 0; int unchecked = n; Arrays.sort(a); checked = new boolean[n]; while (unchecked > 0) { unchecked -= getGrowingSeq(a); ans++; } out.println(ans); } public void run() { try { solve(); } catch (Exception e) { System.out.println("System exiting...."); e.printStackTrace(); System.exit(888); } finally { out.flush(); out.close(); } } public static void main(String[] args) throws FileNotFoundException { new TaskC().run(); } public TaskC() throws FileNotFoundException { //in = new BufferedReader(new FileReader("A-large.in")); //out = new PrintWriter(new File("A-large.out")); in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); setPriority(Thread.MAX_PRIORITY); } private BufferedReader in; private PrintWriter out; private StringTokenizer st; private int _int() throws IOException { return Integer.parseInt(nextToken()); } private double _double() throws IOException { return Double.parseDouble(nextToken()); } private long _long() throws IOException { return Long.parseLong(nextToken()); } private char[] _chars() throws IOException { return nextToken().toCharArray(); } private String nextToken() throws IOException { if (st == null || !st.hasMoreElements()) st = new StringTokenizer(in.readLine(), " \t\r\n"); return st.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.io.OutputStreamWriter; import java.io.BufferedWriter; import java.util.Comparator; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.io.IOException; import java.util.Arrays; import java.util.InputMismatchException; import java.util.NoSuchElementException; import java.math.BigInteger; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Árysson Cavalcanti */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); Task solver = new Task(); solver.solve(1, in, out); out.close(); } } class Task { public void solve(int testNumber, InputReader in, OutputWriter out) { int n=in.readInt(); int[] arr= IOUtils.readIntArray(in, n); ArrayUtils.sort(arr); ArrayUtils.reverse(arr); for (int i=1; i<=n; i++) if (f(arr, i)) { out.printLine(i); return; } } boolean f(int[] arr, int n) { int[] aux=Arrays.copyOf(arr, n); for (int i=n; i<arr.length; i++) { int idx=ArrayUtils.maxPosition(aux); if (aux[idx]==0) return false; aux[idx]=MiscUtils.min(aux[idx]-1, arr[i]); } return true; } } 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 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; } 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 void close() { writer.close(); } public void printLine(int i) { writer.println(i); } } class IOUtils { public static int[] readIntArray(InputReader in, int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = in.readInt(); return array; } } class ArrayUtils { public static int[] sort(int[] array) { return sort(array, IntComparator.DEFAULT); } public static int[] sort(int[] array, IntComparator comparator) { return sort(array, 0, array.length, comparator); } public static int[] sort(int[] array, int from, int to, IntComparator comparator) { if (from == 0 && to == array.length) new IntArray(array).inPlaceSort(comparator); else new IntArray(array).subList(from, to).inPlaceSort(comparator); return array; } public static void reverse(int[] array) { for (int i = 0, j = array.length - 1; i < j; i++, j--) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } } public static int maxPosition(int[] array) { return maxPosition(array, 0, array.length); } public static int maxPosition(int[] array, int from, int to) { if (from >= to) return -1; int max = array[from]; int result = from; for (int i = from + 1; i < to; i++) { if (array[i] > max) { max = array[i]; result = i; } } return result; } } class MiscUtils { public static<T extends Comparable<T>> T min(T first, T second) { if (first.compareTo(second) <= 0) return first; return second; } } abstract class IntCollection { public abstract IntIterator iterator(); public abstract int size(); } interface IntIterator { public int value() throws NoSuchElementException; /* * @throws NoSuchElementException only if iterator already invalid */ public void advance() throws NoSuchElementException; public boolean isValid(); } interface IntComparator { public static final IntComparator DEFAULT = new IntComparator() { public int compare(int first, int second) { if (first < second) return -1; if (first > second) return 1; return 0; } }; public int compare(int first, int second); } abstract class IntList extends IntCollection implements Comparable<IntList> { private static final int INSERTION_THRESHOLD = 16; public abstract int get(int index); public abstract void set(int index, int value); public IntIterator iterator() { return new IntIterator() { private int size = size(); private int index = 0; public int value() throws NoSuchElementException { if (!isValid()) throw new NoSuchElementException(); return get(index); } public void advance() throws NoSuchElementException { if (!isValid()) throw new NoSuchElementException(); index++; } public boolean isValid() { return index < size; } }; } public IntList subList(final int from, final int to) { return new SubList(from, to); } private void swap(int first, int second) { if (first == second) return; int temp = get(first); set(first, get(second)); set(second, temp); } public IntSortedList inPlaceSort(IntComparator comparator) { quickSort(0, size() - 1, (Integer.bitCount(Integer.highestOneBit(size()) - 1) * 5) >> 1, comparator); return new IntSortedArray(this, comparator); } private void quickSort(int from, int to, int remaining, IntComparator comparator) { if (to - from < INSERTION_THRESHOLD) { insertionSort(from, to, comparator); return; } if (remaining == 0) { heapSort(from, to, comparator); return; } remaining--; int pivotIndex = (from + to) >> 1; int pivot = get(pivotIndex); swap(pivotIndex, to); int storeIndex = from; int equalIndex = to; for (int i = from; i < equalIndex; i++) { int value = comparator.compare(get(i), pivot); if (value < 0) swap(storeIndex++, i); else if (value == 0) swap(--equalIndex, i--); } quickSort(from, storeIndex - 1, remaining, comparator); for (int i = equalIndex; i <= to; i++) swap(storeIndex++, i); quickSort(storeIndex, to, remaining, comparator); } private void heapSort(int from, int to, IntComparator comparator) { for (int i = (to + from - 1) >> 1; i >= from; i--) siftDown(i, to, comparator, from); for (int i = to; i > from; i--) { swap(from, i); siftDown(from, i - 1, comparator, from); } } private void siftDown(int start, int end, IntComparator comparator, int delta) { int value = get(start); while (true) { int child = ((start - delta) << 1) + 1 + delta; if (child > end) return; int childValue = get(child); if (child + 1 <= end) { int otherValue = get(child + 1); if (comparator.compare(otherValue, childValue) > 0) { child++; childValue = otherValue; } } if (comparator.compare(value, childValue) >= 0) return; swap(start, child); start = child; } } private void insertionSort(int from, int to, IntComparator comparator) { for (int i = from + 1; i <= to; i++) { int value = get(i); for (int j = i - 1; j >= from; j--) { if (comparator.compare(get(j), value) <= 0) break; swap(j, j + 1); } } } public int hashCode() { int hashCode = 1; for (IntIterator i = iterator(); i.isValid(); i.advance()) hashCode = 31 * hashCode + i.value(); return hashCode; } public boolean equals(Object obj) { if (!(obj instanceof IntList)) return false; IntList list = (IntList)obj; if (list.size() != size()) return false; IntIterator i = iterator(); IntIterator j = list.iterator(); while (i.isValid()) { if (i.value() != j.value()) return false; i.advance(); j.advance(); } return true; } public int compareTo(IntList o) { IntIterator i = iterator(); IntIterator j = o.iterator(); while (true) { if (i.isValid()) { if (j.isValid()) { if (i.value() != j.value()) { if (i.value() < j.value()) return -1; else return 1; } } else return 1; } else { if (j.isValid()) return -1; else return 0; } i.advance(); j.advance(); } } private class SubList extends IntList { private final int to; private final int from; private int size; public SubList(int from, int to) { this.to = to; this.from = from; size = to - from; } public int get(int index) { if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); return IntList.this.get(index + from); } public void set(int index, int value) { if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); IntList.this.set(index + from, value); } public int size() { return size; } } } abstract class IntSortedList extends IntList { protected final IntComparator comparator; protected IntSortedList(IntComparator comparator) { this.comparator = comparator; } public void set(int index, int value) { throw new UnsupportedOperationException(); } public IntSortedList inPlaceSort(IntComparator comparator) { if (comparator == this.comparator) return this; throw new UnsupportedOperationException(); } protected void ensureSorted() { int size = size(); if (size == 0) return; int last = get(0); for (int i = 1; i < size; i++) { int current = get(i); if (comparator.compare(last, current) > 0) throw new IllegalArgumentException(); last = current; } } public IntSortedList subList(final int from, final int to) { return new IntSortedList(comparator) { private int size = to - from; public int get(int index) { if (index < 0 || index >= size) throw new IndexOutOfBoundsException(); return IntSortedList.this.get(index + from); } public int size() { return size; } }; } } class IntArray extends IntList { private final int[] array; public IntArray(int[] array) { this.array = array; } public int get(int index) { return array[index]; } public void set(int index, int value) { array[index] = value; } public int size() { return array.length; } } class IntSortedArray extends IntSortedList { private final int[] array; public IntSortedArray(IntCollection collection, IntComparator comparator) { super(comparator); array = new int[collection.size()]; int i = 0; for (IntIterator iterator = collection.iterator(); iterator.isValid(); iterator.advance()) array[i++] = iterator.value(); ensureSorted(); } public int get(int index) { return array[index]; } public int size() { return array.length; } }
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; import java.util.LinkedList; import java.util.Queue; import java.util.StringTokenizer; public class C228 { public void run() throws NumberFormatException, IOException { BufferedReader bReader = new BufferedReader(new InputStreamReader( System.in)); int n = Integer.parseInt(bReader.readLine()); StringTokenizer tokenizer = new StringTokenizer(bReader.readLine()); int arr[] = new int[n]; for (int i = 0; i < n; i++) arr[i] = Integer.parseInt(tokenizer.nextToken()); Arrays.sort(arr); LinkedList<Queue<Integer>> all = new LinkedList<Queue<Integer>>(); for (int i = 0; i < n; i++) { if (all.size() == 0) { Queue<Integer> now = new LinkedList<Integer>(); now.add(arr[i]); all.add(now); continue; } boolean ins = false; for (int j = 0; j < all.size(); j++) { if (all.get(j).size() <= arr[i]) { all.get(j).add(arr[i]); ins = true; break; } } if (!ins) { Queue<Integer> now = new LinkedList<Integer>(); now.add(arr[i]); all.add(now); } } System.out.println(all.size()); } public static void main(String[] args) throws NumberFormatException, IOException { // TODO Auto-generated method stub new C228().run(); } }
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.Collections; import java.util.LinkedList; import java.util.List; import java.util.ListIterator; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); List<Integer> list = new LinkedList<>(); for(int i=0; i<n; i++) { list.add(sc.nextInt()); } Collections.sort(list); int piles = 0; ListIterator<Integer> it; while(list.size() > 0) { ++piles; it = list.listIterator(); int count = 0; while(it.hasNext()) { int strength = it.next(); if(strength >= count) { it.remove(); ++count; } } } 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
#include <bits/stdc++.h> using namespace std; int main() { int n, strength[104], count = 0; bool strikeoff[104]; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &strength[i]); strikeoff[i] = false; } sort(strength, strength + n); int weight = 0, start_at = 0; bool start_set = true; while (start_set != false) { start_set = false; weight = 0; for (int i = start_at; i < n; i++) { if (strikeoff[i] == false) { if (weight <= strength[i]) { strikeoff[i] = true; weight++; } else { if (start_set == false) { start_at = i; start_set = true; } } } } count++; } printf("%d\n", count); 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; const int maxn = 1e2 + 5; int a[maxn]; int bo[maxn]; int fin[105]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; bo[a[i]]++; } sort(a + 1, a + n + 1); int maxx = 1; int i = 0; while (n) { int k = 1; while (bo[a[i]]) { if (a[i] < fin[k]) { k++; } fin[k]++; bo[a[i]]--; n--; } i++; if (k > maxx) { maxx = k; } } cout << maxx << 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() { ios_base::sync_with_stdio(false); int n, a[105]; vector<int> v; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); for (int i = 0; i < n; i++) { int j; for (j = 0; j < (int)v.size(); j++) { if (v[j] <= a[i]) { v[j]++; break; } } if (j == (int)v.size()) v.push_back(1); } cout << (int)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
#include <bits/stdc++.h> using namespace std; const int INF = 1000000000; const int maxn = 110; int n; int s[maxn]; bool used[maxn]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &s[i]); sort(s, s + n); for (int i = 0; i < n; i++) used[i] = false; int cnt = 0; for (int i = 0; i < n; i++) { if (!used[i]) { int pile = 1; used[i] = true; for (int j = i + 1; j < n; j++) { if (!used[j] && pile <= s[j]) { used[j] = true; pile++; } } cnt++; } } printf("%d\n", 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
/****************************************************************************** Online Java Compiler. Code, Compile, Run and Debug java program online. Write your code in this editor and press "Run" button to execute it. *******************************************************************************/ import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); ArrayList<Integer> arr=new ArrayList<>(); for(int i=0;i<n;i++) arr.add(sc.nextInt()); int piles = 0; Collections.sort(arr); while(arr.size()>0){ int storage=arr.get(0); int count=1; arr.remove(0); for(int i=0;i<arr.size();){ if(arr.get(i)>=storage && arr.get(i)>=count){ count++; // System.out.println(arr.get(i)); storage=arr.get(i); arr.remove(i); } else i++; } 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
#include <bits/stdc++.h> int n, MaxStr, i, x, f[105], ans; int max(int a, int b) { return a > b ? a : b; } void solve(int str, int now) { if (str > MaxStr) return; if ((now > str) || (!f[str])) solve(str + 1, now); else { f[str]--; solve(str, now + 1); } } int main() { scanf("%d", &n); memset(f, 0, sizeof(f)); for (MaxStr = 0, i = 1; i <= n; ++i) { scanf("%d", &x); MaxStr = max(x, MaxStr); f[x]++; } for (ans = i = 0; i <= MaxStr; ++i) while (f[i]) { solve(i, 0); ans++; } printf("%d\n", ans); }
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.*; import java.io.*; public class Fox_And_Fox_Accumulation { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 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; } } public static void main(String[] args) { // TODO Auto-generated method stub FastReader t = new FastReader(); PrintWriter o = new PrintWriter(System.out); int n = t.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; ++i) a[i] = t.nextInt(); Arrays.sort(a); int max = 1; for (int i = 1; i < n; ++i) { if (a[i] < (i / max)) max++; } o.println(max); o.flush(); o.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.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int x[] = new int[n]; boolean taken[] = new boolean[n]; for(int i = 0; i < n; i++) { x[i] = scanner.nextInt(); } Arrays.sort(x); int ans = 0; for(int i = 0; i < x.length; i++) { int boxes = 1; if(taken[i]) continue; for(int j = i+1; j < x.length; j++) { if(!taken[j] && x[j] >= boxes) { boxes++; taken[j] = true; } } ans++; } System.out.println(ans); scanner.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
""" greedy alg properties : len(stack) < x[i], if not, new stack """ number_of_items = int(raw_input()) items_strength = map(int, raw_input().split(" ")) items_strength.sort() stacks = [] new_stack = [] stacks.append(new_stack) for item in items_strength: flag = True for stack in stacks: if len(stack) <= item: stack.append(item) flag = False break # open new stack if flag: new_stack = [] new_stack.append(item) stacks.append(new_stack) print len(stacks)
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.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.Arrays; import java.util.InputMismatchException; public class CF { public static void main(String[] args) { FasterScanner sc = new FasterScanner(); PrintWriter out = new PrintWriter(System.out); int N = sc.nextInt(); int[] boxes = new int[N]; for(int a=0;a<N;a++) boxes[a]=-sc.nextInt(); Arrays.sort(boxes); for(int a=0;a<N;a++) boxes[a]=-boxes[a]; int low = 1; int high = N; int mid = 42; int ans = N; while(low<=high){ mid = (low+high)/2; if(check(mid,boxes)){ high = mid-1; ans = mid; } else { low = mid+1; } } out.println(ans); out.close(); } private static boolean check(int mid, int[] boxes) { int N = boxes.length; for(int a=0;a<mid;a++){ int start = a; ArrayDeque<Integer> AD = new ArrayDeque<Integer>(); while(start<N){ AD.add(boxes[start]); start+=mid; } while(!AD.isEmpty()){ int cur = AD.poll(); if(cur<AD.size())return false; } } return true; } static class FasterScanner { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FasterScanner() { stream = System.in; } 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++]; } boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } boolean isEndline(int c) { return c == '\n' || c == '\r' || c == -1; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } String nextLine() { int c = read(); while (isEndline(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndline(c)); return res.toString(); } } }
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 = 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 pile 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 sys n=int(raw_input()) ar=map(int,raw_input().split()) ar.sort() p=0 c_ar=ar[::] while ar!=[]: ## print ar nb=0 p+=1 for i in ar: if nb<=i: nb+=1 c_ar.remove(i) ar=c_ar[::] print p
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 n, i, a[10000], t, q, kol, ans[10000]; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (int i = 0; i < n; i++) { if (ans[i] == 0) { t = 1; q = a[i]; kol++; ans[i] = -1; for (int j = i + 1; j < n; j++) { if (a[j] >= t && ans[j] != -1) { ans[j] = -1; t++; } } } } cout << kol << endl; }
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() { bool used[101] = {}; int n; int a[101] = {}; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); int ans = 0; while (1) { int numbx = 0; bool ff = false; for (int i = 0; i < n; i++) { if (!used[i] && a[i] >= numbx) { used[i] = 1; numbx++; ff = true; } } if (ff) ans++; if (!ff) break; } 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
n = int(input()) li = sorted([int(x) for x in input().split()]) res = 1 for i in range(n): if (li[i] < i // res): res += 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
# -*- coding: utf-8 -*- """ Created on Fri Jun 21 00:21:11 2019 @author: sj """ n=int(input()) s=input().split(" ") for i in range(0,n): s[i]=int(s[i]) s=sorted(s) a=[] tt=0 for i in range(0,n): c=0 for j in range(tt,len(a)): if s[i]>a[j]: a[j]+=1 c=1 break if c==0: a.append(0) print(len(a))
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(void) { int n, a[105], k = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); for (int i = 0; i < n; i++) { if (k * a[i] + k <= i) k++; } cout << k; }
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
/** * Created with IntelliJ IDEA. * User: igarus * Date: 13.12.13 * Time: 18:48 * To change this template use File | Settings | File Templates. */ import java.util.Arrays; import java.util.Scanner; public class CF228_C { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); scanner.nextLine(); String[] a = scanner.nextLine().split(" "); int[] x = new int[n]; int[] v = new int[n]; Arrays.fill(v, 1); for (int i = 0; i < n; i++) { x[i] = Integer.parseInt(a[i]); } Arrays.sort(x); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (x[j] >= v[i]) { // System.out.println("# iteration for i" + i + ":j" + j); v[j] += v[i]; x[j] -= v[i]; if (x[j] > x[i]) x[j] = x[i]; x[i] = -1; v[i] = -1; // System.out.println(Arrays.toString(x)); // System.out.println(Arrays.toString(v)); break; } } } int s = 0; for (int i : x) if (i != -1) s++; System.out.println(s); } }
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() { std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int n, i; cin >> n; vector<int> a(n); for (i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); int count = 0; i = 0; while (n) { i = 0; for (int j = 0; j < a.size(); j++) { if (a[j] >= i) { a[j] = -1; i++; n--; } } count++; } cout << count; }
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.InputStreamReader; import java.util.*; public class Main { public static void main(String[] args) throws IOException { //Scanner sc = new Scanner(System.in); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine().trim()); int[] nums = new int[n]; StringTokenizer row = new StringTokenizer(br.readLine().trim()); for(int i = 0; i < n; i++){ nums[i] = Integer.parseInt(row.nextToken().trim()); } Arrays.sort(nums); int[] piles = new int[n]; for(int i = 0; i < n; i++){ // fill pile i for(int j = 0; j < n; j++){ if(piles[j] <= nums[i]) { piles[j]++; break; } } } int count = 0; for(int i = 0; i < n; i++){ if(piles[i] > 0){ count++; } } System.out.println(count); } }
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 itertools import combinations # from bisect import bisect_left from collections import Counter I = lambda: list(map(int, input().split())) n, a = I(), I() a.sort() h = [1] for el in a[1:]: mn = min(h) if el >= mn: h[h.index(mn)] += 1 else: h.append(1) print(len(h))
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()) ar = list(map(int,input().split(' '))) ar.sort() s=[] for i in ar: s.sort(reverse=True) for j in range(len(s)): if i>=s[j]: s[j]+=1 break else: s.append(1) print(len(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
def isPossible(arr,pile): arr.sort(reverse=True) size = [10**18]*pile #print(arr) c = 0 for item in arr: if size[c%pile] >= 1: size[c%pile] = min(size[c%pile]-1,item) else: return False c = c + 1 return True n = int(input()) arr = [int(num) for num in input().split(' ')] ans = n while ans>=1: if isPossible(arr,ans) == True: res = ans else: break ans = ans - 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
n = int(input()) nums = sorted(list(map(int, input().split(' ')))) v = [] b = [0] * 105 cnt = 0 k = 0 while cnt < n: k += 1 for i in range(n): if nums[i] >= len(v) and b[i] == 0: v.append(nums[i]) b[i] = 1 cnt += 1 #print(list(reversed(v))) v = [] 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; const int N = 1e3; int m[N], n, tmp, ans, z; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> tmp; m[tmp]++; z += (tmp == 0); } for (int i = 0; i < 103; i++) { if (m[i]) { while (m[i]) { m[i]--; ans++; tmp = 1; for (int j = i; j < 103; j++) { while (m[j] && j >= tmp) { m[j]--; tmp++; } } } } } ans = max(ans, z); cout << ans << endl; }
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, a[111]; vector<int> L[111]; bool ok; int main() { cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; sort(a, a + n); int cnt = 0; for (int i = 0; i < n; ++i) { ok = 1; for (int j = 0; j < cnt; ++j) { if (a[i] >= L[j].size()) { L[j].push_back(a[i]); ok = false; break; } } if (ok) L[cnt++].push_back(a[i]); } cout << 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
import java.io.*; import java.util.*; public class Main{ public static void main(String args[]) { Scanner in= new Scanner(System.in); int n=in.nextInt(),num; int []a=new int[n]; for(int i=0;i<n;i++) a[i]=in.nextInt(); Arrays.sort(a); for (num=1;;num++) { int flag=1; for (int i=0;i<n;i++) if(a[i]<i/num){ flag=0; break; } if(flag==1){ System.out.println(num); break; } } } }
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; bool cmp(int a, int b) { return (a > b); } bool check(int num, int x[], int N) { int cap[100]; int totalCap = 0; for (int i = 0; i < num; i++) { cap[i] = x[i]; totalCap += cap[i]; } if (totalCap < N - num) return false; for (int i = num; i < N; i++) { int j = i % num; int tmp = min(cap[j] - 1, x[i]); totalCap -= (cap[j] - tmp); if (totalCap < N - (i + 1)) return false; cap[j] = tmp; } return true; } int main() { int N, x[100]; while (1 == scanf("%d", &N)) { int M = 0; for (int i = 0; i < N; i++) { scanf("%d", &x[i]); if (x[i] == 0) M++; } sort(x, x + N, cmp); int result = N; int low = M; int high = N; while (low <= high) { int mid = (low + high) / 2; if (check(mid, x, N)) { result = min(result, mid); high = mid - 1; } else { low = mid + 1; } } printf("%d\n", result); } 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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author neuivn */ 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(); int[] elem = new int[N]; for (int i = 0; i < N; ++i) elem[i] = in.nextInt(); Arrays.sort(elem); boolean[] used = new boolean[N]; int min = 0; for (int i = 0; i < N; ++i) { if (!used[i]) { used[i] = true; ++min; int cur = 1; for (int j = i + 1; j < N; ++j) { if (elem[j] >= cur && !used[j]) { used[j] = true; ++cur; } } } } out.println(min); } } 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()); } } }
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; bool visited[10000]; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); int str = 0, count1 = 0; for (int i = 0; i < n; i++) { if (visited[i]) continue; count1++; str = 0; for (int j = i + 1; j < n; j++) { if (visited[j]) continue; if (a[j] > str) { str++; visited[j] = 1; } } } cout << count1; 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() res = 0 for i in range(n): cnt = i+1 lvl = a[i]+1 res = max(res , (cnt+lvl-1)//lvl) 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
#include <bits/stdc++.h> using namespace ::std; int cols[200], a[200], sizes[200]; int main() { int n, active = 0; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (int i = 0; i < n; i++) { int l = 0; while (l < active) { if (cols[l] <= a[i] && sizes[l] <= a[i]) { cols[l] = a[i]; sizes[l]++; break; } l++; } if (l == active) { cols[l] = a[i]; sizes[l] = 1; active++; } } cout << active << endl; }
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 power(long long a, long long b) { long long result = 1; while (b > 0) { if (b % 2 == 1) { result *= a; } a *= a; b /= 2; } return result; } long long gcd(long long x, long long y) { long long r; while (y != 0 && (r = x % y) != 0) { x = y; y = r; } return y == 0 ? x : y; } long long countSetBits(long long x) { long long Count = 0; while (x > 0) { if (x & 1) Count++; x = x >> 1; } return Count; } bool isPerfectSquare(long long n) { long long sr = sqrt(n); if (sr * sr == n) return true; else return false; } long long mod(long long x, long long M) { return ((x % M + M) % M); } long long add(long long a, long long b, long long M) { return mod(mod(a, M) + mod(b, M), M); } long long mul(long long a, long long b, long long M) { return mod(mod(a, M) * mod(b, M), M); } long long powerM(long long a, long long b, long long M) { long long res = 1; while (b) { if (b % 2 == 1) { res = mul(a, res, M); } a = mul(a, a, M); b /= 2; } return res; } long long modInverse(long long n, long long M) { return powerM(n, M - 2, M); } long long nCrM(long long n, long long r, long long M) { if (n < r) return 0; if (r == 0) return 1; vector<long long> fact(n + 1); fact[0] = 1; for (long long i = 1; i <= n; i++) { fact[i] = mul(fact[i - 1], i, M); } return mul(mul(fact[n], modInverse(fact[r], M), M), modInverse(fact[n - r], M), M); } void solve() { long long n; cin >> n; vector<long long> v(n); for (auto& w : v) cin >> w; sort(v.begin(), v.end(), greater<int>()); long long l = 1; long long r = n; while (l < r) { long long mid = (l + r) / 2; long long fal = 0; vector<vector<long long>> vv(mid); long long curr = 0; long long ans = n; while (curr < n) { for (long long i = 0; i < mid; i++) { if (curr < n) { vv[i].push_back(v[curr]); curr++; } } } for (long long i = 0; i < mid; i++) { long long curr = 0; for (long long j = vv[i].size() - 1; j >= 0; j--) { if (curr > vv[i][j]) { fal++; break; } curr++; } } if (fal != 0) { l = mid + 1; } else { ans = mid; r = mid; } } cout << l << '\n'; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); 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
#include <bits/stdc++.h> using namespace std; int n, a[110], i, hsh[110][3], j, cnt, k, ans, span; int main() { ios::sync_with_stdio(0); cin >> n; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (i = 0, k = 0; i < n; i = j) { cnt = 1; for (j = i + 1; j < n; j++) { if (a[j] != a[i]) break; cnt++; } hsh[k][0] = a[i]; hsh[k++][1] = cnt; } hsh[0][2] = hsh[0][1]; for (i = 1; i < k; i++) { hsh[i][2] = hsh[i][1] + hsh[i - 1][2]; } ans = 0; cnt = 0; bool flag = 1; while (cnt < n) { for (i = k - 1; i >= 0; i--) { if (hsh[i][1] > 0) { k = i + 1; break; } } if (cnt < n) { for (i = k - 1; i >= 0;) { if (hsh[i][1] > 0) { if (hsh[i][2] <= hsh[i][0] + 1) { cnt += hsh[i][1]; hsh[i][1] = 0; } else if (hsh[i][2] - hsh[i][1] < hsh[i][0]) { cnt += 1 + hsh[i][0] - (hsh[i][2] - hsh[i][1]); hsh[i][1] -= 1 + hsh[i][0] - (hsh[i][2] - hsh[i][1]); } else { hsh[i][1]--; cnt++; } j = i; for (i--; i >= 0; i--) { if (hsh[i][1]) break; } if (i < 0) continue; if (hsh[j][1] <= hsh[j][0] - hsh[i][0] - 1) { cnt += hsh[j][1]; hsh[j][1] = 0; } else { cnt += hsh[j][0] - hsh[i][0] - 1; hsh[j][1] -= hsh[j][0] - hsh[i][0] - 1; } } } hsh[0][2] = hsh[0][1]; for (i = 1; i < k; i++) hsh[i][2] = hsh[i][1] + hsh[i - 1][2]; ans++; } } 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> using namespace std; int n, a[101], piller; int main() { cin >> n; for (auto i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (auto i = 0; i < n; i++) { if (piller * a[i] + piller <= i) piller++; } cout << piller; }
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, a[105], f, ans; int main() { cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; } sort(a, a + n); while (n) { ++ans; for (int i = 0; i < n; ++i) { if (a[i] >= f) { ++f; a[i] = 999; } } sort(a, a + n); n -= f; f = 0; } cout << ans; }
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.InputStream; import java.io.InputStreamReader; import java.util.Arrays; import java.util.PriorityQueue; import java.util.StringTokenizer; public class A525 { public static void main(String[] Args) throws Exception{ FastScanner sc = new FastScanner(System.in); int n = sc.nextInt(); int[] vals = new int[n]; int ans = 0; for (int k = 0 ; k < n; k++){ vals[k] = sc.nextInt(); } Arrays.sort(vals); PriorityQueue<Integer> pq = new PriorityQueue<Integer>(); pq.add(1); ans++; for (int k = 1; k < n; k++){ if (pq.peek() <= vals[k]){ pq.add(pq.poll()+1); }else{ ans++; pq.add(1); } } System.out.println(ans); } public static class FastScanner{ StringTokenizer st; BufferedReader br; FastScanner(InputStream in) throws Exception{ br = new BufferedReader(new InputStreamReader(in)); st = new StringTokenizer(br.readLine()); } String next() throws Exception{ if (st.hasMoreTokens()) return st.nextToken(); st = new StringTokenizer(br.readLine()); return next(); } int nextInt() throws Exception{ 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 v[1000], x[1000]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &x[i]); sort(x, x + n); int ans = n; int high = n; int low = 1; while (high >= low) { int flag = 1; int med = (high + low) / 2; int i = n - 1; for (int j = 0; j < med; j++) { v[j] = x[i]; i--; } int j = 0; while (i >= 0) { v[j] = min(x[i], v[j] - 1); if (v[j] < 0) { flag = 0; goto label; } i--; j = (j + 1) % med; } label: if (flag) { ans = med; high = med - 1; } else low = med + 1; } 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
n = int(raw_input()) x_s = sorted([int(i) for i in raw_input().split()]) num_piles = n for k in range(1, n+1): flag = True for i in range(n): if x_s[i] < i/k: flag = False break if flag: num_piles = k break print num_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 double PI = acos(-1); const long long OO = 0x3f3f3f3f3f3f3f3f; const int oo = 0x3f3f3f3f; const long long mod = 1e9 + 7; const int N = 1e6 + 5; void run_case() { int n, ans = 0; cin >> n; vector<int> a(n); for (int &it : a) cin >> it; sort(a.begin(), a.end()); for (int i = 0; i < n; i++) { int x = 1; if (a[i] >= 0) { ans++; for (int j = i + 1; j < n; j++) { if (a[j] >= x) { a[j] = -12; x++; } } } } cout << ans; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int tc = 1; while (tc--) run_case(); }
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
var n=+readline(); var x=readline().split(' ').map(function(v){return+v;}); x.sort(function(a,b){return a-b;}); var ans=[]; while(true){ var q=[] ans.push(q); for(var j=0;j<x.length;j++){ if(x[j]>=q.length){ q.push(x[j]); x.splice(j,1); j--; } } if(x.length===0){ break; } } print(ans.length);
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[105]; int main() { ios_base::sync_with_stdio(false); scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", &a[i]); sort(a, a + n); for (int k = 1; k <= n; ++k) { int ok = true; for (int i = 0; i < n; ++i) if (a[i] < i / k) ok = false; if (ok) { 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 heapq n = int(input()) boxes = sorted(list(map(int, input().split()))) piles = [1] for b in boxes[1:]: if piles[0] <= b: heapq.heappush(piles, heapq.heappop(piles)+1) else: heapq.heappush(piles, 1) 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
#include <bits/stdc++.h> using namespace std; int n, a[101], c = 1, s, used[101]; int mi(int s2) { int minn = 5154541, minnn = -1; for (int i = 1; i <= n; i++) if (a[i] >= s2 && a[i] < minn && !used[i]) minn = a[i], minnn = i; used[minnn] = 1; return minnn; } bool allused(void) { for (int i = 1; i <= n; i++) if (!used[i]) return false; return true; } int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + 1 + n); while (!allused()) { if (mi(s) != -1) s++; else s = 0, c++; } 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
#include <bits/stdc++.h> using namespace std; const int N = 1e5; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed; cout.precision(10); ; int n; cin >> n; vector<int> arr(n); for (auto &i : arr) cin >> i; sort(arr.begin(), arr.end()); int temp[101]{}; int cnt = 0; for (int i = 0; i < n; i++) { for (int j = 1; j <= 100; j++) { if (temp[j] <= arr[i]) { temp[j]++; break; } } } for (long long i = 1; i < 101; i++) cnt += (temp[i] != 0); cout << 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
n=int(input()) a=map(int,raw_input().split()) a.sort() p=[1] for x in a[1:]: mini=min(p) if(x>=mini): p[p.index(mini)]+=1 else: p.append(1) print len(p)
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; vector<vector<int> > piles; vector<int> inp; int n; void process(int w) { bool can_reuse = false; for (int i = 0; i < piles.size(); i++) if (piles[i].size() <= w) { can_reuse = true; piles[i].push_back(w); return; } if (can_reuse == false) { vector<int> temp; temp.push_back(w); piles.push_back(temp); return; } } int main() { int t; cin >> n; for (int i = 0; i < n; i++) { cin >> t; inp.push_back(t); } sort(inp.begin(), inp.end()); for (int i = 0; i < n; i++) if (inp[i] != 0) break; else { vector<int> temp; temp.push_back(0); piles.push_back(temp); } while (inp.size() && inp[0] == 0) inp.erase(inp.begin()); if (piles.size() >= inp.size()) { cout << piles.size() << endl; return 0; } while (inp.size()) { process(inp[0]); inp.erase(inp.begin()); } cout << piles.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
n = input() h = [0] A = sorted(map(int, input().split())) for x in A: if x < min(h): h += [1] else: h[h.index(min(h))]+=1 print(len(h))
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 n; int a[110]; priority_queue<int> f; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); sort(a + 1, a + 1 + n); f.push(-1); for (int i = 2; i <= n; i++) { a[i] = -a[i]; if (f.top() < a[i]) f.push(-1); else f.push(f.top() - 1), f.pop(); } printf("%d", f.size()); 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 sys input = sys.stdin.readline read_tuple = lambda _type: map(_type, input().split(' ')) def is_correct(seq): if len(seq) == 1: return True _len = 1 res = True for idx in range(1, len(seq)): res = res and seq[idx] >= _len _len += 1 return res def solve(): n = int(input()) x = list(read_tuple(int)) x.sort(reverse=True) ans = 0 while x: stack = [x.pop()] while x: added = False for i in range(len(x) - 1, -1, -1): if is_correct(stack + [x[i]]): stack.append(x[i]) x.pop(i) added = True break if not added: break ans += 1 print(ans) if __name__ == '__main__': solve()
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()) a = [int(x) for x in input().split()] a.sort() pile,tc=0,n visited = [0]*n while tc != 0: tt=0 for i in range(0,n): if a[i]>=tt and visited[i] != 1: visited[i]=1 tt+=1 tc-=1 if(tt>0): pile+=1 print(pile)
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
'''input 2 50 50 ''' from sys import stdin def get_freq(arr): freq = [0] * (101) for i in arr: freq[i] += 1 return freq # main starts n = int(stdin.readline().strip()) arr = list(map(int, stdin.readline().split())) freq = get_freq(arr) i = 0 c = 0 while i <= 100: if freq[i] == 0: i += 1 else: freq[i] -= 1 c += 1 j = i strength = 1 while j <= 100: if j >= strength and freq[j] > 0: freq[j] -= 1 strength += 1 continue j += 1 print(c)
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.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] boxes = new int[100+1]; for (int i=0 ; i<n ; i++) boxes[in.nextInt()]++; in.close(); int piles = 0; while (n>0) { piles++; int stacked = 0; int m = 0; while (m<=100) { if (boxes[m] != 0 && m>=stacked) { n--; boxes[m]--; stacked++; } else m++; } } 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
#include <bits/stdc++.h> using namespace std; const int maxn = 1e2 + 10; int dp[maxn]; int n; int a[maxn]; int mp[maxn]; int mo[maxn]; int v(int u) { int res = 0; for (int i = u; i < maxn; i++) { res += mo[i]; } return res; } void of(int k, int f) { for (int i = k; i < maxn && f > 0; i++) { if (mo[i] > 0) { if (mo[i] < f) { f -= mo[i]; mo[i] = 0; } else { mo[i] -= f; f = 0; } } } return; } bool ok(int h) { int o = n / h; fill(mo, mo + maxn, 0); if (n % h != 0) { o++; } for (int i = 0; i < maxn; i++) { mo[i] = mp[i]; } for (int i = 0; i < o; i++) { if (i != o - 1) { if (v(i) < h) { return 0; } of(i, h); } else { if (n % h == 0) { if (v(i) < h) { return 0; } of(i, h); } else { if (v(i) < (int)n % h) { return 0; } of(i, h); } } } return 1; } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; mp[a[i]]++; } for (int i = 1; i <= n; i++) { if (ok(i)) { 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
#include <bits/stdc++.h> using namespace std; int a[101], n, res; bool can(int x) { vector<int> b(a, a + x); int k = 0; for (int j = x; j < n; ++j) { bool f = 0; int c = 0; while (c != x) { if (k == x) k = 0; if (b[k] > 0) { b[k] = min(a[j], b[k] - 1); k++; f = 1; break; } else { k++, c++; } } if (!f) return false; } return true; } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", &a[i]); sort(a, a + n); reverse(a, a + n); int l = 1, r = n, mid; while (l <= r) { mid = (l + r) / 2; if (can(mid)) { res = mid; r = mid - 1; } else l = mid + 1; } printf("%d\n", res); 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(void) { vector<int> piles[105]; vector<int> boxes; int n; scanf("%d", &n); for (int i = 0; i < n; i++) { int buff; scanf("%d", &buff); boxes.push_back(buff); } sort(boxes.begin(), boxes.end()); for (int i = 1; i <= n; i++) { for (int j = 0; j < n; j++) piles[j].clear(); for (int j = 0; j < n; j++) piles[j % i].push_back(boxes[boxes.size() - j - 1]); bool able = true; for (int j = 0; j < i; j++) { int s = piles[j].size() - 1; for (int k = 0; k < piles[j].size(); k++) { if (piles[j][k] < s) { able = false; break; } s--; } if (!able) break; } if (able) { printf("%d\n", i); break; } } 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 p[100100]; int main() { int n, a, b, t, cn = 1; cin >> n; for (int i = 0; i < n; i++) { cin >> a; p[i] = a; } sort(p, p + n); int ans = 0; vector<int> v; for (int i = 0; i < n; i++) { int j = 0; for (j = 0; j < v.size(); j++) { if (p[i] >= v[j]) { v[j]++; break; } } if (j == v.size()) { v.push_back(1); } } cout << v.size() << endl; return 0; }
CPP