# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
63904 | 2018-08-03T06:43:23 Z | ainta(#1864) | Ranklist Sorting (BOI07_sorting) | C++11 | 395 ms | 4588 KB |
#include<cstdio> #include<algorithm> #include<vector> using namespace std; #define pii pair<int,int> int n, w[1010], D[1010][1010], Path[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 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++) { 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], c = 0; for (k = 1; k < i; k++) { if (k > j)t -= c; if (w[k] > w[i]) { c++; if (k < j) { t += i - j; } else { t += i - k; } } } 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 | 3 ms | 484 KB | Output is correct |
3 | Correct | 3 ms | 484 KB | Output is correct |
4 | Correct | 3 ms | 612 KB | Output is correct |
5 | Correct | 4 ms | 868 KB | Output is correct |
6 | Correct | 6 ms | 1328 KB | Output is correct |
7 | Correct | 55 ms | 2572 KB | Output is correct |
8 | Correct | 249 ms | 3768 KB | Output is correct |
9 | Correct | 395 ms | 4556 KB | Output is correct |
10 | Correct | 354 ms | 4588 KB | Output is correct |