1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #include <algorithm> #include <cstring> #include <iostream> #include <queue> using namespace std; const int maxn = 2e5 + 50;
int n, m, bus[70];
int valid(int a, int d) { for (int i = a; i < 60; i += d) { if (!bus[i]) return 0; } return 1; }
struct node { int cnt_, a_, d_; node(int cnt = 0, int a = 0, int d = 0) : cnt_(cnt), a_(a), d_(d) {} bool operator<(node &x) { return cnt_ > x.cnt_ || cnt_ == x.cnt_ && a_ < x.a_ || cnt_ == x.cnt_ && a_ == x.a_ && d_ < x.d_; } } s[maxn];
bool dfs(int depth, int sum, int start) { if (!depth) return sum == n; for (int i = start; i < m; ++i) { int a = s[i].a_, d = s[i].d_, cnt = s[i].cnt_; if (cnt * depth + sum < n) continue; if (!valid(a, d)) continue; for (int j = a; j < 60; j += d) --bus[j]; if (dfs(depth - 1, sum + cnt, i)) return 1; for (int j = a; j < 60; j += d) ++bus[j]; } return 0; }
void init() { cin >> n; for (int x, i = 0; i < n; ++i) cin >> x, ++bus[x]; for (int a = 0; a < 60; ++a) { for (int d = a + 1; a + d < 60; ++d) { if (valid(a, d)) s[m++] = node((59 - a) / d + 1, a, d); } } sort(s, s + m); }
int main() { init(); int dep = 0; while (!dfs(dep, 0, 0)) ++dep; cout << dep << endl; return 0; }
|