// Artur Kraska, II UWr #include <bits/stdc++.h> #define forr(i, n) for(int i=0; i<n; i++) #define FOREACH(iter, coll) for(auto iter = coll.begin(); iter != coll.end(); ++iter) #define FOREACHR(iter, coll) for(auto iter = coll.rbegin(); iter != coll.rend(); ++iter) #define lbound(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;}) #define testy() int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests) #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define CONTAIN(el, coll) (coll.find(el) != coll.end()) #define FOR(i, a, b) for(int i=a; i<=b; i++) #define FORD(i, a, b) for(int i=a; i>=b; i--) #define MP make_pair #define PB push_back #define deb(X) X; //#define M 1000000007 #define INF 1000000007 using namespace std; int n, a, tab[1007], S, d[1007]; long long wynik[107], minn[107], m[107], w[107], mnoz[107]; // dodac longlongi przy 1<<nr !!! long long M; string maskuj(long long x) { string s = ""; forr(i, n) s += ((x>>i)&1)+'0'; return s; } void wkladaj(int nr, int ile) { nr += S; while(nr > 0) { d[nr] += ile; nr >>= 1; } } int zlicz(int nr) { int res = 0; nr += S; while(nr > 0) { if((nr & 1) == 0) res += d[nr+1]; nr >>= 1; } return res; } void licz(long long nr, int ile, int kl, long long mask, int mn) { if(nr == n) { if(kl < minn[ile]) { minn[ile] = kl; wynik[ile] = mn; } else if(kl == minn[ile]) wynik[ile] += mn; return ; } bool spr = 1; mnoz[nr] = 1; forr(i, nr) { if((mask & (1LL<<i)) == 0) { long long mm = mask & (-1 - (1LL<<i) - (1LL<<nr)); //cout << "nr = " << nr << ", i = " << i << ", mm = " << maskuj(mm) << endl; //cout << "maska[n] = " << maskuj(m[nr]) << ", maska[i] = " << maskuj(m[i]) << endl; if(((mm & m[i])) == ((mm & m[i] & m[nr]))) { if((mm & m[nr]&(M>>(n-nr-1))) != (mm & m[i]&(M>>(n-nr-1)))) { //cout << "dla maski " << maskuj(mm) << ", i = " << i << ", nr = " << nr << " mamy nazdbior" << endl; spr = 0; } /*if((mm & m[nr]) == (mm & m[i])) { cout << "mnoznik++" << endl; mnoz[i]++; spr = 0; }*/ } } } licz(nr+1, ile, kl, mask - (1LL<<nr), mn); if(spr) { wkladaj(tab[nr], 1); licz(nr+1, ile+1, kl + zlicz(tab[nr]), mask, mn*mnoz[nr]); wkladaj(tab[nr], -1); } } int main() { scanf("%d", &n); M = (1<<n)-1; forr(i, n) scanf("%d", &tab[i]); S = 1; while(S <= n) S *= 2; forr(i, n) { forr(j, n) { if(i < j && tab[i] > tab[j]) { m[i] |= (1LL<<j); m[j] |= (1LL<<i); } } } FOR(i, 0, n) minn[i] = INF; licz(0, 0, 0, (1<<n)-1, 1); FOR(i, 1, n) printf("%lld %lld\n", minn[i], wynik[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 | // Artur Kraska, II UWr #include <bits/stdc++.h> #define forr(i, n) for(int i=0; i<n; i++) #define FOREACH(iter, coll) for(auto iter = coll.begin(); iter != coll.end(); ++iter) #define FOREACHR(iter, coll) for(auto iter = coll.rbegin(); iter != coll.rend(); ++iter) #define lbound(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;}) #define testy() int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests) #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define CONTAIN(el, coll) (coll.find(el) != coll.end()) #define FOR(i, a, b) for(int i=a; i<=b; i++) #define FORD(i, a, b) for(int i=a; i>=b; i--) #define MP make_pair #define PB push_back #define deb(X) X; //#define M 1000000007 #define INF 1000000007 using namespace std; int n, a, tab[1007], S, d[1007]; long long wynik[107], minn[107], m[107], w[107], mnoz[107]; // dodac longlongi przy 1<<nr !!! long long M; string maskuj(long long x) { string s = ""; forr(i, n) s += ((x>>i)&1)+'0'; return s; } void wkladaj(int nr, int ile) { nr += S; while(nr > 0) { d[nr] += ile; nr >>= 1; } } int zlicz(int nr) { int res = 0; nr += S; while(nr > 0) { if((nr & 1) == 0) res += d[nr+1]; nr >>= 1; } return res; } void licz(long long nr, int ile, int kl, long long mask, int mn) { if(nr == n) { if(kl < minn[ile]) { minn[ile] = kl; wynik[ile] = mn; } else if(kl == minn[ile]) wynik[ile] += mn; return ; } bool spr = 1; mnoz[nr] = 1; forr(i, nr) { if((mask & (1LL<<i)) == 0) { long long mm = mask & (-1 - (1LL<<i) - (1LL<<nr)); //cout << "nr = " << nr << ", i = " << i << ", mm = " << maskuj(mm) << endl; //cout << "maska[n] = " << maskuj(m[nr]) << ", maska[i] = " << maskuj(m[i]) << endl; if(((mm & m[i])) == ((mm & m[i] & m[nr]))) { if((mm & m[nr]&(M>>(n-nr-1))) != (mm & m[i]&(M>>(n-nr-1)))) { //cout << "dla maski " << maskuj(mm) << ", i = " << i << ", nr = " << nr << " mamy nazdbior" << endl; spr = 0; } /*if((mm & m[nr]) == (mm & m[i])) { cout << "mnoznik++" << endl; mnoz[i]++; spr = 0; }*/ } } } licz(nr+1, ile, kl, mask - (1LL<<nr), mn); if(spr) { wkladaj(tab[nr], 1); licz(nr+1, ile+1, kl + zlicz(tab[nr]), mask, mn*mnoz[nr]); wkladaj(tab[nr], -1); } } int main() { scanf("%d", &n); M = (1<<n)-1; forr(i, n) scanf("%d", &tab[i]); S = 1; while(S <= n) S *= 2; forr(i, n) { forr(j, n) { if(i < j && tab[i] > tab[j]) { m[i] |= (1LL<<j); m[j] |= (1LL<<i); } } } FOR(i, 0, n) minn[i] = INF; licz(0, 0, 0, (1<<n)-1, 1); FOR(i, 1, n) printf("%lld %lld\n", minn[i], wynik[i]); return 0; } |