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
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; int str[2000000]; bool can[2000000]; int main() { int i, j, k; int n, m, d; int now; while (cin >> n >> d) { for (i = 1; i <= n; i++) { scanf("%d", &str[i]); } sort(str + 1, str + 1 + n); now = 0; memset(can, false, sizeof(can)); can[0] = true; for (i = 1; i <= n; i++) { for (j = 2000000 - 1; j >= str[i]; j--) { if (can[j - str[i]]) can[j] = true; } } int day = 0; int now = 0; while (1) { for (i = now + d; i > now; i--) { if (can[i]) break; } if (i == now) break; day++; now = i; } printf("%d %d\n", now, day); } return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> int dp[51 * 10001]; int main(void) { int n, d, a, sum, i, j; while (scanf("%d%d", &n, &d) != EOF) { sum = 0; memset(dp, 0, sizeof(dp)); dp[0] = 1; for (i = 0; i < n; i++) { scanf("%d", &a); sum += a; for (j = sum; j >= a; j--) { if (dp[j - a] == 1) dp[j] = 1; } } int k = 0; int ans = 0; while (true) { i = k + d; while (dp[i] == 0 && i > k) i--; if (k == i) break; k = i; ans++; } printf("%d %d\n", k, ans); } return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; int a[500003]; vector<int> p; int main() { int N, D, u; cin >> N >> D; for (int i = 0; i < 500003; ++i) { a[i] = 0; } a[0] = 1; p.push_back(0); for (int i = 0; i < N; ++i) { cin >> u; for (int j = p.size(); j--;) { if (p[j] + u < 500003 && !a[p[j] + u]) { a[p[j] + u] = 1; p.push_back(p[j] + u); } } } int days = 0; int now = 0; while (1) { bool moved = false; for (int i = D + 1; i--;) { if (i == 0) break; if (now + i < 500003 && a[now + i]) { ++days; now += i; moved = true; break; } } if (!moved) break; } cout << now << " " << days << endl; return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; bitset<500005> dp; int N, D, ans, i, j, x, d, s = 1; int main() { dp[0] = 1; for (scanf("%d%d", &N, &D), i = 0; i < N; i++) { scanf("%d", &x); for (j = 500000; j >= x; j--) dp[j] = (dp[j] || dp[j - x]); } while (s) { for (s = 0, i = min(ans + D, 500000); i > ans; i--) { if (dp[i]) { s = i - ans; break; } } ans += s; d++; } printf("%d %d\n", ans, d - 1); return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; const int N = 55; const int D = 10100; int dp[N * D]; int n, d; int A[N]; void do_package() { memset(dp, 0, sizeof(dp)); dp[0] = 1; for (int i = 0; i < n; i++) { for (int j = N * D - 1; j >= 0; j--) { if (dp[j] && j + A[i] < N * D) { dp[j + A[i]] = 1; } } } } void solve(int &val, int &day) { val = day = 0; while (1) { bool flag = false; for (int i = val + d; i > val; i--) { if (dp[i]) { val = i; day++; flag = true; break; } } if (!flag) break; } } int main() { while (cin >> n >> d) { for (int i = 0; i < n; i++) { cin >> A[i]; } do_package(); int a, b; solve(a, b); cout << a << ' ' << b << endl; } return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; int dp[550001], c[10005]; int main() { int n, d; cin >> n >> d; for (register int i = 1; i <= n; i++) { cin >> c[i]; } memset(dp, 0, sizeof(dp)); dp[0] = 1; for (register int i = 1; i <= n; i++) { for (register int j = 550000; j >= c[i]; j--) { if (dp[j - c[i]] == 1) dp[j] = 1; } } register int now = 0, t = 0; for (; t <= 550000; t++) { int e = now + d; for (register int i = e; i >= now; i--) { if (dp[i] == 1) { now = i; break; } } if (e == now + d) break; } cout << now << ' ' << t << endl; return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; int dp[5000100]; int main() { int i, j, n, d; int cnt = 0, sum = 0, c = 0; dp[0] = 1; cin >> n >> d; int p; for (i = 0; i < n; i++) { cin >> p; for (j = (sum += p); j >= p; j--) if (dp[j - p] == 1) dp[j] = 1; } while (1) { j = d + c; while (!dp[j] && j > c) j--; if (j == c) break; c = j; cnt++; } cout << c << ' ' << cnt << endl; return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; const unsigned long long P = 239017, MaxN = 2100000, INF = 1000000000; int n, d, can[510100], a[1000]; vector<int> q; int main() { scanf("%d%d", &n, &d); for (int i = 0; i < n; ++i) scanf("%d", &a[i]); can[0] = 1; for (int i = 0; i < n; ++i) { for (int have = 500000; have >= 0; --have) if (can[have]) can[have + a[i]] = 1; } for (int i = 0; i <= 500000; ++i) if (can[i]) q.push_back(i); int day = 0, now = 0; for (int i = 0; i < q.size(); ++i) { if (now + d < q[i]) { if (now + d >= q[i - 1]) now = q[i - 1], ++day; } } if (now + d >= q.back()) now = q.back(), ++day; printf("%d %d\n", now, now == 0 ? 0 : day); return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; const int MAXN = 500010; bool vis[MAXN] = {0}; int M[MAXN]; int N, K, T; int main() { cin >> N >> K; vis[0] = true; for (int i = 1; i <= N; i++) { cin >> T; for (int j = i * 10000; j >= 0; j--) if (vis[j]) vis[j + T] = true; } int all = 0; for (int i = 0; i < MAXN; i++) if (vis[i]) M[all++] = i; int ans = 0; int now = 0; while (1) { int p = now + K; int po = upper_bound(M, M + all, p) - M - 1; if (M[po] == now) break; now = M[po]; ans++; } printf("%d %d\n", now, ans); }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; const int sz = 112345; int a[100]; bool dp[52][500005]; int sums[500005]; int main() { int n, d, i, j, ans, day; scanf("%d", &n); scanf("%d", &d); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } for (i = 0; i < 500005; i++) dp[0][i] = 0; dp[0][0] = 1; for (i = 1; i <= n; i++) { dp[i][0] = 1; for (j = 1; j < 500005; j++) { dp[i][j] = dp[i - 1][j]; if (a[i - 1] <= j) dp[i][j] |= dp[i - 1][j - a[i - 1]]; } } j = 0; for (i = 0; i < 500005; i++) if (dp[n][i]) { sums[j] = i; j++; } ans = 0; day = 0; for (i = 1; i < j; i++) { if (sums[i] > sums[i - 1] + d) break; int k = i; while (k < j && sums[k] <= sums[i - 1] + d) k++; k--; ans = sums[k]; i = k; day++; } printf("%d %d\n", ans, day); return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; int dp[555555]; int a[55]; int n, k; int main() { memset(dp, 0, sizeof(dp)); cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i]; dp[0] = 1; for (int i = 0; i < n; i++) for (int j = 555555; j >= a[i]; j--) dp[j] |= dp[j - a[i]]; int ans = 0, day = 0; while (1) { int i, t = ans; for (i = ans + k; i > ans; i--) { if (dp[i]) { ans = i; day++; break; } } if (t == ans) break; } cout << ans << " " << day << endl; return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; void solve(); int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed; cout.precision(12); solve(); return 0; } template <typename T> void sc(T& x) { cin >> x; } template <typename Head, typename... Tail> void sc(Head& head, Tail&... tail) { cin >> head; sc(tail...); } template <typename T> void pr(const T& x) { cout << x << '\n'; } template <typename Head, typename... Tail> void pr(const Head& head, const Tail&... tail) { cout << head << ' '; pr(tail...); } const int MAX_SUM = 5e5 + 10; bool vis[MAX_SUM]; void solve() { fill(vis, vis + MAX_SUM, false); int n, d; sc(n, d); vector<int> oks; oks.push_back(0); for (int i = 0; i < n; i++) { int x; sc(x); for (int j = ((int)oks.size()) - 1; j >= 0; j--) { if (x + oks[j] < MAX_SUM && !vis[x + oks[j]]) { oks.push_back(x + oks[j]); vis[x + oks[j]] = true; } } } sort(oks.begin(), oks.end()); int ans = 0, cur = 0; while (true) { int idx = upper_bound(oks.begin(), oks.end(), oks[cur] + d) - oks.begin(); if (oks[idx] != cur + d) { idx--; } if (idx >= ((int)oks.size()) || idx == cur) { break; } ans++; cur = idx; } pr(oks[cur], ans); }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; vector<int> arr; int n, d; int have; int lastHave; int dp[50][10000 * 55]; bool knapsack() { for (int j = lastHave + 1; j <= have + d; ++j) dp[0][j] = (arr[0] <= j) ? arr[0] : 0; for (int i = 1; i < n; ++i) { for (int j = lastHave + 1; j <= have + d; ++j) { dp[i][j] = dp[i - 1][j]; if (arr[i] <= j && dp[i - 1][j - arr[i]] + arr[i] > dp[i][j]) dp[i][j] = dp[i - 1][j - arr[i]] + arr[i]; } } if (have == dp[n - 1][have + d]) return false; lastHave = have; have = dp[n - 1][have + d]; return true; } int main() { int days = 0; have = 0; lastHave = 0; cin >> n >> d; arr.resize(n); for (int i = 0; i < n; ++i) scanf("%d", &arr[i]); sort(arr.begin(), arr.end()); while (knapsack()) ++days; cout << have << " " << days << endl; return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; int arr[50 + 5]; bool Sum[(int)1e4 * 50 + 5]; int main() { int N, D, ans = 0, maxi = 0, tot = 0; scanf("%d %d", &N, &D); for (int i = 0; i < N; i++) { scanf("%d", &arr[i]); tot += arr[i]; } Sum[0] = 1; for (int i = 0; i < N; i++) for (int j = tot - arr[i]; j >= 0; j--) if (Sum[j]) Sum[j + arr[i]] = 1; while (1) { int nmaxi = 0; for (int i = min(tot, maxi + D); i > 0; i--) { if (!Sum[i]) continue; nmaxi = max(nmaxi, i); break; } if (nmaxi <= maxi) break; maxi = nmaxi; ans++; } printf("%d %d\n", maxi, ans); return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; int N, D; int val[60]; int dp[10010 * 60]; int main() { int i, j; cin >> N >> D; int sum = 0; dp[0] = 1; for (i = 0; i < N; i++) { cin >> val[i]; sum += val[i]; for (j = sum; j >= val[i]; j--) { if (dp[j - val[i]]) { dp[j] = 1; } } } int day, ans; day = ans = 0; while (true) { for (i = D; i > 0; i--) { if (dp[ans + i]) { ans += i; day++; break; } } if (i == 0) { break; } } cout << ans << " " << day << endl; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; const int maxn = 2e6; int a[maxn], dp[maxn]; int main() { int n, d; scanf("%d%d", &n, &d); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); dp[0] = 1; for (int i = 1; i <= n; i++) for (int j = 600000; j >= a[i]; j--) dp[j] = max(dp[j], dp[j - a[i]]); int ans = 0, num = 0; while (1) { int flag = 0; for (int i = ans + d; i > ans; i--) if (dp[i]) { ans = i; num++; flag = 1; } if (!flag) break; } printf("%d %d", ans, num); return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:268435456,268435456") using namespace std; int a[50]; bool dp[510001]; int main() { int n, d; scanf("%d %d", &n, &d), dp[0] = 1; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); for (int j = 510001 - 1; j >= a[i]; j--) dp[j] |= dp[j - a[i]]; } int curr = 0, ans = 0, ans2 = 0; while (true) { int temp = curr; for (int i = d; i >= 1; i--) if (dp[temp + i]) { temp += i, ans++, ans2 = max(ans2, temp); break; } if (temp == curr) break; curr = temp; } printf("%d %d\n", ans2, ans); return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; const int N = 555555; bool dp[N]; int main() { int n, d; while (cin >> n >> d) { memset(dp, 0, sizeof(dp)); int c; dp[0] = 1; for (int i = 0; i < n; i++) { cin >> c; for (int j = N - 1; j >= c; j--) dp[j] |= dp[j - c]; } int mx = 0, cnt = 0; while (1) { int nx = mx + d; while (!dp[nx]) nx--; if (nx == mx) break; mx = nx; cnt++; } cout << mx << ' ' << cnt << endl; } return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; const long long A = 100000000000000LL, N = 2228228; long long dp[N], i, j, n, r, m, a, o[2]; int main() { cin >> n >> m, dp[0] = 1; for (i = 0; i < n; i++) { cin >> a, r += a; for (j = r; j >= a; j--) if (dp[j - a]) dp[j] = 1; } while (1) { j = o[0] + m; while (!dp[j] && (j > o[0])) j--; if (j == o[0]) break; o[0] = j, o[1]++; } cout << o[0] << " " << o[1] << "\n"; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
import java.util.*; public class b { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(), d = input.nextInt(); boolean[] possible = new boolean[1000000]; possible[0] = true; int[] data = new int[n]; for(int i = 0; i<n; i++) data[i] = input.nextInt(); for(int i = 0; i<n; i++) { for(int j = 600000; j>=0; j--) if(possible[j]) possible[j+data[i]] = true; } Queue<Integer> good = new LinkedList<Integer>(); for(int i = 0; i<possible.length; i++) if(possible[i]) good.add(i); good.poll(); int ans = 0, count = 0; while(!good.isEmpty()) { if(good.peek() - ans > d) break; int next = good.poll(); while(!good.isEmpty() && good.peek() - ans <= d) { next = good.poll(); } ans = next; count++; } System.out.println(ans+" "+count); } }
JAVA
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; int a[300], b[500001], m, n, d; bool used[51][500001], used1[500001]; void solve(int i, int s) { used1[s] = true; if (i > n) return; if (used[i][s]) return; used[i][s] = true; solve(i + 1, s); solve(i + 1, s + a[i]); } int main() { cin >> n >> d; for (int i = 1; i <= n; i++) cin >> a[i]; memset(used, false, sizeof(used)); memset(used1, false, sizeof(used1)); solve(1, 0); m = 0; for (int i = 0; i <= 500000; i++) if (used1[i]) b[++m] = i; int ans1 = 0, ans2 = 0; int i = 1; while (i <= m) { if (i == m || b[i + 1] > ans1 + d) { if (b[i] == ans1) break; ans1 = b[i]; ans2++; } else i++; } cout << ans1 << " " << ans2; return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; const int MAX_BUF_SIZE = 16384; char BUFOR[MAX_BUF_SIZE]; int BUF_SIZE, BUF_POS; char ZZZ; int _MINUS; const int MXN = 2000010; const int C = 262144; const int INF = 1000000001; const int MXS = 500010; bool knapsack[MXN]; int n, d; int c[MXN]; priority_queue<pair<int, int> > Q; int DP[MXN]; void test() { scanf("%d %d", &n, &d); for (int i = (1); i <= (n); i++) scanf("%d", &c[i]); knapsack[0] = 1; for (int i = (1); i <= (n); i++) for (int j = (MXS); j >= (0); j--) if (j - c[i] >= 0 && knapsack[j - c[i]] > 0) knapsack[j] = 1; Q.push(make_pair(0, 0)); int maks = 0, res = 0; for (int i = (1); i <= (MXS); i++) { if (!knapsack[i]) continue; while (!Q.empty()) { pair<int, int> p1 = Q.top(); p1 = make_pair(-p1.first, -p1.second); if (p1.second + d < i) Q.pop(); else break; } if (!Q.empty()) { pair<int, int> p1 = Q.top(); p1 = make_pair(-p1.first, -p1.second); Q.push(make_pair(-(p1.first + 1), -i)); maks = p1.first + 1; res = i; } } printf("%d %d\n", res, maks); } int main() { int te = 1; while (te--) test(); return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; const int maxn = 511111; int num[maxn]; int q[maxn]; int main() { int i, j, n, m, k; scanf("%d%d", &n, &m); memset(num, 0, sizeof(num)); num[0] = 1; for (i = 1; i <= n; i++) { scanf("%d", &k); for (j = 500000; j >= k; j--) { if (num[j - k]) num[j] = 1; } } int tot = 0; for (i = 0; i <= 500000; i++) if (num[i]) q[++tot] = i; q[0] = 0; int tt = 0; int ans = 0; q[++tot] = 1111111111; for (i = 1; i <= tot;) { if (q[i] - ans <= m) { i++; } else { i--; if (ans == q[i]) break; ans = q[i]; tt++; } } printf("%d %d\n", ans, tt); return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; const int MN = 50; const int MVAL = 500000; const int MM = 505050; bool can[MM]; int main() { int n, d; scanf("%d %d", &n, &d); can[0] = true; int i, j; for ((i) = 0; (i) < (int)(n); ++(i)) { int a; scanf("%d", &a); for (j = MVAL; j >= 0; --j) { if (can[j] && j + a <= MVAL) can[j + a] = true; } } int ans = 0, days = 0; while (true) { int cur = ans; for (i = cur + 1; i <= ans + d && i <= MVAL; ++i) if (can[i]) cur = i; if (cur == ans) break; ans = cur; ++days; } cout << ans << ' ' << days << endl; return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> int n, m, d; bool dp[55 * 10100]; int main() { dp[0] = true; scanf("%d %d", &n, &d); for (int i = 0; i < n; ++i) { int t; scanf("%d", &t); for (int j = m; j >= 0; --j) if (dp[j]) dp[j + t] = true; m += t; } int cur = 0, days = 0; bool changed = true; while (changed) { changed = false; for (int i = cur + d; i > cur; --i) { if (dp[i]) { cur = i; ++days; changed = true; break; } } } printf("%d %d", cur, days); return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; const int maxn = 55, maxv = 500005; int n, D, a[maxn], f[maxv], b[maxv], now, cnt; int main() { scanf("%d%d", &n, &D); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); f[0] = 1; for (int i = 1; i <= n; i++) for (int j = 500000; j >= a[i]; j--) f[j] |= f[j - a[i]]; for (int i = 0; i <= 500000; i++) if (f[i]) b[++b[0]] = i; b[b[0] + 1] = 1e9; now = 1; cnt = 0; for (int i = 2; i <= b[0]; i++) { if (b[i] <= b[now] + D && b[now] + D < b[i + 1]) cnt++, now = i; } printf("%d %d\n", b[now], cnt); return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; const int maxix = 60; const int maxnum = 1000 * 1000 * 1000 + 10; const int maxd = 50 * 10000 + 10000; int dp[maxd], mark[maxd]; int arr[maxix]; int main() { int n, d; cin >> n >> d; for (int i = 1; i <= n; i++) { cin >> arr[i]; } sort(arr + 1, arr + n + 1); int sum = 0; int all = 0; dp[0] = 1; for (int i = 0; i <= n; i++) { if (sum + d >= arr[i]) { sum += arr[i]; } for (int j = all; j >= 0; j--) { if (dp[j] == 1) dp[j + arr[i]] = 1; } all += arr[i]; } dp[all] = 1; for (int i = 0; i < 20; i++) cerr << dp[i] << " "; cerr << "\n"; cout << sum << " "; all = 0; int day = 0; while (true) { int index = 0; int now = all + d; for (int i = now; i >= all; i--) { if (dp[i] == 1) { index = i; break; } } if (index == all) break; all = index; day++; } cout << day; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; const int M = 500000 + 10; int a[M]; int dp[2][M]; vector<int> x; int main() { int n, d; cin >> n >> d; for (int i = 1; i <= n; i++) { cin >> a[i]; } dp[0][0] = 1; for (int i = 1; i <= n; i++) { int flag = i % 2; int pre = 1 - flag; for (int j = 0; j < M; j++) { int dp1 = dp[pre][j]; int dp2 = 0; if (j - a[i] >= 0) { dp2 = dp[pre][j - a[i]]; } int val = dp1 + dp2; dp[flag][j] = (val > 0); } } for (int j = 0; j < M; j++) { if (dp[n % 2][j] == 1) { x.push_back(j); } } int s = 0; int cnt = 0; while (true) { int v = s + d; int l = 0; int r = x.size() - 1; while (l < r) { int m = (l + r + 1) / 2; if (x[m] <= v) { l = m; } else { r = m - 1; } } if (x[l] == s) { break; } else { s = x[l]; cnt++; } } cout << s << " " << cnt << endl; return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; int p[50 * 10000 + 1]; int v[50]; int solve_problem() { vector<int> positions; int n, d; if (scanf("%d %d", &n, &d) != 2) return 1; for (int i = 0; i < n; i++) if (scanf("%d", &v[i]) != 1) return 1; int s = 0; for (int i = 0; i < n; i++) s += v[i]; sort(v, v + n); p[0] = 1; for (int i = 0; i < n; i++) for (int j = s; j >= 0; j--) if (p[j]) p[j + v[i]] = 1; for (int i = 0; i <= s; i++) if (p[i]) positions.push_back(i); int t = 0; int accum = 0; while (true) { vector<int>::iterator it = lower_bound(positions.begin(), positions.end(), accum + d); if (it != positions.begin()) { if (it == positions.end() || *it > accum + d) --it; } if (*it <= accum) break; accum = *it; ++t; } printf("%d %d\n", accum, t); return 0; } int main() { solve_problem(); return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; void solve(); int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed; cout.precision(12); solve(); return 0; } template <typename T> void sc(T& x) { cin >> x; } template <typename Head, typename... Tail> void sc(Head& head, Tail&... tail) { cin >> head; sc(tail...); } template <typename T> void pr(const T& x) { cout << x << '\n'; } template <typename Head, typename... Tail> void pr(const Head& head, const Tail&... tail) { cout << head << ' '; pr(tail...); } const int MAX_SUM = 5e5 + 10; bool vis[MAX_SUM]; void solve() { fill(vis, vis + MAX_SUM, false); int n, d; sc(n, d); vector<int> oks; oks.push_back(0); for (int i = 0; i < n; i++) { int x; sc(x); for (int j = ((int)oks.size()) - 1; j >= 0; j--) { if (x + oks[j] < MAX_SUM && !vis[x + oks[j]]) { oks.push_back(x + oks[j]); vis[x + oks[j]] = true; } } } sort(oks.begin(), oks.end()); int ans = 0, cur = 0; while (true) { int idx = upper_bound(oks.begin(), oks.end(), oks[cur] + d) - oks.begin() - 1; if (idx >= ((int)oks.size()) || idx == cur) { break; } ans++; cur = idx; } pr(oks[cur], ans); }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; int dp[500001]; int main() { int n, d, x = 0, i, j; int a[50]; scanf("%d %d", &n, &d); for (i = 0; i < n; i++) scanf("%d", &a[i]); dp[0] = 1; for (i = 0; i < n; i++) { for (j = 500000; j >= 0; j--) { if (dp[j] == 1) dp[j + a[i]] = 1; } } for (i = 0;; i++) { int y = 0; for (j = x + 1; j <= x + d && j <= 500000; j++) { if (dp[j] == 1) y = j; } if (y > x) { x = y; } else { printf("%d %d\n", x, i); return 0; } } return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; int n, d, a[55], f[500005][55], s[1000005]; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> n >> d; for (int i = 1; i <= n; i++) { cin >> a[i]; } fill(f[0], f[0] + n + 2, 1); vector<int> v(1); for (int i = 1; i < 500005; i++) { for (int j = 1; j <= n; j++) { f[i][j] = f[i][j - 1]; if (i >= a[j] && f[i - a[j]][j - 1]) { f[i][j] = 1; } } if (f[i][n]) { v.push_back(i); } } for (int i = 1;; i++) { s[i] = *(upper_bound(v.begin(), v.end(), s[i - 1] + d) - 1); if (s[i] == s[i - 1]) { cout << s[i - 1] << " " << i - 1 << "\n"; return 0; } } return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; stringstream ss; long long mod = 1000000007LL; int a[64], dp[500010]; int main() { int n, d; cin >> n >> d; dp[0] = 1; for (int i = 0; i < n; i++) { cin >> a[i]; for (int j = 500000; j >= a[i]; j--) if (dp[j - a[i]]) dp[j] = 1; } vector<int> cand; for (int i = 0; i <= 500000; i++) if (dp[i]) cand.push_back(i); int now = 0, last = -1, ans = 0, i = 0; while (now != last) { last = now; while (i < cand.size() && cand[i] - last <= d) now = cand[i++]; ans++; } cout << now << " " << ans - 1 << endl; return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; const double EPS = 0.0000001; const double PI = acos(-1); const long long INFLL = 0x7FFFFFFFFFFFFFFF; const int INF = 0x7FFFFFFF; template <typename T> inline void next(T &num) { char c; num = 0; do { c = getchar_unlocked(); } while (c != EOF && c == ' ' && c == '\n' && c == '\t'); int sign = (c == '-' ? -1 : 1); if (c != '-') num += (c - '0'); while ((c = getchar_unlocked()) != EOF && c != '\n' && c != '\t' && c != ' ') { num *= 10; num += (c - '0'); } num *= sign; } inline string getstr() { string str; char k; while ((k = getchar_unlocked()) == ' ' || k == '\n') { k = getchar_unlocked(); if (k == ' ' || k == '\n') continue; else break; } str.push_back(k); while ((k = getchar_unlocked()) != EOF && k != '\n' && k != '\t' && k != '\v' && k != '\0' && k != ' ') str.push_back(k); return str; } const int N = 500003; bitset<N> stat; vector<int> gotten; int x, n, d; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> d; gotten.push_back(0); while (n--) { cin >> x; for (int i = int(0), _b = int(gotten.size() - 1); i <= _b; i++) if (!stat[gotten[i] + x]) { stat[gotten[i] + x] = 1; gotten.push_back(gotten[i] + x); } } sort((gotten).begin(), (gotten).end()); int cur = 0, ans = 0; for (int i = 0; i < gotten.size(); ++ans) { while (i < gotten.size() && cur + d >= gotten[i]) ++i; if (gotten[i - 1] != cur) cur = gotten[i - 1]; else break; } cout << cur << ' ' << ans; return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; int main() { set<int> s; s.insert(0); int n, d; cin >> n >> d; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); for (int i = 0; i < n; i++) { set<int> temp; for (set<int>::iterator it = s.begin(); it != s.end(); it++) { if (s.find(*it + a[i]) == s.end()) temp.insert(*it + a[i]); } for (set<int>::iterator it = temp.begin(); it != temp.end(); it++) { s.insert(*it); } } int prev = 0; int c = 0; vector<int> v; s.insert(INT_MAX); for (set<int>::iterator it = s.begin(); it != s.end(); it++) { v.push_back(*it); } for (int i = 1; i < v.size(); i++) { if (v[i] > prev + d) { break; } while (v[i] <= prev + d) i++; i--; prev = v[i]; c++; } cout << prev << " " << c << endl; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; int q[505050], f[505050], g[505050], a[505050]; int main() { int n, D; scanf("%d%d", &n, &D); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); int m = 0; for (int i = 1; i <= n; i++) m += a[i]; memset(g, 0, sizeof(g)); g[0] = 1; for (int i = 1; i <= n; i++) for (int j = m; j >= a[i]; j--) g[j] |= g[j - a[i]]; memset(f, 0x3f3f3f3f, sizeof(f)); f[0] = 0; int l = 0, r = 0; q[r++] = 0; int ans = m; for (int i = 1; i <= m; i++) { if (!g[i]) continue; while (l < r && q[l] + D < i) l++; if (l == r) break; f[i] = f[q[l]] + 1; while (l < r && f[q[r]] >= f[i]) r--; q[r++] = i; } while (f[ans] == 0x3f3f3f3f) ans--; printf("%d %d\n", ans, f[ans]); return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; bool used[50]; int n; int ss[500001], v[50]; int main(void) { int d; int i, ans = 0, sum = 0, j; scanf("%d%d", &n, &d); for (i = 0; i < n; i++) { scanf("%d", &v[i]); sum += v[i]; } ss[0] = true; for (i = 0; i < n; i++) for (j = sum; j >= v[i]; j--) if (ss[j - v[i]]) ss[j] = true; int curr = 0, next = 0; while (curr < sum) { for (i = curr + 1; i <= min(sum, curr + d); i++) if (ss[i]) next = i; if (curr == next) break; ans++; curr = next; } printf("%d %d\n", curr, ans); }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:268435456,268435456") using namespace std; bool dp[510001]; int main() { int n, d, t; scanf("%d %d", &n, &d), dp[0] = 1; for (int i = 0; i < n; i++) { scanf("%d", &t); for (int j = 510000; j - t >= 0; j--) dp[j] |= dp[j - t]; } int curr = 0, num = 0, sum = 0; while (true) { for (int i = d; i >= 1; i--) if (dp[curr + i]) { curr += i, num++; break; } if (curr > sum) sum = curr; else break; } printf("%d %d\n", sum, num); return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; const int MAXN = 10000 * 55; const long long LINF = 1000000000LL * 1000000000LL; bool f[MAXN], fn[MAXN]; long long ff[MAXN]; int i, j, n, w, d, a[111]; map<long long, int> x; int main() { cin >> n >> d; for (i = 0; i < n; i++) { cin >> a[i]; w += a[i]; } f[0] = true; for (i = 1; i <= n; i++) { int nowW = a[i - 1]; memset(fn, 0, sizeof(fn)); for (j = 0; j <= w; j++) { fn[j] |= f[j]; if (j - nowW >= 0) fn[j] |= f[j - nowW]; } memcpy(f, fn, sizeof(f)); } for (i = 0; i <= w; i++) ff[i] = LINF; ff[0] = 0; int l = 0, r; x[ff[0]]++; int ans = 0; for (i = 1; i <= w; i++) { r = i; while (r - l > d) { x[ff[l]]--; if (x[ff[l]] == 0) { x.erase(ff[l]); } l++; } if (!f[r]) { x[ff[r]]++; continue; } long long tmp = x.begin()->first; if (tmp < LINF) { ans = r; ff[r] = tmp + 1; } x[ff[r]]++; } cout << ans << ' ' << ff[ans]; return 0; }
CPP
365_D. Free Market
John Doe has recently found a "Free Market" in his city β€” that is the place where you can exchange some of your possessions for other things for free. John knows that his city has n items in total (each item is unique). You can bring any number of items to the market and exchange them for any other one. Note that each item is one of a kind and that means that you cannot exchange set {a, b} for set {v, a}. However, you can always exchange set x for any set y, unless there is item p, such that p occurs in x and p occurs in y. For each item, John knows its value ci. John's sense of justice doesn't let him exchange a set of items x for a set of items y, if s(x) + d < s(y) (s(x) is the total price of items in the set x). During one day John can exchange only one set of items for something else. Initially, he has no items. John wants to get a set of items with the maximum total price. Find the cost of such set and the minimum number of days John can get it in. Input The first line contains two space-separated integers n, d (1 ≀ n ≀ 50, 1 ≀ d ≀ 104) β€” the number of items on the market and John's sense of justice value, correspondingly. The second line contains n space-separated integers ci (1 ≀ ci ≀ 104). Output Print two space-separated integers: the maximum possible price in the set of items John can get and the minimum number of days needed to get such set. Examples Input 3 2 1 3 10 Output 4 3 Input 3 5 1 2 3 Output 6 2 Input 10 10000 10000 9999 1 10000 10000 10000 1 2 3 4 Output 50010 6 Note In the first sample John can act like this: * Take the first item (1 - 0 ≀ 2). * Exchange the first item for the second one (3 - 1 ≀ 2). * Take the first item (1 - 0 ≀ 2).
2
10
#include <bits/stdc++.h> using namespace std; int n, d, a[60], t; bool best[500100]; vector<int> s; int main() { cin >> n >> d; for (int i = 1; i <= n; ++i) { cin >> a[i]; t += a[i]; } best[0] = 1; for (int i = 1; i <= n; ++i) for (int j = t - a[i]; j >= 0; --j) { best[j + a[i]] = max(best[j], best[j + a[i]]); } for (int i = 0; i <= t; ++i) if (best[i] == 1) { s.push_back(i); } int x = 0, y = 0, z = 0, rez = 0; while (s[x] + d >= s[x + 1] && x < s.size() - 1) { ++x; y = s[x] - z; if (y == d) { ++rez; z = s[x]; } else if (y > d) { ++rez; z = s[x - 1]; } } if (x && s[x] - z < d && s[x] - z > 0) ++rez; cout << s[x] << " " << rez; 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 long long mod = 1000000007; const long long INF = 1e18; int main() { double n, i, k, m = 101; cin >> n; vector<long long> a(n, 0); vector<double> b(101, 0); for (i = 0; i < (n); i++) { cin >> a[i]; b[a[i]]++; } double x = 0, y = 0; for (i = 0; i < (101); i++) { if (b[i] >= y) { b[i] -= y; y = 0; } else { y -= b[i]; b[i] = 0; } if (b[i] >= x) { y += ((i + 1) * ceil((b[i] - x) / (i + 1))) - (b[i] - x); x += ceil((b[i] - x) / (i + 1)); } else { y += (x - b[i]); } } cout << x; return 0; }
CPP
389_C. Fox and Box Accumulation
Fox Ciel has n boxes in her room. They have the same size and weight, but they might have different strength. The i-th box can hold at most xi boxes on its top (we'll call xi the strength of the box). Since all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For example, imagine Ciel has three boxes: the first has strength 2, the second has strength 1 and the third has strength 1. She cannot put the second and the third box simultaneously directly on the top of the first one. But she can put the second box directly on the top of the first one, and then the third box directly on the top of the second one. We will call such a construction of boxes a pile. <image> Fox Ciel wants to construct piles from all the boxes. Each pile will contain some boxes from top to bottom, and there cannot be more than xi boxes on the top of i-th box. What is the minimal number of piles she needs to construct? Input The first line contains an integer n (1 ≀ n ≀ 100). The next line contains n integers x1, x2, ..., xn (0 ≀ xi ≀ 100). Output Output a single integer β€” the minimal possible number of piles. Examples Input 3 0 0 10 Output 2 Input 5 0 1 2 3 4 Output 1 Input 4 0 0 0 0 Output 4 Input 9 0 1 0 2 0 1 1 2 10 Output 3 Note In example 1, one optimal way is to build 2 piles: the first pile contains boxes 1 and 3 (from top to bottom), the second pile contains only box 2. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; long long i, j, k, l, n, a, b, ans = 0; int main() { std::ios_base::sync_with_stdio(0); cin >> n; vector<long long> num(n); for (i = 0; i < n; i++) { cin >> num[i]; } sort(num.begin(), num.end()); for (i = 0; i < n; i++) { if (num[i] == -1) continue; ans++; k = 1; for (j = i + 1; j < n; j++) { if (num[j] == -1 || k > num[j]) continue; num[j] = -1; k++; } } 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 main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long a; cin >> a; vector<long long> x; for (long long i = 0; i < a; i++) { long long b; cin >> b; x.push_back(b); } bool done[101] = {false}; sort(x.begin(), x.end()); long long counter = 0; long long ans = 0; while (counter != a) { long long num = 0; for (long long i = 0; i < a; i++) { if (x[i] >= num && !done[i]) { num++; counter++; done[i] = true; } } ans++; } cout << ans << "\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
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector<int> v(n); for (long long int i = 0; i < n; i++) cin >> v[i]; sort(v.begin(), v.end()); int ans = 0; while (!v.empty()) { ans++; int num = 0; for (long long int i = 0; i < v.size(); i++) { if (v[i] >= num) { num++; v.erase(v.begin() + i); i--; } } } cout << ans; return 0; }
CPP
389_C. Fox and Box Accumulation
Fox Ciel has n boxes in her room. They have the same size and weight, but they might have different strength. The i-th box can hold at most xi boxes on its top (we'll call xi the strength of the box). Since all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For example, imagine Ciel has three boxes: the first has strength 2, the second has strength 1 and the third has strength 1. She cannot put the second and the third box simultaneously directly on the top of the first one. But she can put the second box directly on the top of the first one, and then the third box directly on the top of the second one. We will call such a construction of boxes a pile. <image> Fox Ciel wants to construct piles from all the boxes. Each pile will contain some boxes from top to bottom, and there cannot be more than xi boxes on the top of i-th box. What is the minimal number of piles she needs to construct? Input The first line contains an integer n (1 ≀ n ≀ 100). The next line contains n integers x1, x2, ..., xn (0 ≀ xi ≀ 100). Output Output a single integer β€” the minimal possible number of piles. Examples Input 3 0 0 10 Output 2 Input 5 0 1 2 3 4 Output 1 Input 4 0 0 0 0 Output 4 Input 9 0 1 0 2 0 1 1 2 10 Output 3 Note In example 1, one optimal way is to build 2 piles: the first pile contains boxes 1 and 3 (from top to bottom), the second pile contains only box 2. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import sys inp = [map(int, i.split(' ')) for i in sys.stdin.read().strip().splitlines()] dat = sorted(inp[1]) res = 0 while dat: chain = 0 for i in xrange(len(dat)): if dat[i] >= chain: chain += 1 dat[i] = -1 res += 1 dat = filter(lambda x: x >= 0, dat) sys.stdout.write(str(res))
PYTHON
389_C. Fox and Box Accumulation
Fox Ciel has n boxes in her room. They have the same size and weight, but they might have different strength. The i-th box can hold at most xi boxes on its top (we'll call xi the strength of the box). Since all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For example, imagine Ciel has three boxes: the first has strength 2, the second has strength 1 and the third has strength 1. She cannot put the second and the third box simultaneously directly on the top of the first one. But she can put the second box directly on the top of the first one, and then the third box directly on the top of the second one. We will call such a construction of boxes a pile. <image> Fox Ciel wants to construct piles from all the boxes. Each pile will contain some boxes from top to bottom, and there cannot be more than xi boxes on the top of i-th box. What is the minimal number of piles she needs to construct? Input The first line contains an integer n (1 ≀ n ≀ 100). The next line contains n integers x1, x2, ..., xn (0 ≀ xi ≀ 100). Output Output a single integer β€” the minimal possible number of piles. Examples Input 3 0 0 10 Output 2 Input 5 0 1 2 3 4 Output 1 Input 4 0 0 0 0 Output 4 Input 9 0 1 0 2 0 1 1 2 10 Output 3 Note In example 1, one optimal way is to build 2 piles: the first pile contains boxes 1 and 3 (from top to bottom), the second pile contains only box 2. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#------------------------template--------------------------# import os import sys from math import * from collections import * from fractions import * from bisect import * from heapq import* from io import BytesIO, IOBase def vsInput(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") def value():return tuple(map(int,input().split())) def array():return [int(i) for i in input().split()] def Int():return int(input()) def Str():return input() def arrayS():return [i for i in input().split()] #-------------------------code---------------------------# #vsInput() def possible(s): last={i:a[i] for i in range(s)} for i in range(s,n,s): for j in range(0,s): if(last[j]==0): return False else: last[j]=min(last[j]-1,a[i+j]) if(i+j+1==n): break return True n=Int() a=sorted(array(),reverse=True) low=1 high=n while(low<=high): mid=low+(high-low)//2 if(possible(mid)): ans=mid high=mid-1 else: low=mid+1 print(ans)
PYTHON3
389_C. Fox and Box Accumulation
Fox Ciel has n boxes in her room. They have the same size and weight, but they might have different strength. The i-th box can hold at most xi boxes on its top (we'll call xi the strength of the box). Since all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For example, imagine Ciel has three boxes: the first has strength 2, the second has strength 1 and the third has strength 1. She cannot put the second and the third box simultaneously directly on the top of the first one. But she can put the second box directly on the top of the first one, and then the third box directly on the top of the second one. We will call such a construction of boxes a pile. <image> Fox Ciel wants to construct piles from all the boxes. Each pile will contain some boxes from top to bottom, and there cannot be more than xi boxes on the top of i-th box. What is the minimal number of piles she needs to construct? Input The first line contains an integer n (1 ≀ n ≀ 100). The next line contains n integers x1, x2, ..., xn (0 ≀ xi ≀ 100). Output Output a single integer β€” the minimal possible number of piles. Examples Input 3 0 0 10 Output 2 Input 5 0 1 2 3 4 Output 1 Input 4 0 0 0 0 Output 4 Input 9 0 1 0 2 0 1 1 2 10 Output 3 Note In example 1, one optimal way is to build 2 piles: the first pile contains boxes 1 and 3 (from top to bottom), the second pile contains only box 2. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class CSolver { private static InputReader in; private static PrintWriter out; public static void main(String[] args) throws IOException { InputStream is = System.in; PrintWriter pw = new PrintWriter(System.out); init(is, pw); solve(); is.close(); out.close(); } public static void init(InputStream is, PrintWriter pw) { in = new InputReader(is); out = pw; } static int n; static int[] boxesCount; public static void solve() { int n = in.ni(); boxesCount = new int[101]; for (int i = 0; i < n; i++) { boxesCount[in.ni()]++; } int count = 0; for (; !arePiled(); count++) { int boxesCountSoFar = 0; for (int i = 0; i <= 100; i++) { if (boxesCount[i] > 0) { int canLift = Math.min(i - boxesCountSoFar, boxesCount[i] - 1); // System.out.println(i + " " + canLift); boxesCountSoFar += 1 + canLift; boxesCount[i] -= 1 + canLift; } } } out.println(count); } static boolean arePiled() { for (int i = 0; i <= 100; i++) { if (boxesCount[i] > 0) { return false; } } return true; } private static class InputReader { BufferedReader br; StringTokenizer st; InputReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } int[] na(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = ni(); } return arr; } } }
JAVA
389_C. Fox and Box Accumulation
Fox Ciel has n boxes in her room. They have the same size and weight, but they might have different strength. The i-th box can hold at most xi boxes on its top (we'll call xi the strength of the box). Since all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For example, imagine Ciel has three boxes: the first has strength 2, the second has strength 1 and the third has strength 1. She cannot put the second and the third box simultaneously directly on the top of the first one. But she can put the second box directly on the top of the first one, and then the third box directly on the top of the second one. We will call such a construction of boxes a pile. <image> Fox Ciel wants to construct piles from all the boxes. Each pile will contain some boxes from top to bottom, and there cannot be more than xi boxes on the top of i-th box. What is the minimal number of piles she needs to construct? Input The first line contains an integer n (1 ≀ n ≀ 100). The next line contains n integers x1, x2, ..., xn (0 ≀ xi ≀ 100). Output Output a single integer β€” the minimal possible number of piles. Examples Input 3 0 0 10 Output 2 Input 5 0 1 2 3 4 Output 1 Input 4 0 0 0 0 Output 4 Input 9 0 1 0 2 0 1 1 2 10 Output 3 Note In example 1, one optimal way is to build 2 piles: the first pile contains boxes 1 and 3 (from top to bottom), the second pile contains only box 2. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.IOException; import java.util.StringTokenizer; import java.io.InputStream; import java.io.InputStreamReader; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } } class TaskC { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[] a = new int[101]; for (int i = 0; i < n; i++) { a[in.nextInt()]++; } boolean allZero = false; int ans = 0; while (!allZero) { int boxes = 0; ans++; allZero = true; for (int i = 0; i <= 100; i++) { while (a[i] > 0 && boxes <= i) { a[i]--; boxes++; } if (a[i] > 0) { allZero = false; } } } out.print(ans); } } 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 v[110]; int cnt[110]; vector<int> a; int main() { int test; int n; cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; if (v[x] == false) { v[x] = true; } a.push_back(x); cnt[x]++; } int l = a.size(); vector<int> v1; sort(a.begin(), a.end()); int ans = 0; for (int k = 0; k < l; k++) { int i = a[k]; int kk = 0; int x = v1.size(); while (kk < x) { if (v1[kk] <= i) break; kk++; } if (kk < x) v1[kk]++; else v1.push_back(1); } cout << v1.size() << 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
input();print(max([((ind+1)//(i+1)+((ind+1)%(i+1)!=0)) for ind,i in enumerate(sorted(map(int,input().split())))]))
PYTHON3
389_C. Fox and Box Accumulation
Fox Ciel has n boxes in her room. They have the same size and weight, but they might have different strength. The i-th box can hold at most xi boxes on its top (we'll call xi the strength of the box). Since all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For example, imagine Ciel has three boxes: the first has strength 2, the second has strength 1 and the third has strength 1. She cannot put the second and the third box simultaneously directly on the top of the first one. But she can put the second box directly on the top of the first one, and then the third box directly on the top of the second one. We will call such a construction of boxes a pile. <image> Fox Ciel wants to construct piles from all the boxes. Each pile will contain some boxes from top to bottom, and there cannot be more than xi boxes on the top of i-th box. What is the minimal number of piles she needs to construct? Input The first line contains an integer n (1 ≀ n ≀ 100). The next line contains n integers x1, x2, ..., xn (0 ≀ xi ≀ 100). Output Output a single integer β€” the minimal possible number of piles. Examples Input 3 0 0 10 Output 2 Input 5 0 1 2 3 4 Output 1 Input 4 0 0 0 0 Output 4 Input 9 0 1 0 2 0 1 1 2 10 Output 3 Note In example 1, one optimal way is to build 2 piles: the first pile contains boxes 1 and 3 (from top to bottom), the second pile contains only box 2. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.util.*;import java.io.*;import java.math.*; public class Main { public static void process()throws IOException { int n=ni(); int[]A=nai(n); Arrays.sort(A); int ans=1; for(int i=1;i<n;i++) if(A[i]<(i/ans)) ans++; pn(ans); } static AnotherReader sc; static PrintWriter out; public static void main(String[]args)throws IOException { boolean oj = System.getProperty("ONLINE_JUDGE") != null; if(oj){sc=new AnotherReader();out=new PrintWriter(System.out);} else{sc=new AnotherReader(100);out=new PrintWriter("output.txt");} int t=1; // t=ni(); while(t-->0) {process();} out.flush();out.close(); } static void pn(Object o){out.println(o);} static void p(Object o){out.print(o);} static void pni(Object o){out.println(o);out.flush();} static int ni()throws IOException{return sc.nextInt();} static long nl()throws IOException{return sc.nextLong();} static double nd()throws IOException{return sc.nextDouble();} static String nln()throws IOException{return sc.nextLine();} static int[] nai(int N)throws IOException{int[]A=new int[N];for(int i=0;i!=N;i++){A[i]=ni();}return A;} static long[] nal(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;} static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);} static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));} ///////////////////////////////////////////////////////////////////////////////////////////////////////// static class AnotherReader{BufferedReader br; StringTokenizer st; AnotherReader()throws FileNotFoundException{ br=new BufferedReader(new InputStreamReader(System.in));} AnotherReader(int a)throws FileNotFoundException{ br = new BufferedReader(new FileReader("input.txt"));} String next()throws IOException{ while (st == null || !st.hasMoreElements()) {try{ st = new StringTokenizer(br.readLine());} catch (IOException e){ e.printStackTrace(); }} return st.nextToken(); } int nextInt() throws IOException{ return Integer.parseInt(next());} long nextLong() throws IOException {return Long.parseLong(next());} double nextDouble()throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException{ String str = ""; try{ str = br.readLine();} catch (IOException e){ e.printStackTrace();} return str;}} ///////////////////////////////////////////////////////////////////////////////////////////////////////////// }
JAVA
389_C. Fox and Box Accumulation
Fox Ciel has n boxes in her room. They have the same size and weight, but they might have different strength. The i-th box can hold at most xi boxes on its top (we'll call xi the strength of the box). Since all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For example, imagine Ciel has three boxes: the first has strength 2, the second has strength 1 and the third has strength 1. She cannot put the second and the third box simultaneously directly on the top of the first one. But she can put the second box directly on the top of the first one, and then the third box directly on the top of the second one. We will call such a construction of boxes a pile. <image> Fox Ciel wants to construct piles from all the boxes. Each pile will contain some boxes from top to bottom, and there cannot be more than xi boxes on the top of i-th box. What is the minimal number of piles she needs to construct? Input The first line contains an integer n (1 ≀ n ≀ 100). The next line contains n integers x1, x2, ..., xn (0 ≀ xi ≀ 100). Output Output a single integer β€” the minimal possible number of piles. Examples Input 3 0 0 10 Output 2 Input 5 0 1 2 3 4 Output 1 Input 4 0 0 0 0 Output 4 Input 9 0 1 0 2 0 1 1 2 10 Output 3 Note In example 1, one optimal way is to build 2 piles: the first pile contains boxes 1 and 3 (from top to bottom), the second pile contains only box 2. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.io.FileReader; import java.io.IOException; import java.math.BigInteger; import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; public class CodeForces { class pairT implements Comparable<pairT>{ int count; int can; @Override public int compareTo(pairT o) { return can - o.can; } } public static void main(String[] args) throws NumberFormatException, IOException, ParseException { new CodeForces(); } public CodeForces() throws NumberFormatException, IOException, ParseException { // Scanner sc = new Scanner(new FileReader("input.txt")); Scanner sc = new Scanner(System.in); int n; n = sc.nextInt(); ArrayList<pairT> arr = new ArrayList<pairT>(); for(int i=0;i<n;i++){ pairT p = new pairT(); p.can = sc.nextInt(); p.count = 1; arr.add(p); } Collections.sort(arr); for(int i=0;i<arr.size();i++){ for(int j=0;j<arr.size();j++){ if(i!=j){ pairT p1 = arr.get(i); pairT p2 = arr.get(j); if(p2.can>=p1.count){ p2.can = minim(p1.can, p2.can-p1.count); p2.count+=p1.count; arr.remove(i); i=-1; Collections.sort(arr); break; } } } } System.out.println(arr.size()); sc.close(); } private int mod(int i) { if (i < 0) { return -i; } return i; } private int minim(int a, int b) { if (a > b) { return b; } return a; } private int maxim(int a, int b) { if (a > b) { return a; } return b; } }
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 used[110]; int main() { int n, a[110], ans = 0; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); while (true) { int i; for (i = 0; i < n; i++) if (!used[i]) break; if (i == n) break; ans++; int h = 0; for (i = 0; i < n; i++) { if (!used[i] && a[i] >= h) { used[i] = 1; ++h; } } } cout << ans; return 0; }
CPP
389_C. Fox and Box Accumulation
Fox Ciel has n boxes in her room. They have the same size and weight, but they might have different strength. The i-th box can hold at most xi boxes on its top (we'll call xi the strength of the box). Since all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For example, imagine Ciel has three boxes: the first has strength 2, the second has strength 1 and the third has strength 1. She cannot put the second and the third box simultaneously directly on the top of the first one. But she can put the second box directly on the top of the first one, and then the third box directly on the top of the second one. We will call such a construction of boxes a pile. <image> Fox Ciel wants to construct piles from all the boxes. Each pile will contain some boxes from top to bottom, and there cannot be more than xi boxes on the top of i-th box. What is the minimal number of piles she needs to construct? Input The first line contains an integer n (1 ≀ n ≀ 100). The next line contains n integers x1, x2, ..., xn (0 ≀ xi ≀ 100). Output Output a single integer β€” the minimal possible number of piles. Examples Input 3 0 0 10 Output 2 Input 5 0 1 2 3 4 Output 1 Input 4 0 0 0 0 Output 4 Input 9 0 1 0 2 0 1 1 2 10 Output 3 Note In example 1, one optimal way is to build 2 piles: the first pile contains boxes 1 and 3 (from top to bottom), the second pile contains only box 2. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.io.*; import java.util.*; import java.math.*; public class Main { static BufferedReader in; static PrintWriter out; static StringTokenizer tok; static void solve() throws Exception { int n = nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } Arrays.sort(a); boolean[] used = new boolean[n]; int[] c = new int[n]; for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { if (!used[j] && a[j]+1 <= a[i]) { c[i] = c[j]; a[i] = a[j]+1; used[j] = true; break; } } if (c[i] == 0) { c[i] = i+1; a[i] = 0; } } Set<Integer> res = new HashSet<Integer>(); for (int i = 0; i < n; i++) { res.add(c[i]); } out.println(res.size()); } static int sqr(int x) { return x*x; } static int nextInt() throws IOException { return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static BigInteger nextBigInteger() throws IOException { return new BigInteger(next()); } static String next() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } static String nextLine() throws IOException { tok = new StringTokenizer(""); return in.readLine(); } static boolean hasNext() throws IOException { while (tok == null || !tok.hasMoreTokens()) { String s = in.readLine(); if (s == null) { return false; } else { tok = new StringTokenizer(s); } } return true; } public static void main(String args[]) { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); //in = new BufferedReader(new FileReader("input.in")); //out = new PrintWriter(new FileWriter("output.out")); solve(); in.close(); out.close(); } catch (Throwable e) { e.printStackTrace(); java.lang.System.exit(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
#include <bits/stdc++.h> using namespace std; int a[105], b[105]; bool cmp(int x, int y) { return x > y; } int main() { int t, i, n, j; cin >> n; for (i = 1; i <= n; i++) { scanf("%d", &a[i]); } sort(a + 1, a + 1 + n, cmp); for (t = 1;; t++) { memset(b, -1, sizeof(b)); bool flag = 0; for (i = 1, j = 1; i <= n; i++) { if (b[j] == 0) { flag = 1; break; } if (i <= t) b[j] = a[i]; else b[j] = min(a[i], b[j] - 1); j++; if (j == t + 1) j = 1; } if (flag == 0) break; } cout << t << 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; int a[101]; int b[101]; vector<int> x; int res; void Init() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; b[a[i]]++; } } void AB() { for (int i = 0; i <= 100; i++) { while (b[i] > 0 && i >= x.size()) { x.push_back(i); b[i]--; } } if (x.size() > 0) res++; } bool check() { for (int i = 0; i <= 100; i++) { if (b[i] != 0) return false; } return true; } void Solve() { while (1) { x.clear(); AB(); if (check()) { cout << res; return; } } } int main() { Init(); Solve(); }
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 = raw_input() n = int(n) boxes = raw_input() boxes = boxes.split() boxes = map(int, boxes) piles = [] while len(boxes) > 0: pile = [] s = max(boxes) boxes.remove(s) if s != 0: temp_boxes = boxes while len(temp_boxes) > 0 and len(pile)<s: smallest = min(temp_boxes) boxes.remove(smallest) pile.append(smallest) temp_boxes = [x for x in boxes if x >= len(pile)] pile.append(s) piles.append(pile) print len(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
''' Created on 01-Jul-2017 @author: kandarp ''' import sys def check(x,a): if x ==0 : return False; b =[] ans = True; for i in xrange(0,x): b.append([]); for i in xrange(0,len(a)): b[i%x].append(a[i]) for i in xrange(0,x): for j in xrange(0,len(b[i])): if b[i][j] < (len(b[i]) - (j+1)): ans = False; return ans; return ans; n = int(raw_input()) a = [int(x) for x in raw_input().split()] a.sort(reverse=True) if n==1: print(1) sys.exit(0); current_watch = 0; l=0; r=len(a); ans =1; while(r - l >1): mid = int((l + r)/2); if(check(mid, a)): r = mid; else: l = mid; if(check(l,a)): print(l); else: print(r);
PYTHON
389_C. Fox and Box Accumulation
Fox Ciel has n boxes in her room. They have the same size and weight, but they might have different strength. The i-th box can hold at most xi boxes on its top (we'll call xi the strength of the box). Since all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For example, imagine Ciel has three boxes: the first has strength 2, the second has strength 1 and the third has strength 1. She cannot put the second and the third box simultaneously directly on the top of the first one. But she can put the second box directly on the top of the first one, and then the third box directly on the top of the second one. We will call such a construction of boxes a pile. <image> Fox Ciel wants to construct piles from all the boxes. Each pile will contain some boxes from top to bottom, and there cannot be more than xi boxes on the top of i-th box. What is the minimal number of piles she needs to construct? Input The first line contains an integer n (1 ≀ n ≀ 100). The next line contains n integers x1, x2, ..., xn (0 ≀ xi ≀ 100). Output Output a single integer β€” the minimal possible number of piles. Examples Input 3 0 0 10 Output 2 Input 5 0 1 2 3 4 Output 1 Input 4 0 0 0 0 Output 4 Input 9 0 1 0 2 0 1 1 2 10 Output 3 Note In example 1, one optimal way is to build 2 piles: the first pile contains boxes 1 and 3 (from top to bottom), the second pile contains only box 2. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.util.Arrays; import java.util.Scanner; public class FoxAndBox { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] x = new int[n]; for(int i=0; i<n; i++){ x[i] = sc.nextInt(); } System.out.println(solve(x)); sc.close(); } static int solve(int[] x){ int[] sorted = Arrays.stream(x).boxed().sorted().mapToInt(p -> p).toArray(); boolean[] isUsed = new boolean[x.length]; int result = 0; int len = isUsed.length; while(true){ int length = 0; for(int i=0; i<isUsed.length; i++){ if(!isUsed[i] && sorted[i] >= length){ isUsed[i] = true; length++; } } if(length == 0){ break; } result++; } return result++; } }
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()) li=list(map(int,input().split(" ",n)[:n])) li.sort() ans=1 for i in range(n): if li[i]< i//ans: 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
import java.util.Scanner; import java.io.OutputStream; import java.io.IOException; import java.io.PrintWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } } class TaskC { public void solve(int testNumber, Scanner in, PrintWriter out) { int n = in.nextInt(); int[] x = IOUtils.readIntArray(in, n); boolean[] used = new boolean[n]; int res = 0; while (notAllUsed(used)) { res++; int count = 0; int index = find(x, used, count); while (index >= 0) { count++; used[index] = true; index = find(x, used, count); } } out.print(res); } private int find(int[] x, boolean[] used, int count) { int minIndex = -1; int min = Integer.MAX_VALUE; for (int i = 0; i < used.length; ++i) if (!used[i]) { if (count > 0 && x[i] == 0) continue; if (x[i] >= count && x[i] < min) { min = x[i]; minIndex = i; } } return minIndex; } private boolean notAllUsed(boolean[] used) { for (int i = 0; i < used.length; ++i) if (!used[i]) return true; return false; } } class IOUtils { public static int[] readIntArray(Scanner in, int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = in.nextInt(); return array; } }
JAVA
389_C. Fox and Box Accumulation
Fox Ciel has n boxes in her room. They have the same size and weight, but they might have different strength. The i-th box can hold at most xi boxes on its top (we'll call xi the strength of the box). Since all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For example, imagine Ciel has three boxes: the first has strength 2, the second has strength 1 and the third has strength 1. She cannot put the second and the third box simultaneously directly on the top of the first one. But she can put the second box directly on the top of the first one, and then the third box directly on the top of the second one. We will call such a construction of boxes a pile. <image> Fox Ciel wants to construct piles from all the boxes. Each pile will contain some boxes from top to bottom, and there cannot be more than xi boxes on the top of i-th box. What is the minimal number of piles she needs to construct? Input The first line contains an integer n (1 ≀ n ≀ 100). The next line contains n integers x1, x2, ..., xn (0 ≀ xi ≀ 100). Output Output a single integer β€” the minimal possible number of piles. Examples Input 3 0 0 10 Output 2 Input 5 0 1 2 3 4 Output 1 Input 4 0 0 0 0 Output 4 Input 9 0 1 0 2 0 1 1 2 10 Output 3 Note In example 1, one optimal way is to build 2 piles: the first pile contains boxes 1 and 3 (from top to bottom), the second pile contains only box 2. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.util.*; public class cf388A { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int boxes = sc.nextInt(); int[] strengths = new int[101]; for(int i = 0; i < boxes; i++) { strengths[sc.nextInt()]++; } int[] piles = new int[100]; int nPiles = 0; for(int i = 0; i < 101; i++) { for(int j = 0; j < strengths[i]; j++) { int p = 0; // System.out.println(i); while(piles[p] > i) { p++; } if(piles[p] == 0) { nPiles++; } piles[p]++; } } System.out.println(nPiles); } }
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, count = 0; int arr[200]; cin >> n; for (int i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); vector<int> ans; int k = 0; int l; ans.push_back(1); for (int i = 1; i < n; i++) { if (arr[i] == 0) ans.push_back(1); else { l = ans.size() - 1; while (l >= 0) { if (ans[l] <= arr[i]) { ans[l]++; break; } l--; } if (l < 0) ans.push_back(1); } } cout << ans.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
import math from decimal import * getcontext().prec=30 n=int(input()) x=list(map(int,input().split())) x.sort() l=list() for i in range(len(x)): added=0 for j in range(len(l)): if x[i]>=len(l[j]): l[j].append(x[i]) added=1 break if not added: v=[x[i]] l.append(v) print(len(l))
PYTHON3
389_C. Fox and Box Accumulation
Fox Ciel has n boxes in her room. They have the same size and weight, but they might have different strength. The i-th box can hold at most xi boxes on its top (we'll call xi the strength of the box). Since all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For example, imagine Ciel has three boxes: the first has strength 2, the second has strength 1 and the third has strength 1. She cannot put the second and the third box simultaneously directly on the top of the first one. But she can put the second box directly on the top of the first one, and then the third box directly on the top of the second one. We will call such a construction of boxes a pile. <image> Fox Ciel wants to construct piles from all the boxes. Each pile will contain some boxes from top to bottom, and there cannot be more than xi boxes on the top of i-th box. What is the minimal number of piles she needs to construct? Input The first line contains an integer n (1 ≀ n ≀ 100). The next line contains n integers x1, x2, ..., xn (0 ≀ xi ≀ 100). Output Output a single integer β€” the minimal possible number of piles. Examples Input 3 0 0 10 Output 2 Input 5 0 1 2 3 4 Output 1 Input 4 0 0 0 0 Output 4 Input 9 0 1 0 2 0 1 1 2 10 Output 3 Note In example 1, one optimal way is to build 2 piles: the first pile contains boxes 1 and 3 (from top to bottom), the second pile contains only box 2. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> int b[110], vis[110]; int cmp(const void *a, const void *b) { return *(int *)a - *(int *)b; } int main(void) { int n, i, cur, k, pile; while (scanf("%d%*c", &n) != EOF) { for (i = 0; i < n; i++) scanf("%d", &b[i]); qsort(b, n, 4, cmp); memset(vis, 0, sizeof(vis)); k = 0; pile = 0; while (k < n) { cur = 0; pile++; for (i = 0; i < n; i++) if (!vis[i] && cur <= b[i]) { vis[i] = 1; cur++; k++; } } printf("%d\n", pile); } 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.*; import java.io.*; //DONT FORGET TO CHANGE CLASS NAME! public class FoxAndBoxAccumulation { /* First, we need to put all of our box strengths into an array. We can then sort the array from least to greatest Finally, we have a linked list to keep track of the number of boxes in each pile Remember, when placing a box, it only matters what the size is of the piles we have currently made Thus we can place our weakest boxes first, trying to place them under any piles they can support, and if there are no such piles, we create a new pile. Since we go from least to greatest, we are garunteed to always have an optimal solution */ static boolean testing = false; //change to false on submission static String name = "FoxAndBoxAccumulation"; // insert the class name here public static void main(String[] args) throws IOException { Scanner input; PrintWriter writer; if(testing){input = new Scanner(new FileReader(name+".in"));writer = new PrintWriter(name+".out");}else{input = new Scanner(System.in);writer = new PrintWriter(System.out);} //Declare Variables StringTokenizer tokenizer; int numBoxes, i, k; int[] strengths; LinkedList<Integer> piles; boolean placed; //read input tokenizer = new StringTokenizer(input.nextLine()); numBoxes = Integer.parseInt(tokenizer.nextToken()); tokenizer = new StringTokenizer(input.nextLine()); strengths = new int[numBoxes]; for(i=0;i<numBoxes;i++){ strengths[i] = Integer.parseInt(tokenizer.nextToken()); } Arrays.sort(strengths); // stack boxes piles = new LinkedList<Integer>(); for(i=0;i<numBoxes;i++){ placed = false; for(k = 0;k<piles.size();k++){ if(piles.get(k)<=strengths[i]){ piles.set(k, piles.get(k)+1); placed = true; break; } } if(!placed){ piles.add(1); } } writer.println(piles.size()); writer.close(); input.close(); } }//Class Close Bracket
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.StringTokenizer; public final class FoxBox { /** * @param args */ public static void main(String[] args) { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); try { int n = Integer.valueOf(bufferedReader.readLine()).intValue(); int[] sizes = new int[101]; StringTokenizer st = new StringTokenizer(bufferedReader.readLine(), " "); while(st.hasMoreElements()) { sizes[Integer.valueOf(st.nextToken()).intValue()]++; } int result = 0; int[] piles = new int[100]; for(int i=0; i<101; i++) { int j=0; while (sizes[i]>0) { if(i>=piles[j]) { sizes[i]--; piles[j]++; } else { j++; if (j > result) { result = j; } } } } System.out.println(result+1); } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
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() { long long n; cin >> n; vector<pair<long long, pair<long long, long long> > > v; for (long long i = 1; i <= n; i++) { long long x; cin >> x; v.push_back(make_pair(x, make_pair(1, x))); } sort(v.begin(), v.end()); while (1) { bool paisi = true; for (long long i = 0; i < v.size() - 1; i++) { if (v[i].first == -1) continue; int keep = v[i].second.first; for (long long j = i + 1; j < v.size(); j++) { int temp = v[j].second.second; if (keep <= temp) { v[j].second.first += v[i].second.first; v[j].second.second = min(v[i].second.second, (v[j].second.second) - keep); paisi = false; v[i].first = -1; break; } } } if (paisi) { int cnt = 0; for (long long i = 0; i < v.size(); i++) { if (v[i].first != -1) cnt++; } printf("%d\n", cnt); 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 n, t, ans; multiset<pair<int, int> > S; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> t; S.insert(make_pair(t, 1)); } while (!S.empty()) { bool F = false; pair<int, int> cur = *S.begin(); S.erase(S.begin()); for (multiset<pair<int, int> >::iterator it = S.begin(); it != S.end(); it++) { if (cur.second <= (*it).first) { pair<int, int> tmp = *it; S.erase(it); S.insert(make_pair(min(cur.first, tmp.first - cur.second), cur.second + tmp.second)); F = true; break; } } if (!F) 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
from collections import defaultdict def partition(seq, key=int): d = defaultdict(int) for x in seq: d[key(x)] += 1 return d def popBox(boxes, x): if boxes[x] == 1: del boxes[x] else: boxes[x] -= 1 nBoxes = int(raw_input()) boxes = partition([x for x in raw_input().split()]) stacks = 0 while (len(boxes) > 0): stacks += 1 boxesOnStack = 0 for i in range (0, 101): while(i in boxes and i>=boxesOnStack): boxesOnStack +=1 popBox(boxes, i) print 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.BufferedReader; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.Stack; import java.util.StringTokenizer; import static java.util.Arrays.sort; public class FoxAndBoxAccumulation implements Closeable { private InputReader in = new InputReader(System.in); private PrintWriter out = new PrintWriter(System.out); public void solve() { int n = in.ni(), cnt = 0, result = 0; int[] x = new int[n]; for (int i = 0; i < n; i++) { x[i] = in.ni(); } sort(x); boolean[] used = new boolean[n]; while (cnt < n) { result++; int k = 0; for (int i = 0; i < n; i++) { if (!used[i] && x[i] >= k) { used[i] = true; cnt++; k++; } } } out.println(result); } @Override public void close() throws IOException { in.close(); out.close(); } 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 ni() { return Integer.parseInt(next()); } public long nl() { return Long.parseLong(next()); } public void close() throws IOException { reader.close(); } } public static void main(String[] args) throws IOException { try (FoxAndBoxAccumulation instance = new FoxAndBoxAccumulation()) { instance.solve(); } } }
JAVA
389_C. Fox and Box Accumulation
Fox Ciel has n boxes in her room. They have the same size and weight, but they might have different strength. The i-th box can hold at most xi boxes on its top (we'll call xi the strength of the box). Since all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For example, imagine Ciel has three boxes: the first has strength 2, the second has strength 1 and the third has strength 1. She cannot put the second and the third box simultaneously directly on the top of the first one. But she can put the second box directly on the top of the first one, and then the third box directly on the top of the second one. We will call such a construction of boxes a pile. <image> Fox Ciel wants to construct piles from all the boxes. Each pile will contain some boxes from top to bottom, and there cannot be more than xi boxes on the top of i-th box. What is the minimal number of piles she needs to construct? Input The first line contains an integer n (1 ≀ n ≀ 100). The next line contains n integers x1, x2, ..., xn (0 ≀ xi ≀ 100). Output Output a single integer β€” the minimal possible number of piles. Examples Input 3 0 0 10 Output 2 Input 5 0 1 2 3 4 Output 1 Input 4 0 0 0 0 Output 4 Input 9 0 1 0 2 0 1 1 2 10 Output 3 Note In example 1, one optimal way is to build 2 piles: the first pile contains boxes 1 and 3 (from top to bottom), the second pile contains only box 2. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
n = int(raw_input()) X = map(int, raw_input().split()) L = [[] for i in xrange(100)] X.sort() for i in xrange(n): x = X[i] for j in xrange(n): if len(L[j]) <= x: L[j] += [x] break print sum([L[i] != [] for i in xrange(n)])
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
def Acc(arr): arr.sort() arr_aux=[0 for i in range(len(arr))] i=0 j=0 while i<len(arr): if arr[i]>=arr_aux[j]: arr_aux[j]=arr_aux[j]+1 i+=1 j=0 else: j+=1 #print(arr_aux) valor_actual=len(arr)-arr_aux.count(0) print(valor_actual) return n=int(input()) arr = list(map(int, input().strip().split())) Acc(arr)
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()) l = list(map(int,input().split(' '))) l.sort() nb = 0 for i in range(n): if l[i] == -1 : continue h = 1 nb += 1 for j in range(i+1,n): if l[j]>=h: h+=1 l[j] = -1 print(nb)
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
"""Template for Python Competitive Programmers prepared by Mayank Chaudhary aka chaudhary_19""" # to use the print and division function of Python3 from __future__ import division, print_function """value of mod""" MOD = 10 ** 9 + 7 """use resource""" # import resource # resource.setrlimit(resource.RLIMIT_STACK, [0x100000000, resource.RLIM_INFINITY]) """uncomment next 4 lines while doing recursion based question""" # import threading # threading.stack_size(2**27) import sys # sys.setrecursionlimit(10**6) """uncomment next 7 lines while doing nCr question""" # fact=[1] # for i in range(1,100001): # fact.append((fact[-1]*i)%mod) # ifact=[0]*100001 # ifact[100000]=pow(fact[100000],mod-2,mod) # for i in range(100000,0,-1): # ifact[i-1]=(i*ifact[i])%mod """uncomment modules according to your need""" # from bisect import bisect_left, bisect_right, insort # import itertools from math import floor, ceil, sqrt # import heapq # from random import randint as rn # from Queue import Queue as Q from collections import Counter, defaultdict ''' def modinv(n, p): return pow(n, p - 2, p) ''' ''' def ncr(n, r, p): # for using this uncomment the lines calculating fact and ifact t = ((fact[n]) * ((ifact[r] * ifact[n - r]) % p)) % p return t ''' def get_ints(): return map(int, sys.stdin.readline().strip().split()) def get_array(): return list(map(int, sys.stdin.readline().strip().split())) def input(): return sys.stdin.readline().strip() def GCD(x, y): while (y): x, y = y, x % y return x def lcm(x, y): return (x*y)//(GCD(x, y)) def get_xor(n): return [n,1,n+1,0][n%4] """*******************************************************""" def main(): n = int(input()) Arr = sorted(get_array()) ans = 1 for index, value in enumerate(Arr): if value < index//ans : ans += 1 print(ans) """ -------- Python 2 and 3 footer by Pajenegod and c1729 ---------""" py2 = round(0.5) if py2: from future_builtins import ascii, filter, hex, map, oct, zip range = xrange import os, sys from io import IOBase, BytesIO BUFSIZE = 8192 class FastIO(BytesIO): newlines = 0 def __init__(self, file): self._file = file self._fd = file.fileno() self.writable = "x" in file.mode or "w" in file.mode self.write = super(FastIO, self).write if self.writable else None def _fill(self): s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0]) return s def read(self): while self._fill(): pass return super(FastIO, self).read() def readline(self): while self.newlines == 0: s = self._fill(); self.newlines = s.count(b"\n") + (not s) self.newlines -= 1 return super(FastIO, self).readline() def flush(self): if self.writable: os.write(self._fd, self.getvalue()) self.truncate(0), self.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable if py2: self.write = self.buffer.write self.read = self.buffer.read self.readline = self.buffer.readline else: self.write = lambda s: self.buffer.write(s.encode('ascii')) self.read = lambda: self.buffer.read().decode('ascii') self.readline = lambda: self.buffer.readline().decode('ascii') sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip('\r\n') """ main function""" if __name__ == '__main__': main() # threading.Thread(target=main).start()
PYTHON
389_C. Fox and Box Accumulation
Fox Ciel has n boxes in her room. They have the same size and weight, but they might have different strength. The i-th box can hold at most xi boxes on its top (we'll call xi the strength of the box). Since all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For example, imagine Ciel has three boxes: the first has strength 2, the second has strength 1 and the third has strength 1. She cannot put the second and the third box simultaneously directly on the top of the first one. But she can put the second box directly on the top of the first one, and then the third box directly on the top of the second one. We will call such a construction of boxes a pile. <image> Fox Ciel wants to construct piles from all the boxes. Each pile will contain some boxes from top to bottom, and there cannot be more than xi boxes on the top of i-th box. What is the minimal number of piles she needs to construct? Input The first line contains an integer n (1 ≀ n ≀ 100). The next line contains n integers x1, x2, ..., xn (0 ≀ xi ≀ 100). Output Output a single integer β€” the minimal possible number of piles. Examples Input 3 0 0 10 Output 2 Input 5 0 1 2 3 4 Output 1 Input 4 0 0 0 0 Output 4 Input 9 0 1 0 2 0 1 1 2 10 Output 3 Note In example 1, one optimal way is to build 2 piles: the first pile contains boxes 1 and 3 (from top to bottom), the second pile contains only box 2. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; using namespace std::chrono; int main() { auto start = high_resolution_clock::now(); ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; ++i) cin >> v[i]; sort(v.begin(), v.end()); priority_queue<int, vector<int>, greater<int>> pq; int ans = 0; for (int i : v) { if (pq.empty()) { ++ans; pq.push(1); } else { int top = pq.top(); if (top <= i) { pq.pop(); pq.push(top + 1); } else { pq.push(1); ++ans; } } } cout << ans << endl; cerr << "Total execution time : " << duration_cast<milliseconds>(high_resolution_clock::now() - start) .count() << " ms" << 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> int main() { int sayac = 0, x, n, t1, t = 0, i, j, arr[105] = {0}; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &x); arr[x]++; } while (1) { t = 0; t1 = 1; for (i = 0; i <= 100; i++) if (arr[i]) t1 = 0; if (t1) break; for (i = 0; i <= 100; i++) { if (i >= t && arr[i]) { t++; arr[i]--; i = 0; } } sayac++; } printf("%d", sayac); 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] p=sorted(map(int,raw_input().split())) for x in p: if x<min(h): h+=[1] else: h[h.index(min(h))]+=1 print len(h)
PYTHON
389_C. Fox and Box Accumulation
Fox Ciel has n boxes in her room. They have the same size and weight, but they might have different strength. The i-th box can hold at most xi boxes on its top (we'll call xi the strength of the box). Since all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For example, imagine Ciel has three boxes: the first has strength 2, the second has strength 1 and the third has strength 1. She cannot put the second and the third box simultaneously directly on the top of the first one. But she can put the second box directly on the top of the first one, and then the third box directly on the top of the second one. We will call such a construction of boxes a pile. <image> Fox Ciel wants to construct piles from all the boxes. Each pile will contain some boxes from top to bottom, and there cannot be more than xi boxes on the top of i-th box. What is the minimal number of piles she needs to construct? Input The first line contains an integer n (1 ≀ n ≀ 100). The next line contains n integers x1, x2, ..., xn (0 ≀ xi ≀ 100). Output Output a single integer β€” the minimal possible number of piles. Examples Input 3 0 0 10 Output 2 Input 5 0 1 2 3 4 Output 1 Input 4 0 0 0 0 Output 4 Input 9 0 1 0 2 0 1 1 2 10 Output 3 Note In example 1, one optimal way is to build 2 piles: the first pile contains boxes 1 and 3 (from top to bottom), the second pile contains only box 2. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.PriorityQueue; import java.util.Random; import java.util.StringTokenizer; public class A { public static void main(String[] args) throws IOException { FastScanner sc = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int freq[] = new int[101]; int n = sc.nextInt(); for(int i=0; i<n; i++) { int a = sc.nextInt(); freq[a]++; } ArrayList<Pair> lis = new ArrayList<A.Pair>(); ff:for(int i=0; i<101; i++) { int val = freq[i]; if(val == 0)continue; while(val > 0) { int si = lis.size(); for(int j=0; j<si; j++) { Pair e = lis.get(j); int curr = e.x; while(i>=curr+1) { lis.set(j, new Pair(curr+1, i)); curr = curr+1; val--; if(val == 0)continue ff; } } lis.add(new Pair(0, i)); val--; if(val == 0)break; } } System.out.println(lis.size()); } static void ruffleSort(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } static class Pair implements Comparable<Pair> { int x, y; Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { return Integer.compare(this.x, o.x); } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readArrayLong(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static int ceil(int x, int y) { return (x % y == 0 ? x / y : (x / y + 1)); } static long ceil(long x, long y) { return (x % y == 0 ? x / y : (x / y + 1)); } static int max(int x, int y) { return Math.max(x, y); } static int min(int x, int y) { return Math.min(x, y); } static int abs(int x) { return Math.abs(x); } static long abs(long x) { return Math.abs(x); } static int log2(int N) { int result = (int) (Math.log(N) / Math.log(2)); return result; } static long max(long x, long y) { return Math.max(x, y); } static long min(long x, long y) { return Math.min(x, y); } static int min(int a[]) { int x = 1_000_000_00_9; for (int i = 0; i < a.length; i++) x = min(x, a[i]); return x; } static int max(int a[]) { int x = -1_000_000_00_9; for (int i = 0; i < a.length; i++) x = max(x, a[i]); return x; } static long min(long a[]) { long x = (long) 3e18; for (int i = 0; i < a.length; i++) x = min(x, a[i]); return x; } static long max(long a[]) { long x = -(long) 3e18; for (int i = 0; i < a.length; i++) x = max(x, a[i]); return x; } static int power(int x, int y) { int res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y >>= 1; x = (x * x); } return res; } static long power(long x, long y) { long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y >>= 1; x = (x * x); } return res; } static void intsort(int[] a) { List<Integer> temp = new ArrayList<Integer>(); for (int i = 0; i < a.length; i++) temp.add(a[i]); Collections.sort(temp); for (int i = 0; i < a.length; i++) a[i] = temp.get(i); } static void longsort(long[] a) { List<Long> temp = new ArrayList<Long>(); for (int i = 0; i < a.length; i++) temp.add(a[i]); Collections.sort(temp); for (int i = 0; i < a.length; i++) a[i] = temp.get(i); } static void reverseintsort(int[] a) { List<Integer> temp = new ArrayList<Integer>(); for (int i = 0; i < a.length; i++) temp.add(a[i]); Collections.sort(temp); Collections.reverseOrder(); for (int i = 0; i < a.length; i++) a[i] = temp.get(i); } static void reverselongsort(long[] a) { List<Long> temp = new ArrayList<Long>(); for (int i = 0; i < a.length; i++) temp.add(a[i]); Collections.sort(temp); Collections.reverseOrder(); for (int i = 0; i < a.length; i++) a[i] = temp.get(i); } static class longpair implements Comparable<longpair> { long x, y; longpair(long x, long y) { this.x = x; this.y = y; } public int compareTo(longpair p) { return Long.compare(this.x, p.x); } } public static int gcd(int a, int b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.intValue(); } public static long gcd(long a, long b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.longValue(); } }
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; std::cin >> n; int arr[n]; for (int i = 0; i < n; i++) { std::cin >> arr[i]; } sort(arr, arr + n); vector<int> vis(n, 0); int ans = 0, total = n; while (total) { int cnt = 0; for (int i = 0; i < n; i++) { if (!vis[i] && arr[i] >= cnt) { cnt++; total--; vis[i] = 1; } } ans++; } std::cout << ans << std::endl; return 0; }
CPP
389_C. Fox and Box Accumulation
Fox Ciel has n boxes in her room. They have the same size and weight, but they might have different strength. The i-th box can hold at most xi boxes on its top (we'll call xi the strength of the box). Since all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For example, imagine Ciel has three boxes: the first has strength 2, the second has strength 1 and the third has strength 1. She cannot put the second and the third box simultaneously directly on the top of the first one. But she can put the second box directly on the top of the first one, and then the third box directly on the top of the second one. We will call such a construction of boxes a pile. <image> Fox Ciel wants to construct piles from all the boxes. Each pile will contain some boxes from top to bottom, and there cannot be more than xi boxes on the top of i-th box. What is the minimal number of piles she needs to construct? Input The first line contains an integer n (1 ≀ n ≀ 100). The next line contains n integers x1, x2, ..., xn (0 ≀ xi ≀ 100). Output Output a single integer β€” the minimal possible number of piles. Examples Input 3 0 0 10 Output 2 Input 5 0 1 2 3 4 Output 1 Input 4 0 0 0 0 Output 4 Input 9 0 1 0 2 0 1 1 2 10 Output 3 Note In example 1, one optimal way is to build 2 piles: the first pile contains boxes 1 and 3 (from top to bottom), the second pile contains only box 2. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#from collections import deque n = map(int,input().split()) x = sorted(list(map(int,input().split()))) piles = 1 current = x.pop(0) current_size = 1 while x: for i,e in enumerate(x): if e >= current_size: current = e x.pop(i) current_size += 1 break else: current = x[0] x.pop(0) current_size = 1 piles += 1 print (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
import java.util.*; public class FoxAndBox { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = Integer.parseInt(s.nextLine()); List<Integer> boxes = new ArrayList<>(n); for (int i = 0; i < n; i++) { boxes.add(s.nextInt()); } Collections.sort(boxes); int count = 0; int neededWeigth = 0; while(!boxes.isEmpty()){ boxes.remove(0); count++; neededWeigth = 1; Iterator<Integer> boxesIterator = boxes.listIterator(); while(boxesIterator.hasNext()){ if(boxesIterator.next() >= neededWeigth){ neededWeigth++; boxesIterator.remove(); } } } System.out.print(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
#include <bits/stdc++.h> using namespace std; int arr[1000], ans = 0; int cnt[1000]; int main() { int n; scanf("%d", &n); priority_queue<int> pq; for (int i = 0; i < n; ++i) { int x; scanf("%d", &x); pq.push(-x); } for (int i = 0; i < 1000; ++i) { arr[i] = 0; cnt[i] = 0; } while (!pq.empty()) { int u = -pq.top(); pq.pop(); int p = -1, smallest = 100000; for (int i = 0; i < ans; ++i) if (arr[i] < smallest && cnt[i] <= u) { smallest = cnt[i]; p = i; } if (p == -1) { ++cnt[ans]; arr[ans++] = u; } else { ++cnt[p]; arr[p] = u; } } 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
import java.util.Arrays; import java.util.Scanner; public class Main { public static boolean check(int[] arr, int no){ int[] piles = new int[no]; for(int i = 0; i < arr.length; i++){ boolean isPossible = false; for(int j = 0; j < no; j++){ if(piles[j] <= arr[i]){ piles[j]++; isPossible = true; break; } } if(!isPossible){ return false; } } return true; } public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(), piles = 0; int[] arr = new int[n]; for(int i = 0; i < n; i++){ arr[i] = s.nextInt(); } Arrays.sort(arr); int start = 1, end = n, ans = n; while(start <= end){ int mid = start + ((end - start) >> 1); if(check(arr, mid)){ ans = mid; end = mid - 1; }else{ start = mid + 1; } } System.out.println(ans); } }
JAVA
389_C. Fox and Box Accumulation
Fox Ciel has n boxes in her room. They have the same size and weight, but they might have different strength. The i-th box can hold at most xi boxes on its top (we'll call xi the strength of the box). Since all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For example, imagine Ciel has three boxes: the first has strength 2, the second has strength 1 and the third has strength 1. She cannot put the second and the third box simultaneously directly on the top of the first one. But she can put the second box directly on the top of the first one, and then the third box directly on the top of the second one. We will call such a construction of boxes a pile. <image> Fox Ciel wants to construct piles from all the boxes. Each pile will contain some boxes from top to bottom, and there cannot be more than xi boxes on the top of i-th box. What is the minimal number of piles she needs to construct? Input The first line contains an integer n (1 ≀ n ≀ 100). The next line contains n integers x1, x2, ..., xn (0 ≀ xi ≀ 100). Output Output a single integer β€” the minimal possible number of piles. Examples Input 3 0 0 10 Output 2 Input 5 0 1 2 3 4 Output 1 Input 4 0 0 0 0 Output 4 Input 9 0 1 0 2 0 1 1 2 10 Output 3 Note In example 1, one optimal way is to build 2 piles: the first pile contains boxes 1 and 3 (from top to bottom), the second pile contains only box 2. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; int n, pile, cnt, ans = 0; multiset<int> s; multiset<int>::iterator it; int main(void) { ios ::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; ++i) { cin >> pile; s.insert(pile); } while (s.size()) { s.erase(s.begin()); cnt = 1; while ((it = s.lower_bound(cnt)) != s.end()) { ++cnt; s.erase(it); } ++ans; } cout << ans << "\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
#include <bits/stdc++.h> using namespace std; const long long int inf = 1e18; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL), cout.tie(NULL); vector<long long int> arr[100]; long long int n, t, i, p, j, cnt = 0; cin >> n; long long int v[n]; for (i = 0; i < n; i++) cin >> v[i]; sort(v, v + n); for (i = 0; i < n; i++) { p = v[i]; for (j = 0; j < 100; j++) { if (arr[j].size() <= p) { arr[j].push_back(p); cnt = max(cnt, j); break; } } } cout << cnt + 1 << 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> const int inf = (int)1e9; const int mod = 1e9 + 7; using namespace std; int a[111], ans = 0; bool cmp(int x, int y) { return (x > y); } bool u[111]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } sort(a + 1, a + 1 + n); for (int i = 1; i <= n; i++) { if (u[i]) continue; u[i] = 1; ans++; int cnt = 1; for (int j = i + 1; j <= n; j++) { if (!u[j] && a[j] >= cnt) { u[j] = 1; cnt++; } } } cout << ans; return 0; }
CPP
389_C. Fox and Box Accumulation
Fox Ciel has n boxes in her room. They have the same size and weight, but they might have different strength. The i-th box can hold at most xi boxes on its top (we'll call xi the strength of the box). Since all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For example, imagine Ciel has three boxes: the first has strength 2, the second has strength 1 and the third has strength 1. She cannot put the second and the third box simultaneously directly on the top of the first one. But she can put the second box directly on the top of the first one, and then the third box directly on the top of the second one. We will call such a construction of boxes a pile. <image> Fox Ciel wants to construct piles from all the boxes. Each pile will contain some boxes from top to bottom, and there cannot be more than xi boxes on the top of i-th box. What is the minimal number of piles she needs to construct? Input The first line contains an integer n (1 ≀ n ≀ 100). The next line contains n integers x1, x2, ..., xn (0 ≀ xi ≀ 100). Output Output a single integer β€” the minimal possible number of piles. Examples Input 3 0 0 10 Output 2 Input 5 0 1 2 3 4 Output 1 Input 4 0 0 0 0 Output 4 Input 9 0 1 0 2 0 1 1 2 10 Output 3 Note In example 1, one optimal way is to build 2 piles: the first pile contains boxes 1 and 3 (from top to bottom), the second pile contains only box 2. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Scanner; import java.util.Stack; public class Main { @SuppressWarnings("unchecked") public static void main(String args[]){ try{ //FileInputStream fs = new FileInputStream(new File("input.txt")); //BufferedReader br = new BufferedReader(new InputStreamReader(fs)); Scanner br = new Scanner(System.in); int n = br.nextInt(); //int k = br.nextInt(); //int d = br.nextInt(); int a[] = new int [n]; //int max = 0; for(int i = 0;i<n; i++){ a[i] = br.nextInt(); } Arrays.sort(a); ArrayList<Integer> stack= new ArrayList(); stack.add(1); for(int i = 1; i<n; i++){ //boolean added = false; int j =0; for(; j<stack.size(); j++){ if(stack.get(j)<=a[i]){ int temp = stack.remove(j); stack.add(j,temp+1);break; } } if(j==stack.size()){ stack.add(1); } //System.out.println(stack); } System.out.println(stack.size()); }catch(Exception e){e.printStackTrace();} } }
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.InputStreamReader; import java.io.IOException; import java.io.*; import java.io.OutputStream; import java.io.PrintWriter; import java.lang.reflect.Array; import java.math.BigInteger; import java.util.*; import java.util.Arrays; import java.util.Collections; import java.io.InputStream; public class Main { static int minPrice = Integer.MAX_VALUE; static public Node[][] levels; static public int[][] states; static double[][] distances; static boolean[][] field; public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); // Scanner sc = new Scanner(System.in); //int n = sc.nextInt(); //StringBuilder strb = new StringBuilder(sc.nextLine()); //System.out.println(strb.toString()); int n = in.nextInt(); ArrayList<Integer> boxes = new ArrayList<Integer>(); int proch; for (int i = 0; i< n; i++) { proch = in.nextInt(); boxes.add(proch); } ArrayList<ArrayList<Integer>> stacks = new ArrayList<ArrayList<Integer>>(); stacks.add(new ArrayList<Integer>()); for (int i = 0; i< 101;i++) { for (int j = 0; j < boxes.size();j++) { if (boxes.get(j) == i) { for (int k = 0; k < stacks.size();k++) { if (!stacks.get(k).isEmpty()) if (stacks.get(k).size()<=boxes.get(j)) { stacks.get(k).add(boxes.get(j)); // System.out.println(i + " added"); boxes.remove(j); j--; break; } if (k==stacks.size()-1) { stacks.add(new ArrayList<Integer>()); stacks.get(stacks.size()-1).add(boxes.get(j)); boxes.remove(j); j--; // System.out.println(i + " added too"); break; } } } } } System.out.println(stacks.size()-1); } } class Point{ public int x; public int y; public boolean colored = false; public Point(int x, int y) { this.x = x; this.y = y; } } class Node{ public boolean colored = false; public ArrayList<Integer> pathto = new ArrayList<Integer>(); public ArrayList<Boolean> switcher = new ArrayList<Boolean>();// pathto and up! public int switchCounter = -2; } class InputReader { private static BufferedReader reader; private static StringTokenizer tok; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tok = null; } private static String readToken() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(reader.readLine()); } return tok.nextToken(); } public int nextInt () throws IOException { return Integer.parseInt(readToken()); } public String nextLine() throws IOException { return reader.readLine(); } }
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; scanf("%d", &n); vector<int> v; vector<int> st; vector<vector<int> > vv; for (int i = 0; i < n; i++) { int x; scanf("%d", &x); v.push_back(x); } sort(v.rbegin(), v.rend()); for (int i = 0; i < n; i++) { int x = v[i]; vector<int> temp(n + 1, 0); vv.push_back(temp); st.push_back(x); } int t1 = 0; int t2 = n - 1; int tt = 0; while (1) { if (t2 == t1 || t2 == tt) break; if (st[t1] - 1 >= vv[t1][0] && v[t2] >= vv[t1][0]) { int index = vv[t1][0]; if (index == -1) index = 0; vv[t1][index + 1] = v[t2]; vv[t1][0] = index + 1; t2--; if (tt < t1) tt = t1; t1 = 0; } else t1++; } printf("%d\n", t2 + 1); 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; vector<int> inp; vector<vector<int> > dip; void input() { cin >> n; inp.resize(n); for (int i = 0; i < n; ++i) cin >> inp[i]; } void owp() { dip.resize(1); dip[0].push_back(inp[0]); for (int i = 1; i < n; ++i) { int q = dip.size(); bool b = true; for (int j = 0; j < q; ++j) if (inp[i] >= dip[j].size()) { dip[j].insert(dip[j].begin(), inp[i]); b = false; break; } if (b) { dip.resize(dip.size() + 1); dip[dip.size() - 1].push_back(inp[i]); } } } int main() { input(); sort(inp.begin(), inp.end()); owp(); cout << dip.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; int a[101]; bool vis[101] = {0}; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); int ans = 0, tot = n; sort(a + 1, a + n + 1); while (tot) { int cnt = 0; for (int i = 1; i <= n; i++) { if (!vis[i] && cnt <= a[i]) { vis[i] = 1; cnt++; tot--; } } 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; int main() { int n; cin >> n; vector<int> v; int num; for (int i = 0; i < n; i++) { cin >> num; v.push_back(num); } sort(v.begin(), v.end()); int cnt = 0; int all = 0; vector<bool> taken(v.size(), false); while (true) { int cur = 0; for (int i = 0; i < v.size(); i++) { if (taken[i] == true) continue; if (cur <= v[i]) { cur++; all++; taken[i] = true; } } cnt++; if (all == n) break; } cout << cnt << endl; return 0; }
CPP
389_C. Fox and Box Accumulation
Fox Ciel has n boxes in her room. They have the same size and weight, but they might have different strength. The i-th box can hold at most xi boxes on its top (we'll call xi the strength of the box). Since all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For example, imagine Ciel has three boxes: the first has strength 2, the second has strength 1 and the third has strength 1. She cannot put the second and the third box simultaneously directly on the top of the first one. But she can put the second box directly on the top of the first one, and then the third box directly on the top of the second one. We will call such a construction of boxes a pile. <image> Fox Ciel wants to construct piles from all the boxes. Each pile will contain some boxes from top to bottom, and there cannot be more than xi boxes on the top of i-th box. What is the minimal number of piles she needs to construct? Input The first line contains an integer n (1 ≀ n ≀ 100). The next line contains n integers x1, x2, ..., xn (0 ≀ xi ≀ 100). Output Output a single integer β€” the minimal possible number of piles. Examples Input 3 0 0 10 Output 2 Input 5 0 1 2 3 4 Output 1 Input 4 0 0 0 0 Output 4 Input 9 0 1 0 2 0 1 1 2 10 Output 3 Note In example 1, one optimal way is to build 2 piles: the first pile contains boxes 1 and 3 (from top to bottom), the second pile contains only box 2. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; int main() { int n, i, ans = 0, ps, j, f; cin >> n; int a[n], b[n], pile[n + 1]; for (i = 0; i < n; i++) { cin >> a[i]; } for (i = 0; i <= n; i++) { pile[i] = 0; } sort(a, a + n); ans = 1; pile[ans]++; for (i = 1; i < n; i++) { f = 0; for (j = 1; j <= ans; j++) { if (a[i] >= pile[j]) { pile[j]++; f = 1; break; } } if (f == 0) { ans++; pile[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 java.io.*; import java.math.*; import java.util.*; import java.util.stream.*; @SuppressWarnings("unchecked") public class P388A { public void run() throws Exception { TreeMap<Integer, Integer> xc = new TreeMap(); for (int n = nextInt(); n > 0; n--) { int x = nextInt(); xc.put(x, xc.getOrDefault(x, 0) + 1); } int s = 0; for (; xc.size() > 0; s++) { for (int k = 0; xc.ceilingKey(k) != null; k++) { int c = xc.get(xc.ceilingKey(k)) - 1; if (c > 0) { xc.put(xc.ceilingKey(k), c); } else { xc.remove(xc.ceilingKey(k)); } } } println(s); } public static void main(String... args) throws Exception { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedOutputStream(System.out)); new P388A().run(); br.close(); pw.close(); System.err.println("\n[Time : " + (System.currentTimeMillis() - startTime) + " ms]"); } static long startTime = System.currentTimeMillis(); static BufferedReader br; static PrintWriter pw; StringTokenizer stok; String nextToken() throws IOException { while (stok == null || !stok.hasMoreTokens()) { String s = br.readLine(); if (s == null) { return null; } stok = new StringTokenizer(s); } return stok.nextToken(); } void print(byte b) { print("" + b); } void print(int i) { print("" + i); } void print(long l) { print("" + l); } void print(double d) { print("" + d); } void print(char c) { print("" + c); } void print(Object o) { if (o instanceof int[]) { print(Arrays.toString((int [])o)); } else if (o instanceof long[]) { print(Arrays.toString((long [])o)); } else if (o instanceof char[]) { print(Arrays.toString((char [])o)); } else if (o instanceof byte[]) { print(Arrays.toString((byte [])o)); } else if (o instanceof short[]) { print(Arrays.toString((short [])o)); } else if (o instanceof boolean[]) { print(Arrays.toString((boolean [])o)); } else if (o instanceof float[]) { print(Arrays.toString((float [])o)); } else if (o instanceof double[]) { print(Arrays.toString((double [])o)); } else if (o instanceof Object[]) { print(Arrays.toString((Object [])o)); } else { print("" + o); } } void print(String s) { pw.print(s); } void println() { println(""); } void println(byte b) { println("" + b); } void println(int i) { println("" + i); } void println(long l) { println("" + l); } void println(double d) { println("" + d); } void println(char c) { println("" + c); } void println(Object o) { print(o); println(); } void println(String s) { pw.println(s); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } char nextChar() throws IOException { return (char) (br.read()); } String next() throws IOException { return nextToken(); } String nextLine() throws IOException { return br.readLine(); } int [] readInt(int size) throws IOException { int [] array = new int [size]; for (int i = 0; i < size; i++) { array[i] = nextInt(); } return array; } long [] readLong(int size) throws IOException { long [] array = new long [size]; for (int i = 0; i < size; i++) { array[i] = nextLong(); } return array; } double [] readDouble(int size) throws IOException { double [] array = new double [size]; for (int i = 0; i < size; i++) { array[i] = nextDouble(); } return array; } String [] readLines(int size) throws IOException { String [] array = new String [size]; for (int i = 0; i < size; i++) { array[i] = nextLine(); } return array; } int gcd(int a, int b) { return ((b > 0) ? gcd(b, a % b) : a); } long gcd(long a, long b) { return ((b > 0) ? gcd(b, a % b) : a); } }
JAVA
389_C. Fox and Box Accumulation
Fox Ciel has n boxes in her room. They have the same size and weight, but they might have different strength. The i-th box can hold at most xi boxes on its top (we'll call xi the strength of the box). Since all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For example, imagine Ciel has three boxes: the first has strength 2, the second has strength 1 and the third has strength 1. She cannot put the second and the third box simultaneously directly on the top of the first one. But she can put the second box directly on the top of the first one, and then the third box directly on the top of the second one. We will call such a construction of boxes a pile. <image> Fox Ciel wants to construct piles from all the boxes. Each pile will contain some boxes from top to bottom, and there cannot be more than xi boxes on the top of i-th box. What is the minimal number of piles she needs to construct? Input The first line contains an integer n (1 ≀ n ≀ 100). The next line contains n integers x1, x2, ..., xn (0 ≀ xi ≀ 100). Output Output a single integer β€” the minimal possible number of piles. Examples Input 3 0 0 10 Output 2 Input 5 0 1 2 3 4 Output 1 Input 4 0 0 0 0 Output 4 Input 9 0 1 0 2 0 1 1 2 10 Output 3 Note In example 1, one optimal way is to build 2 piles: the first pile contains boxes 1 and 3 (from top to bottom), the second pile contains only box 2. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
#include <bits/stdc++.h> using namespace std; long long int power(long long int a, long long int b) { long long int ans; while (b > 0) { if (b % 2 == 1) { ans *= a; } b /= 2; a *= a; } return ans; } struct comparator { bool operator()(long long int i, long long int j) { return i > j; } }; bool sortpair(pair<long long int, long long int> &a, pair<long long int, long long int> &b) { return (b.first - b.second) > (a.first - a.second); } long long int gcd(long long int a, long long int b) { if (a == 0) { return b; } return gcd(b % a, a); } long long int min(long long int a, long long int b) { return a > b ? b : a; } long long int min(long long int a, long long int b, long long int c) { if (a <= b && a <= c) { return a; } else if (b <= a && b <= c) { return b; } else { return c; } } long long int max(long long int a, long long int b) { return a > b ? a : b; } long long int lcm(long long int a, long long int b) { return (a * b / gcd(a, b)); } bool check(long long int x) { while (x % 2 == 0) { x /= 2; } while (x % 3 == 0) { x /= 3; } if (x != 1) { return false; } return true; } long long int ans(int num, int n) { if (n == 1) { return num; } else if (n == 2) { return num + 1; } else if (n == 3) { return num + 2; } else if (n % 3 == 0) { return ans(num + 2, (n - 1) / 2); } else if (n % 2 == 0) { return ans(num + 1, n / 2); } } vector<long long int> getprimes() { vector<long long int> pl(101, 1); for (int i = 2; i <= 100; i++) { if (pl[i] == 1) { for (int j = 2 * i; j <= 100; j += i) { pl[j] = 0; } } } return pl; } long long int ans(int n) { if (n == 0) { return 1; } else if (n == 1 || n == 2) { return 0; } else if (n < 0) { return 0; } else { return (ans(n - 7) | ans(n - 3)); } } long long int primefactorise(int n) { if (n == 1) { return 1; } long long int ans = n; while (n % 2 == 0) { n = n / 2; if (n != 1) { ans += n; } } for (int i = 3; i <= sqrt(n); i = i + 2) { while (n % i == 0) { n = n / i; if (n != 1) { ans += n; } } } ans += 1; return ans; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); long long int n; cin >> n; int a[n]; int u[n]; for (int i = 0; i < n; i++) { u[n] = 0; cin >> a[i]; } sort(a, a + n); int ans = 1; for (int i = 0; i < n; i++) { int temp = i / ans; if (a[i] < temp) { ans++; } } 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 main() { int n; cin >> n; int a[n]; int c[n]; for (int i = 0; i < n; i++) { cin >> a[i]; c[i] = 0; } sort(a, a + n); int w = 0; ; for (int i = 0; i < n; i++) { if (c[i] == 0) { int x = c[i]; int e = 1; w++; c[i] = w; for (int j = i + 1; j < n; j++) { if (a[j] >= e && c[j] == 0) { x = a[j]; c[j] = w; e++; } } } } cout << w << endl; return 0; }
CPP
389_C. Fox and Box Accumulation
Fox Ciel has n boxes in her room. They have the same size and weight, but they might have different strength. The i-th box can hold at most xi boxes on its top (we'll call xi the strength of the box). Since all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For example, imagine Ciel has three boxes: the first has strength 2, the second has strength 1 and the third has strength 1. She cannot put the second and the third box simultaneously directly on the top of the first one. But she can put the second box directly on the top of the first one, and then the third box directly on the top of the second one. We will call such a construction of boxes a pile. <image> Fox Ciel wants to construct piles from all the boxes. Each pile will contain some boxes from top to bottom, and there cannot be more than xi boxes on the top of i-th box. What is the minimal number of piles she needs to construct? Input The first line contains an integer n (1 ≀ n ≀ 100). The next line contains n integers x1, x2, ..., xn (0 ≀ xi ≀ 100). Output Output a single integer β€” the minimal possible number of piles. Examples Input 3 0 0 10 Output 2 Input 5 0 1 2 3 4 Output 1 Input 4 0 0 0 0 Output 4 Input 9 0 1 0 2 0 1 1 2 10 Output 3 Note In example 1, one optimal way is to build 2 piles: the first pile contains boxes 1 and 3 (from top to bottom), the second pile contains only box 2. <image> In example 2, we can build only 1 pile that contains boxes 1, 2, 3, 4, 5 (from top to bottom). <image>
2
9
n = int(input()) a = list(map(int, input().split())) a.sort() uni = {} for elem in a: if elem not in uni: uni[elem]=1 else: uni[elem]+=1 piles = [] while len(a)>0: box = a.pop(0) if len(piles)==0: piles.append([box]) else: for i in range(len(piles)): temp = piles[i] if box>=len(temp): temp.append(box) piles[i] = temp break else: piles.append([box]) #print(piles) 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; vector<int> V; bool solve(int n); int main() { ios_base::sync_with_stdio(false); int i, st, dr, mid; cin >> N; V.resize(N); for (i = 0; i < N; ++i) { cin >> V[i]; } sort(V.begin(), V.end()); st = 1; dr = N; while (st < dr) { mid = (st + dr) / 2; if (solve(mid)) { dr = mid; } else { st = mid + 1; } } cout << dr << '\n'; return 0; } bool solve(int n) { int i, j, nr; vector<int> piles; piles.resize(n, 101); j = 0; for (i = N - 1; i >= 0; --i) { nr = 0; while (piles[j] == 0) { ++j; ++nr; if (j == n) j = 0; if (nr == n + 1) break; } if (nr == n + 1) break; piles[j] = min(piles[j] - 1, V[i]); ++j; if (j == n) j = 0; } if (i == -1) { return true; } return false; }
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; int a[101], b[101]; int ans[101]; int main() { int i, j; while (cin >> n) { memset(b, 0, sizeof(b)); for (i = 0; i < n; i++) cin >> a[i], b[a[i]]++; memset(ans, 0, sizeof(ans)); int now = 0, Max = 0; for (i = 0; i <= 100; i++) { if (b[i] == 0) continue; for (j = 0; j < now; j++) { int tmp = i + 1 - ans[j]; if (b[i] && b[i] >= tmp) { b[i] -= tmp; ans[j] += tmp; } else if (b[i]) { ans[j] += b[i]; b[i] = 0; } if (b[i] == 0) break; } if (b[i]) { while (1) { int tmp = i + 1 - 0; if (b[i] && b[i] >= tmp) { b[i] -= tmp; ans[now++] += tmp; } else if (b[i]) { ans[now++] = b[i]; b[i] = 0; } if (b[i] == 0) break; if (now >= 100) break; } } } cout << now << endl; } }
CPP