// while (clock()<=69*CLOCKS_PER_SEC) // #pragma comment(linker, "/stack:200000000") // #pragma GCC optimize("O3") // #pragma GCC optimize("Ofast") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // #pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define pb push_back #define SZ(x) ((int)(x).size()) #define ALL(x) x.begin(),x.end() #define all(x) x.begin(),x.end() #define fi first #define se second #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x)))); using namespace std; using namespace __gnu_pbds; template<typename T> using ordered_set = tree< T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego) //X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k typedef long long LL; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; typedef vector<PII> VPII; typedef vector<PLL> VPLL; typedef vector<LL> VLL; typedef vector<int> VI; typedef vector<string> VS; typedef vector<char> VC; typedef long double LD; typedef pair<LD,LD> PLD; typedef vector<LD> VLD; typedef vector<PLD> VPLD; template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++; cerr<<" = "<<h<<", "; _dbg(sdbg+1, a...); } #ifdef LOCAL #define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define dbg(...) #define cerr if(0)cout #endif const int maxn = (100)+7; const int maxk = 21; const int inf = (1e9)+7; const LL LLinf = ((LL)1e18)+7LL; const LD eps = 1e-9; const LL mod = 1e9+7; // ***************************** CODE ***************************** // int tab[maxn]; int res_wyn[maxn]; LL res_ile[maxn]; struct node { int wsk[maxk]; int mini[maxk]; LL ile[maxk]; int zmian; int koniec; node() { for(int i = 0;i < maxk;i++) { wsk[i] = -1; mini[i] = inf; ile[i] = 0; } koniec = -1; } }; const int pot = 1<<20; node drze[2 * pot + 7]; bool ok[maxn]; VPII wek2, wek; int num = 0; int jak[maxn]; int licz(int l, int p, int mask) { int res = 0; for(int i = l;i <= p;i++) if(mask & (1<<(i - l))) for(int j = i + 1;j <= p;j++) if(mask & (1<<(j - l))) if(tab[i] > tab[j]) res++; return res; } int value2(int mask) { int res = 0; for(int i = 0;i < SZ(wek2);i++) if(mask & (1<<i)) for(int j = i + 1;j < SZ(wek2);j++) if(mask & (1<<j)) if((wek2[i].fi > wek2[j].fi and wek2[i].se < wek2[j].se) or (wek2[i].fi < wek2[j].fi and wek2[i].se > wek2[j].se)) res++; return res; } int value(int mask) { int res = 0; for(int i = 0;i < SZ(wek);i++) if(mask & (1<<i)) for(int j = i + 1;j < SZ(wek);j++) if(mask & (1<<j)) if((wek[i].fi > wek[j].fi and wek[i].se < wek[j].se) or (wek[i].fi < wek[j].fi and wek[i].se > wek[j].se)) res++; return res; } void upd(int & cur, int val, LL & ile, int & bity) { // val += drze[cur].zmian; if(drze[cur].mini[bity] >= val) { if(drze[cur].mini[bity] == val) drze[cur].ile[bity] += ile; else { drze[cur].mini[bity] = val; drze[cur].ile[bity] = ile; } } } void update(int x) { if(drze[x].koniec == -1) return; for(int i = 0;i < maxk;i++) { drze[x].mini[i] = drze[drze[x].wsk[0]].mini[i] + drze[drze[x].wsk[0]].zmian; drze[x].ile[i] = drze[drze[x].wsk[0]].ile[i]; } for(int i = 1;i <= drze[x].koniec;i++) { int skad = drze[x].wsk[i]; for(int j = 0;j < maxk;j++) upd(x, drze[skad].mini[j] + drze[skad].zmian, drze[skad].ile[j], j); } } int rob(int l, int p) { if(l > p) return num++; int wsk = l + 1; while(wsk <= p and ok[wsk] == 0) wsk++; int cur = num++; drze[cur].koniec = wsk - l; for(int i = l;i <= wsk;i++) { drze[cur].wsk[i - l] = rob(wsk, p); } return cur; } void add(int val, int bity, int mask) { int cur = 0; while(drze[cur].wsk[0] != -1) { int ile = 0; while(ile < maxk and drze[cur].wsk[ile] != -1) ile++; int tmp = 0; ile--; while(ile--) { tmp += (mask & 1); mask >>= 1; } cur = drze[cur].wsk[tmp]; } LL jed = 1; upd(cur, val, jed, bity); } int cur_mask = 0; void upd_wyn(int res, LL ile, int bity) { if(res_wyn[bity] > res) { res_wyn[bity] = res; res_ile[bity] = ile; } else { if(res_wyn[bity] == res) res_ile[bity] += ile; } } void dodaj(int cur, int g, int val) { if(g <= 0) return; int gdzie; for(int i = 0;i <= drze[cur].koniec;i++) { gdzie = drze[cur].wsk[i]; drze[gdzie].zmian += i * val; dodaj(gdzie, g - drze[cur].koniec, val); } update(cur); } void rek(int x) { if(x == SZ(wek)) { int tmp = value(cur_mask); LL ile = __builtin_popcount(cur_mask); for(int i = 0;i < maxk;i++) upd_wyn(tmp + drze[0].mini[i] + drze[0].zmian, drze[0].ile[i], ile + i); return; } rek(x + 1); dodaj(0, jak[x], 1); cur_mask ^= (1<<x); rek(x + 1); cur_mask ^= (1<<x); dodaj(0, jak[x], -1); } int main() { _upgrade int n; cin>>n; for(int i = 0;i <= n;i++) res_wyn[i] = inf; int pol = min(n - 1, n / 2 + 1); for(int i = 0;i < n;i++) { cin>>tab[i]; tab[i]--; if(pol <= i) wek2.pb({tab[i], i}); else wek.pb({tab[i], i}); } if(n == 1) { cout<<"0 1"; return 0; } sort(all(wek)); reverse(all(wek)); sort(all(wek2)); for(int i = 0;i < SZ(wek);i++) { jak[i] = 0; while(jak[i] < SZ(wek2) and wek2[jak[i]].fi < wek[i].fi) jak[i]++; } for(int i = 0;i < pol;i++) { int wsk = 0; while(wsk < SZ(wek2) and wek2[wsk].fi < tab[i]) wsk++; ok[wsk] = 1; } ok[0] = 1; rob(0, SZ(wek2) - 1); for(int i = 0;i < (1<<(n - pol));i++) { int val = value2(i); int ile = __builtin_popcount(i); add(val, ile, i); } for(int i = num - 1;i >= 0;i--) update(i); rek(0); for(int i = 1;i <= n;i++) cout<<res_wyn[i]<<" "<<res_ile[i]<<"\n"; 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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | // while (clock()<=69*CLOCKS_PER_SEC) // #pragma comment(linker, "/stack:200000000") // #pragma GCC optimize("O3") // #pragma GCC optimize("Ofast") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // #pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define pb push_back #define SZ(x) ((int)(x).size()) #define ALL(x) x.begin(),x.end() #define all(x) x.begin(),x.end() #define fi first #define se second #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x)))); using namespace std; using namespace __gnu_pbds; template<typename T> using ordered_set = tree< T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego) //X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k typedef long long LL; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; typedef vector<PII> VPII; typedef vector<PLL> VPLL; typedef vector<LL> VLL; typedef vector<int> VI; typedef vector<string> VS; typedef vector<char> VC; typedef long double LD; typedef pair<LD,LD> PLD; typedef vector<LD> VLD; typedef vector<PLD> VPLD; template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++; cerr<<" = "<<h<<", "; _dbg(sdbg+1, a...); } #ifdef LOCAL #define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define dbg(...) #define cerr if(0)cout #endif const int maxn = (100)+7; const int maxk = 21; const int inf = (1e9)+7; const LL LLinf = ((LL)1e18)+7LL; const LD eps = 1e-9; const LL mod = 1e9+7; // ***************************** CODE ***************************** // int tab[maxn]; int res_wyn[maxn]; LL res_ile[maxn]; struct node { int wsk[maxk]; int mini[maxk]; LL ile[maxk]; int zmian; int koniec; node() { for(int i = 0;i < maxk;i++) { wsk[i] = -1; mini[i] = inf; ile[i] = 0; } koniec = -1; } }; const int pot = 1<<20; node drze[2 * pot + 7]; bool ok[maxn]; VPII wek2, wek; int num = 0; int jak[maxn]; int licz(int l, int p, int mask) { int res = 0; for(int i = l;i <= p;i++) if(mask & (1<<(i - l))) for(int j = i + 1;j <= p;j++) if(mask & (1<<(j - l))) if(tab[i] > tab[j]) res++; return res; } int value2(int mask) { int res = 0; for(int i = 0;i < SZ(wek2);i++) if(mask & (1<<i)) for(int j = i + 1;j < SZ(wek2);j++) if(mask & (1<<j)) if((wek2[i].fi > wek2[j].fi and wek2[i].se < wek2[j].se) or (wek2[i].fi < wek2[j].fi and wek2[i].se > wek2[j].se)) res++; return res; } int value(int mask) { int res = 0; for(int i = 0;i < SZ(wek);i++) if(mask & (1<<i)) for(int j = i + 1;j < SZ(wek);j++) if(mask & (1<<j)) if((wek[i].fi > wek[j].fi and wek[i].se < wek[j].se) or (wek[i].fi < wek[j].fi and wek[i].se > wek[j].se)) res++; return res; } void upd(int & cur, int val, LL & ile, int & bity) { // val += drze[cur].zmian; if(drze[cur].mini[bity] >= val) { if(drze[cur].mini[bity] == val) drze[cur].ile[bity] += ile; else { drze[cur].mini[bity] = val; drze[cur].ile[bity] = ile; } } } void update(int x) { if(drze[x].koniec == -1) return; for(int i = 0;i < maxk;i++) { drze[x].mini[i] = drze[drze[x].wsk[0]].mini[i] + drze[drze[x].wsk[0]].zmian; drze[x].ile[i] = drze[drze[x].wsk[0]].ile[i]; } for(int i = 1;i <= drze[x].koniec;i++) { int skad = drze[x].wsk[i]; for(int j = 0;j < maxk;j++) upd(x, drze[skad].mini[j] + drze[skad].zmian, drze[skad].ile[j], j); } } int rob(int l, int p) { if(l > p) return num++; int wsk = l + 1; while(wsk <= p and ok[wsk] == 0) wsk++; int cur = num++; drze[cur].koniec = wsk - l; for(int i = l;i <= wsk;i++) { drze[cur].wsk[i - l] = rob(wsk, p); } return cur; } void add(int val, int bity, int mask) { int cur = 0; while(drze[cur].wsk[0] != -1) { int ile = 0; while(ile < maxk and drze[cur].wsk[ile] != -1) ile++; int tmp = 0; ile--; while(ile--) { tmp += (mask & 1); mask >>= 1; } cur = drze[cur].wsk[tmp]; } LL jed = 1; upd(cur, val, jed, bity); } int cur_mask = 0; void upd_wyn(int res, LL ile, int bity) { if(res_wyn[bity] > res) { res_wyn[bity] = res; res_ile[bity] = ile; } else { if(res_wyn[bity] == res) res_ile[bity] += ile; } } void dodaj(int cur, int g, int val) { if(g <= 0) return; int gdzie; for(int i = 0;i <= drze[cur].koniec;i++) { gdzie = drze[cur].wsk[i]; drze[gdzie].zmian += i * val; dodaj(gdzie, g - drze[cur].koniec, val); } update(cur); } void rek(int x) { if(x == SZ(wek)) { int tmp = value(cur_mask); LL ile = __builtin_popcount(cur_mask); for(int i = 0;i < maxk;i++) upd_wyn(tmp + drze[0].mini[i] + drze[0].zmian, drze[0].ile[i], ile + i); return; } rek(x + 1); dodaj(0, jak[x], 1); cur_mask ^= (1<<x); rek(x + 1); cur_mask ^= (1<<x); dodaj(0, jak[x], -1); } int main() { _upgrade int n; cin>>n; for(int i = 0;i <= n;i++) res_wyn[i] = inf; int pol = min(n - 1, n / 2 + 1); for(int i = 0;i < n;i++) { cin>>tab[i]; tab[i]--; if(pol <= i) wek2.pb({tab[i], i}); else wek.pb({tab[i], i}); } if(n == 1) { cout<<"0 1"; return 0; } sort(all(wek)); reverse(all(wek)); sort(all(wek2)); for(int i = 0;i < SZ(wek);i++) { jak[i] = 0; while(jak[i] < SZ(wek2) and wek2[jak[i]].fi < wek[i].fi) jak[i]++; } for(int i = 0;i < pol;i++) { int wsk = 0; while(wsk < SZ(wek2) and wek2[wsk].fi < tab[i]) wsk++; ok[wsk] = 1; } ok[0] = 1; rob(0, SZ(wek2) - 1); for(int i = 0;i < (1<<(n - pol));i++) { int val = value2(i); int ile = __builtin_popcount(i); add(val, ile, i); } for(int i = num - 1;i >= 0;i--) update(i); rek(0); for(int i = 1;i <= n;i++) cout<<res_wyn[i]<<" "<<res_ile[i]<<"\n"; return 0; } |