Submission #63916

# Submission time Handle Problem Language Result Execution time Memory
63916 2018-08-03T07:03:44 Z ainta(#1864) Ranklist Sorting (BOI07_sorting) C++11
100 / 100
19 ms 8540 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

sorting.cpp: In function 'void Calc()':
sorting.cpp:31:6: warning: unused variable 'Mx' [-Wunused-variable]
  int Mx = 0, ck = 0, i, j;
      ^~
sorting.cpp:31:14: warning: unused variable 'ck' [-Wunused-variable]
  int Mx = 0, ck = 0, i, j;
              ^~
sorting.cpp:31:22: warning: unused variable 'i' [-Wunused-variable]
  int Mx = 0, ck = 0, i, j;
                      ^
sorting.cpp: In function 'int main()':
sorting.cpp:98:38: warning: unused variable 'c' [-Wunused-variable]
    int t = D[i - 1][j] + Cost[j][i], c = 0;
                                      ^
sorting.cpp:113:26: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::vector<std::pair<int, int> >::size_type {aka long unsigned int}' [-Wformat=]
  printf("%d\n", RR.size());
                 ~~~~~~~~~^
sorting.cpp:114:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (i = 0; i < RR.size(); i++)printf("%d %d\n", RR[i].first, RR[i].second);
              ~~^~~~~~~~~~~
sorting.cpp:61:12: warning: unused variable 'k' [-Wunused-variable]
  int i, j, k;
            ^
sorting.cpp:62:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
sorting.cpp:64:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &ord[i].a);
   ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 2 ms 564 KB Output is correct
4 Correct 2 ms 896 KB Output is correct
5 Correct 2 ms 1284 KB Output is correct
6 Correct 4 ms 2080 KB Output is correct
7 Correct 8 ms 4512 KB Output is correct
8 Correct 14 ms 6816 KB Output is correct
9 Correct 19 ms 8480 KB Output is correct
10 Correct 19 ms 8540 KB Output is correct