# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
63920 | 2018-08-03T07:05:31 Z | ainta | Ranklist Sorting (BOI07_sorting) | C++17 | 28 ms | 8568 KB |
#include<cstdio> #include<algorithm> #include<vector> using namespace std; #define pii pair<int,int> int n, w[1010], D[1010][1010], Path[1010], Cost[1010][1010]; struct point { int a, num; bool operator < (const point &p)const { return a < p.a; } }ord[1010]; int G[1010], chk[1010]; vector<pii>RR; void Move(int b, int e) { int t = w[b]; if (b < e) { int i; for (i = b; i < e; i++)w[i] = w[i + 1]; w[e] = t; } else { int i; for (i = b; i > e; i--)w[i] = w[i - 1]; w[e] = t; } } int res = 1e9; void Calc() { int Mx = 0, ck = 0, i, j; vector<pii>V; int cost = 0; while (1) { for (j = 1; j <= n; j++) { if (!chk[w[j]])break; } if (j == n + 1)break; int b = j; int pv = 0; for (j = 1; j <= n; j++) { if (chk[w[j]]) { if (w[j] > w[b])break; pv = j; } } chk[w[b]] = 2; if (b < pv + 1)pv--; Move(b, pv + 1); cost += b + pv + 1; V.push_back({ b, pv + 1 }); } if (res > cost) { res = cost; RR = V; // printf("%d\n", res); } } int TP[1010]; int main() { int i, j, k; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &ord[i].a); ord[i].num = i; } sort(ord + 1, ord + n + 1); for (i = 1; i <= n; i++) { w[ord[i].num] = n+1-i; } D[0][0] = 0; for (i = 1; i <= n; i++) { G[i] = 1; for (j = 1; j < i; j++)if (w[j] < w[i])G[i]++; } for (i = 1; i <= n; i++) { int c = 0; for (j = 1; j < i; j++) { if (w[j] > w[i])c++; TP[j] = c; } int s = 0, cc = 0, ss = 0; for (j = i-1; j >= 0; j--) { Cost[j][i] -= s; if(j)s += TP[j-1]; if (w[j + 1] > w[i]) { ss += j + 1; cc++; } Cost[j][i] += TP[j] * (i - j) + cc*i - ss; } } for (i = 1; i <= n; i++) { D[i][i] = 1e9; for (j = 0; j < i; j++) { D[i][j] = D[i - 1][j] + i + G[i]; if (w[j] >= w[i])continue; int t = D[i - 1][j] + Cost[j][i], c = 0; if (D[i][i] > t)D[i][i] = t, Path[i] = j; } } int res = 1e9, pv = -1; for (i = 0; i <= n; i++) { if (res > D[n][i])res = D[n][i], pv = i; } for (i = n; i >= 1; i--) { if (i == pv) { chk[w[i]] = 1; pv = Path[i]; } } Calc(); printf("%d\n", RR.size()); for (i = 0; i < RR.size(); i++)printf("%d %d\n", RR[i].first, RR[i].second); }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 3 ms | 376 KB | Output is correct |
2 | Correct | 4 ms | 612 KB | Output is correct |
3 | Correct | 3 ms | 612 KB | Output is correct |
4 | Correct | 4 ms | 868 KB | Output is correct |
5 | Correct | 3 ms | 1284 KB | Output is correct |
6 | Correct | 4 ms | 2052 KB | Output is correct |
7 | Correct | 10 ms | 4508 KB | Output is correct |
8 | Correct | 21 ms | 6888 KB | Output is correct |
9 | Correct | 28 ms | 8552 KB | Output is correct |
10 | Correct | 25 ms | 8568 KB | Output is correct |