#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") #include <bits/stdc++.h> using namespace std; typedef unsigned int uint; typedef long long int LL; #define st first #define nd second #define PII pair <int, int> const int N = 41; const int M = 1 << 20; int n; int in[N]; uint maskSize[M]; LL ans[N]; int best[N]; int tAns[2][N]; int tBest[2][N]; int halfX, halfY; int _n[4]; vector <int> ids[4]; uint tmp[N]; uint val[2][M]; uint dp[4][4][M]; void getPoint(){ halfX = (n + 1) / 2, halfY = (n + 1) / 2; } void finalUpdate(int r, int val, LL add){ if(best[r] > val) best[r] = val, ans[r] = 0; if(best[r] == val) ans[r] += add; } inline void prvUpdate(int t, int r, int val){ if(tBest[t][r] > val) tBest[t][r] = val, tAns[t][r] = 1; else if(tBest[t][r] == val) tAns[t][r]++; } void preCalc(int t){ for(int i = 0; i < _n[t]; ++i){ tmp[i] = 0; for(int j = 0; j < _n[t]; ++j){ int a = ids[t][i], b = ids[t][j]; if((a < b && in[a] > in[b]) || (a > b && in[a] < in[b])) tmp[i] |= 1 << j; } } for(int i = 1; i < (1 << _n[t]); ++i){ int last = __builtin_ctz(i); dp[t][t][i] = dp[t][t][i ^ (1 << last)] + maskSize[i & tmp[last]]; } } void calc(int ta, int tb){ for(int i = 0; i < _n[tb]; ++i){ tmp[i] = 0; for(int j = 0; j < _n[ta]; ++j){ int a = ids[tb][i], b = ids[ta][j]; if((a < b && in[a] > in[b]) || (a > b && in[a] < in[b])) tmp[i] |= 1 << j; } } assert(_n[ta] + _n[tb] <= 20); for(int i = 0; i < (1 << _n[ta]); ++i){ dp[ta][tb][i << _n[tb]] = 0; for(int j = 0; j < (1 << _n[tb]); ++j){ int last = __builtin_ctz(j); dp[ta][tb][(i << _n[tb]) + j] = dp[ta][tb][(i << _n[tb]) ^ j ^ (1 << last)] + maskSize[i & tmp[last]]; } } } void clear(){ for(int i = 0; i <= _n[0]; ++i) tBest[0][i] = N * N, tAns[0][i] = 0; for(int i = 0; i <= _n[3]; ++i) tBest[1][i] = N * N, tAns[1][i] = 0; } int main(){ scanf("%d", &n); for(int i = 1; i <= n; ++i) scanf("%d", &in[i]); for(int i = 0; i < M; ++i) maskSize[i] = __builtin_popcount(i); for(int i = 1; i <= n; ++i) best[i] = N * N; getPoint(); for(int i = 1; i <= n; ++i){ int id = 2 * (i > halfX) + (in[i] > halfY); ids[id].push_back(i); _n[id]++; } bool swapped = false; if(_n[0] + _n[3] < _n[1] + _n[2]){ swapped = true; swap(ids[0], ids[1]); swap(ids[2], ids[3]); swap(_n[0], _n[1]); swap(_n[2], _n[3]); } for(int t = 0; t < 4; ++t) preCalc(t); calc(0, 1); calc(2, 0); calc(1, 3); calc(2, 3); for(uint ma = 0; ma < (1 << _n[1]); ++ma){ for(uint mc = 0; mc < (1 << _n[0]); ++mc) val[0][mc] = dp[0][0][mc] + dp[0][1][(mc << _n[1]) + ma]; for(uint mc = 0; mc < (1 << _n[3]); ++mc) val[1][mc] = dp[3][3][mc] + dp[1][3][(ma << _n[3]) + mc]; for(uint mb = 0; mb < (1 << _n[2]); ++mb){ int sa = maskSize[ma], sb = maskSize[mb]; clear(); uint add = mb << _n[0]; for(uint mc = 0; mc < (1 << _n[0]); ++mc) prvUpdate(0, maskSize[mc], val[0][mc] + dp[2][0][add | mc]); add = mb << _n[3]; for(uint mc = 0; mc < (1 << _n[3]); ++mc) prvUpdate(1, maskSize[mc], val[1][mc] + dp[2][3][add | mc]); for(int ta = 0; ta <= _n[0]; ++ta) for(int tb = 0; tb <= _n[3]; ++tb){ int addCost = tBest[0][ta] + tBest[1][tb] + dp[1][1][ma] + dp[2][2][mb]; if(swapped) addCost += ta * tb; else addCost += sa * sb; finalUpdate(sa + sb + ta + tb, addCost, 1LL * tAns[0][ta] * tAns[1][tb]); } } } for(int i = 1; i <= n; ++i) printf("%d %lld\n", best[i], ans[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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") #include <bits/stdc++.h> using namespace std; typedef unsigned int uint; typedef long long int LL; #define st first #define nd second #define PII pair <int, int> const int N = 41; const int M = 1 << 20; int n; int in[N]; uint maskSize[M]; LL ans[N]; int best[N]; int tAns[2][N]; int tBest[2][N]; int halfX, halfY; int _n[4]; vector <int> ids[4]; uint tmp[N]; uint val[2][M]; uint dp[4][4][M]; void getPoint(){ halfX = (n + 1) / 2, halfY = (n + 1) / 2; } void finalUpdate(int r, int val, LL add){ if(best[r] > val) best[r] = val, ans[r] = 0; if(best[r] == val) ans[r] += add; } inline void prvUpdate(int t, int r, int val){ if(tBest[t][r] > val) tBest[t][r] = val, tAns[t][r] = 1; else if(tBest[t][r] == val) tAns[t][r]++; } void preCalc(int t){ for(int i = 0; i < _n[t]; ++i){ tmp[i] = 0; for(int j = 0; j < _n[t]; ++j){ int a = ids[t][i], b = ids[t][j]; if((a < b && in[a] > in[b]) || (a > b && in[a] < in[b])) tmp[i] |= 1 << j; } } for(int i = 1; i < (1 << _n[t]); ++i){ int last = __builtin_ctz(i); dp[t][t][i] = dp[t][t][i ^ (1 << last)] + maskSize[i & tmp[last]]; } } void calc(int ta, int tb){ for(int i = 0; i < _n[tb]; ++i){ tmp[i] = 0; for(int j = 0; j < _n[ta]; ++j){ int a = ids[tb][i], b = ids[ta][j]; if((a < b && in[a] > in[b]) || (a > b && in[a] < in[b])) tmp[i] |= 1 << j; } } assert(_n[ta] + _n[tb] <= 20); for(int i = 0; i < (1 << _n[ta]); ++i){ dp[ta][tb][i << _n[tb]] = 0; for(int j = 0; j < (1 << _n[tb]); ++j){ int last = __builtin_ctz(j); dp[ta][tb][(i << _n[tb]) + j] = dp[ta][tb][(i << _n[tb]) ^ j ^ (1 << last)] + maskSize[i & tmp[last]]; } } } void clear(){ for(int i = 0; i <= _n[0]; ++i) tBest[0][i] = N * N, tAns[0][i] = 0; for(int i = 0; i <= _n[3]; ++i) tBest[1][i] = N * N, tAns[1][i] = 0; } int main(){ scanf("%d", &n); for(int i = 1; i <= n; ++i) scanf("%d", &in[i]); for(int i = 0; i < M; ++i) maskSize[i] = __builtin_popcount(i); for(int i = 1; i <= n; ++i) best[i] = N * N; getPoint(); for(int i = 1; i <= n; ++i){ int id = 2 * (i > halfX) + (in[i] > halfY); ids[id].push_back(i); _n[id]++; } bool swapped = false; if(_n[0] + _n[3] < _n[1] + _n[2]){ swapped = true; swap(ids[0], ids[1]); swap(ids[2], ids[3]); swap(_n[0], _n[1]); swap(_n[2], _n[3]); } for(int t = 0; t < 4; ++t) preCalc(t); calc(0, 1); calc(2, 0); calc(1, 3); calc(2, 3); for(uint ma = 0; ma < (1 << _n[1]); ++ma){ for(uint mc = 0; mc < (1 << _n[0]); ++mc) val[0][mc] = dp[0][0][mc] + dp[0][1][(mc << _n[1]) + ma]; for(uint mc = 0; mc < (1 << _n[3]); ++mc) val[1][mc] = dp[3][3][mc] + dp[1][3][(ma << _n[3]) + mc]; for(uint mb = 0; mb < (1 << _n[2]); ++mb){ int sa = maskSize[ma], sb = maskSize[mb]; clear(); uint add = mb << _n[0]; for(uint mc = 0; mc < (1 << _n[0]); ++mc) prvUpdate(0, maskSize[mc], val[0][mc] + dp[2][0][add | mc]); add = mb << _n[3]; for(uint mc = 0; mc < (1 << _n[3]); ++mc) prvUpdate(1, maskSize[mc], val[1][mc] + dp[2][3][add | mc]); for(int ta = 0; ta <= _n[0]; ++ta) for(int tb = 0; tb <= _n[3]; ++tb){ int addCost = tBest[0][ta] + tBest[1][tb] + dp[1][1][ma] + dp[2][2][mb]; if(swapped) addCost += ta * tb; else addCost += sa * sb; finalUpdate(sa + sb + ta + tb, addCost, 1LL * tAns[0][ta] * tAns[1][tb]); } } } for(int i = 1; i <= n; ++i) printf("%d %lld\n", best[i], ans[i]); return 0; } |