#include <bits/stdc++.h> using namespace std; #define PII pair<int, int> #define PLL pair<LL, LL> #define VI vector<int> #define VPII vector<PII> #define LL long long #define LD long double #define f first #define s second #define MP make_pair #define PB push_back #define pb pop_back #define ALL(c) (c).begin(), (c).end() #define SIZ(c) (int)(c).size() #define REP(i, n) for(int i = 0; i < (int)(n); ++i) #define FOR(i, b, e) for(int i = (b); i <= (int)(e); ++i) #define FORD(i, b, e) for(int i = (b); i >= (int)(e); --i) #define Sim template<class n Sim, class s> ostream & operator << (ostream &p, pair<n, s> x) {return p << "<" << x.f << ", " << x.s << ">";} Sim> auto operator << (ostream &p, n y) -> typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type {int o = 0; p << "{"; for(auto c: y) {if(o++) p << ", "; p << c;} return p << "}";} void dor() {cerr << endl;} Sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);} Sim, class s> void mini(n &p, s y) {if(p>y) p = y;} Sim, class s> void maxi(n &p, s y) {if(p<y) p = y;} #ifdef DEB #define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__) #else #define debug(...) #endif #define PLL pair<LL, LL> #define I(x) #x " = ", (x), " " #define A(a, i) #a "[" #i " = ", i, "] = ", a[i], " " const int M = 50, M2 = 1.5e7; int n; int T[M], X[M], x1, add, ktory; VI A[M], S[M]; PLL V[2][M2]; void ad(int i1, int i2, PLL x) { if (V[i1][i2].f > x.f) { V[i1][i2] = x; } else if (V[i1][i2].f == x.f) { V[i1][i2].s += x.s; } } void addd(int ind, int i, int x) { X[i] += x; if (i > ktory) { add += x; x1 += x * S[ind + 1][i]; } else { x1 += x * S[ind + 1][i + 1]; } } void increment(int ind) { FORD(i, SIZ(A[ind]) - 1, 0) { if (X[i] == A[ind][i] - 1) { addd(ind, i, -X[i]); } else { addd(ind, i, 1); break; } } } int32_t main() { scanf("%d", &n); FOR(i, 1, n) { scanf("%d", T + i); } VPII holes; FOR(i, 1, n) { holes.PB(MP(T[i], i)); } sort(ALL(holes)); int sum = 0; FOR(i, 0, n) { int tmp = 0; FOR(j, 0, n - 1) { if (holes[j].s <= i) { tmp++; } else { A[i].PB(tmp + 1); tmp = 0; } } A[i].PB(tmp + 1); S[i].resize(SIZ(A[i])); S[i].back() = A[i].back(); FORD(j, SIZ(A[i]) - 2, 0) { S[i][j] = S[i][j + 1] * A[i][j]; } S[i].PB(1); sum += S[i][0]; } V[0][0] = MP(0, 1); FOR(i, 1, n) { ktory = 0; debug(i, S[i][0]); FOR(j, i + 1, n) { if (T[j] < T[i]) { ktory++; } } FOR(j, 0, S[i][0]) { V[i % 2][j] = MP(1e9, 0); } FOR(j, 0, M - 1) { X[j] = 0; } x1 = 0; add = 0; ad(i % 2, x1 + S[i - 1][ktory + 2], MP(V[1 - i % 2][0].f + add, V[1 - i % 2][0].s)); ad(i % 2, x1, V[1 - i % 2][0]); FOR(j, 1, S[i - 1][0] - 1) { increment(i - 1); ad(i % 2, x1 + S[i - 1][ktory + 2], MP(V[1 - i % 2][j].f + add, V[1 - i % 2][j].s)); ad(i % 2, x1, V[1 - i % 2][j]); } } debug(sum); FOR(i, 1, n) { printf("%lld %lld\n", V[n % 2][i].f, V[n % 2][i].s); } }
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 | #include <bits/stdc++.h> using namespace std; #define PII pair<int, int> #define PLL pair<LL, LL> #define VI vector<int> #define VPII vector<PII> #define LL long long #define LD long double #define f first #define s second #define MP make_pair #define PB push_back #define pb pop_back #define ALL(c) (c).begin(), (c).end() #define SIZ(c) (int)(c).size() #define REP(i, n) for(int i = 0; i < (int)(n); ++i) #define FOR(i, b, e) for(int i = (b); i <= (int)(e); ++i) #define FORD(i, b, e) for(int i = (b); i >= (int)(e); --i) #define Sim template<class n Sim, class s> ostream & operator << (ostream &p, pair<n, s> x) {return p << "<" << x.f << ", " << x.s << ">";} Sim> auto operator << (ostream &p, n y) -> typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type {int o = 0; p << "{"; for(auto c: y) {if(o++) p << ", "; p << c;} return p << "}";} void dor() {cerr << endl;} Sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);} Sim, class s> void mini(n &p, s y) {if(p>y) p = y;} Sim, class s> void maxi(n &p, s y) {if(p<y) p = y;} #ifdef DEB #define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__) #else #define debug(...) #endif #define PLL pair<LL, LL> #define I(x) #x " = ", (x), " " #define A(a, i) #a "[" #i " = ", i, "] = ", a[i], " " const int M = 50, M2 = 1.5e7; int n; int T[M], X[M], x1, add, ktory; VI A[M], S[M]; PLL V[2][M2]; void ad(int i1, int i2, PLL x) { if (V[i1][i2].f > x.f) { V[i1][i2] = x; } else if (V[i1][i2].f == x.f) { V[i1][i2].s += x.s; } } void addd(int ind, int i, int x) { X[i] += x; if (i > ktory) { add += x; x1 += x * S[ind + 1][i]; } else { x1 += x * S[ind + 1][i + 1]; } } void increment(int ind) { FORD(i, SIZ(A[ind]) - 1, 0) { if (X[i] == A[ind][i] - 1) { addd(ind, i, -X[i]); } else { addd(ind, i, 1); break; } } } int32_t main() { scanf("%d", &n); FOR(i, 1, n) { scanf("%d", T + i); } VPII holes; FOR(i, 1, n) { holes.PB(MP(T[i], i)); } sort(ALL(holes)); int sum = 0; FOR(i, 0, n) { int tmp = 0; FOR(j, 0, n - 1) { if (holes[j].s <= i) { tmp++; } else { A[i].PB(tmp + 1); tmp = 0; } } A[i].PB(tmp + 1); S[i].resize(SIZ(A[i])); S[i].back() = A[i].back(); FORD(j, SIZ(A[i]) - 2, 0) { S[i][j] = S[i][j + 1] * A[i][j]; } S[i].PB(1); sum += S[i][0]; } V[0][0] = MP(0, 1); FOR(i, 1, n) { ktory = 0; debug(i, S[i][0]); FOR(j, i + 1, n) { if (T[j] < T[i]) { ktory++; } } FOR(j, 0, S[i][0]) { V[i % 2][j] = MP(1e9, 0); } FOR(j, 0, M - 1) { X[j] = 0; } x1 = 0; add = 0; ad(i % 2, x1 + S[i - 1][ktory + 2], MP(V[1 - i % 2][0].f + add, V[1 - i % 2][0].s)); ad(i % 2, x1, V[1 - i % 2][0]); FOR(j, 1, S[i - 1][0] - 1) { increment(i - 1); ad(i % 2, x1 + S[i - 1][ktory + 2], MP(V[1 - i % 2][j].f + add, V[1 - i % 2][j].s)); ad(i % 2, x1, V[1 - i % 2][j]); } } debug(sum); FOR(i, 1, n) { printf("%lld %lld\n", V[n % 2][i].f, V[n % 2][i].s); } } |