#include <bits/stdc++.h> #include <stdint.h> using namespace std; typedef vector< vector<char> > Graph; bool at_most2(Graph const &G, int k, int m, int p, int z, vector<int> &pick) { int n = G.size(); if(pick.size() == k) return true; if(pick.size() + n - p < k) return false; for(int i = p; i < n; i++) { int tz = 0; for(int j = 0; j < pick.size(); j++) { tz += G[i][pick[j]]; } if(z + tz > m) continue; pick.push_back(i); if(at_most2(G, k, m, i+1, z+tz, pick)) { return true; } pick.pop_back(); } return false; } bool at_most(Graph const &G, int k, int m) { vector<int> pick; return at_most2(G, k, m, 0, 0, pick); } int64_t how_many2(Graph const &G, int k, int m, int p, int z, vector<int> &pick) { int n = G.size(); if(pick.size() == k and z == m) return 1; int todo = k - pick.size(); if(n - p < todo) return 0; int64_t ans = 0; for(int i = p; i < n; i++) { int tz = 0; for(int j = 0; j < pick.size(); j++) { tz += G[i][pick[j]]; } if(z + tz > m) continue; pick.push_back(i); ans += how_many2(G, k, m, i+1, z+tz, pick); pick.pop_back(); } return ans; } int64_t how_many(Graph const &G, int k, int m) { vector<int> pick; return how_many2(G, k, m, 0, 0, pick); } void solve(Graph const &G, int k) { int me = k * (k + 1) / 2; int minp = -1; { if(at_most(G, k, 0)) { minp = 0; } else { int lo = 0; int hi = me; while(lo != hi) { int mi = (lo + hi) / 2; if(at_most(G, k, mi)) { hi = mi; } else { lo = mi + 1; } } minp = lo; } } cout << minp << ' ' << how_many(G, k, minp) << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; Graph G(n, vector<char>(n)); vector<int> A(n); for(int i = 0; i < n; i++) { cin >> A[i]; for(int j = 0; j < i; j++) { if(A[i] < A[j]) { G[i][j] = 1; G[j][i] = 1; } } } cout << "0 " << n << '\n'; for(int i = 2; i <= n; i++) { solve(G, i); } return 0; }
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | #include <bits/stdc++.h> #include <stdint.h> using namespace std; typedef vector< vector<char> > Graph; bool at_most2(Graph const &G, int k, int m, int p, int z, vector<int> &pick) { int n = G.size(); if(pick.size() == k) return true; if(pick.size() + n - p < k) return false; for(int i = p; i < n; i++) { int tz = 0; for(int j = 0; j < pick.size(); j++) { tz += G[i][pick[j]]; } if(z + tz > m) continue; pick.push_back(i); if(at_most2(G, k, m, i+1, z+tz, pick)) { return true; } pick.pop_back(); } return false; } bool at_most(Graph const &G, int k, int m) { vector<int> pick; return at_most2(G, k, m, 0, 0, pick); } int64_t how_many2(Graph const &G, int k, int m, int p, int z, vector<int> &pick) { int n = G.size(); if(pick.size() == k and z == m) return 1; int todo = k - pick.size(); if(n - p < todo) return 0; int64_t ans = 0; for(int i = p; i < n; i++) { int tz = 0; for(int j = 0; j < pick.size(); j++) { tz += G[i][pick[j]]; } if(z + tz > m) continue; pick.push_back(i); ans += how_many2(G, k, m, i+1, z+tz, pick); pick.pop_back(); } return ans; } int64_t how_many(Graph const &G, int k, int m) { vector<int> pick; return how_many2(G, k, m, 0, 0, pick); } void solve(Graph const &G, int k) { int me = k * (k + 1) / 2; int minp = -1; { if(at_most(G, k, 0)) { minp = 0; } else { int lo = 0; int hi = me; while(lo != hi) { int mi = (lo + hi) / 2; if(at_most(G, k, mi)) { hi = mi; } else { lo = mi + 1; } } minp = lo; } } cout << minp << ' ' << how_many(G, k, minp) << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; Graph G(n, vector<char>(n)); vector<int> A(n); for(int i = 0; i < n; i++) { cin >> A[i]; for(int j = 0; j < i; j++) { if(A[i] < A[j]) { G[i][j] = 1; G[j][i] = 1; } } } cout << "0 " << n << '\n'; for(int i = 2; i <= n; i++) { solve(G, i); } return 0; } |