#include<bits/stdc++.h> using namespace std; ostream& operator<<(ostream &out, string str) { for(char c : str) out << c; return out; } template<class L, class R> ostream& operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.first << ", " << p.second << ")"; } template<class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { out << "{"; for(auto it = a.begin(); it != a.end(); it = next(it)) out << (it != a.begin() ? ", " : "") << *it; return out << "}"; } void dump() { cerr << "\n"; } template<class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #ifdef DEBUG # define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else # define debug(...) false #endif #define REP(i, n) for(int i = 0; i < n; i++) #define FOR(i, a, b) for(int i = a; i <= b; i++) #define ST first #define ND second template<class T> int size(T && a) { return a.size(); } using LL = long long; using PII = pair<int, int>; struct Newton { vector<vector<LL>> dp; Newton(int n) { dp.resize(n + 1, vector<LL>(n + 1)); REP(i, n + 1) { dp[i][0] = 1; FOR(j, 1, i) dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; } } LL get(int n, int k) { return dp[n][k]; } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> a(n); REP(i, n) cin >> a[i]; int t; vector<int> s; REP(q, 2) { vector<PII> dp(n); REP(i, n) dp[i] = {1, -1}; PII p = {0, -1}; REP(i, n) { FOR(j, i + 1, n - 1) if((a[i] > a[j]) ^ q) dp[j] = max(dp[j], {dp[i].ST + 1, i}); p = max(p, {dp[i].ST, i}); } debug(dp); vector<int> v; while(p.ND != -1) { debug(p.ND); v.emplace_back(p.ND); p.ND = dp[p.ND].ND; } if(size(v) > size(s)) s = v, t = q; } debug(s, t); int k = size(s); vector<bool> spec(n); for(int x : s) spec[x] = true; vector<int> r; REP(i, n) if(!spec[i]) r.emplace_back(i); int m = size(r); Newton newton(k); debug(newton.dp); vector<int> mn(n + 1, 1e9); vector<LL> cnt(n + 1, 0); REP(mask, 1 << m) { int edges = 0; vector<int> deg(k); REP(i, m) if(mask & (1 << i)) { FOR(j, i + 1, m - 1) if(mask & (1 << j)) edges += (a[r[i]] > a[r[j]]); REP(j, k) deg[j] += (a[r[i]] > a[s[j]]) ^ (r[i] > s[j]); } int c = __builtin_popcount(mask); vector<int> b(m + 1), w(m + 1); debug(deg); REP(j, k) b[deg[j]]++; int pos = 0; if(mn[c] > edges) { mn[c] = edges; cnt[c] = 0; } if(mn[c] == edges) cnt[c]++; REP(j, k) { while(b[pos] == w[pos]) pos++; edges += pos + (t == 0 ? j : 0); w[pos]++, c++; debug(pos, c, b[pos], w[pos], edges, t, j, newton.get(b[pos], w[pos])); if(mn[c] > edges) { mn[c] = edges; cnt[c] = 0; } if(mn[c] == edges) cnt[c] += newton.get(b[pos], w[pos]); } } FOR(i, 1, n) cout << mn[i] << " " << cnt[i] << "\n"; }
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 137 138 139 140 141 142 143 144 145 146 147 | #include<bits/stdc++.h> using namespace std; ostream& operator<<(ostream &out, string str) { for(char c : str) out << c; return out; } template<class L, class R> ostream& operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.first << ", " << p.second << ")"; } template<class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { out << "{"; for(auto it = a.begin(); it != a.end(); it = next(it)) out << (it != a.begin() ? ", " : "") << *it; return out << "}"; } void dump() { cerr << "\n"; } template<class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #ifdef DEBUG # define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else # define debug(...) false #endif #define REP(i, n) for(int i = 0; i < n; i++) #define FOR(i, a, b) for(int i = a; i <= b; i++) #define ST first #define ND second template<class T> int size(T && a) { return a.size(); } using LL = long long; using PII = pair<int, int>; struct Newton { vector<vector<LL>> dp; Newton(int n) { dp.resize(n + 1, vector<LL>(n + 1)); REP(i, n + 1) { dp[i][0] = 1; FOR(j, 1, i) dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; } } LL get(int n, int k) { return dp[n][k]; } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> a(n); REP(i, n) cin >> a[i]; int t; vector<int> s; REP(q, 2) { vector<PII> dp(n); REP(i, n) dp[i] = {1, -1}; PII p = {0, -1}; REP(i, n) { FOR(j, i + 1, n - 1) if((a[i] > a[j]) ^ q) dp[j] = max(dp[j], {dp[i].ST + 1, i}); p = max(p, {dp[i].ST, i}); } debug(dp); vector<int> v; while(p.ND != -1) { debug(p.ND); v.emplace_back(p.ND); p.ND = dp[p.ND].ND; } if(size(v) > size(s)) s = v, t = q; } debug(s, t); int k = size(s); vector<bool> spec(n); for(int x : s) spec[x] = true; vector<int> r; REP(i, n) if(!spec[i]) r.emplace_back(i); int m = size(r); Newton newton(k); debug(newton.dp); vector<int> mn(n + 1, 1e9); vector<LL> cnt(n + 1, 0); REP(mask, 1 << m) { int edges = 0; vector<int> deg(k); REP(i, m) if(mask & (1 << i)) { FOR(j, i + 1, m - 1) if(mask & (1 << j)) edges += (a[r[i]] > a[r[j]]); REP(j, k) deg[j] += (a[r[i]] > a[s[j]]) ^ (r[i] > s[j]); } int c = __builtin_popcount(mask); vector<int> b(m + 1), w(m + 1); debug(deg); REP(j, k) b[deg[j]]++; int pos = 0; if(mn[c] > edges) { mn[c] = edges; cnt[c] = 0; } if(mn[c] == edges) cnt[c]++; REP(j, k) { while(b[pos] == w[pos]) pos++; edges += pos + (t == 0 ? j : 0); w[pos]++, c++; debug(pos, c, b[pos], w[pos], edges, t, j, newton.get(b[pos], w[pos])); if(mn[c] > edges) { mn[c] = edges; cnt[c] = 0; } if(mn[c] == edges) cnt[c] += newton.get(b[pos], w[pos]); } } FOR(i, 1, n) cout << mn[i] << " " << cnt[i] << "\n"; } |