Submission #63871

# Submission time Handle Problem Language Result Execution time Memory
63871 2018-08-03T05:59:25 Z ainta(#1864) Ranklist Sorting (BOI07_sorting) C++11
40 / 100
1000 ms 640 KB
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
#define pii pair<int,int>
int n, w[1010], ow[1010];
struct point {
	int a, num;
	bool operator < (const point &p)const {
		return a < p.a;
	}
}ord[1010];
int 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 });
	}
	for (i = 1; i <= n; i++) {
		w[i] = ow[i];
		if (chk[i] == 2)chk[i] = 0;
	}
	if (res > cost) {
		res = cost;
		RR = V;
	//	printf("%d\n", res);
	}
}
void DFS(int a, int M) {
	if (a == n + 1) {
		Calc();
		return;
	}
	if (M < w[a]) {
		chk[w[a]] = 1;
		DFS(a + 1, w[a]);
		chk[w[a]] = 0;
	}
	DFS(a + 1, M);
}
int main() {
	int i, j;
	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;
	}
	for (i = 1; i <= n; i++)ow[i] = w[i];
	DFS(1,0);
	printf("%d\n", RR.size());
	for (auto &t : RR) {
		printf("%d %d\n", t.first, t.second);
	}
}

Compilation message

sorting.cpp: In function 'void Calc()':
sorting.cpp:30:6: warning: unused variable 'Mx' [-Wunused-variable]
  int Mx = 0, ck = 0, i, j;
      ^~
sorting.cpp:30:14: warning: unused variable 'ck' [-Wunused-variable]
  int Mx = 0, ck = 0, i, j;
              ^~
sorting.cpp: In function 'int main()':
sorting.cpp:87: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:75:9: warning: unused variable 'j' [-Wunused-variable]
  int i, j;
         ^
sorting.cpp:76:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
sorting.cpp:78: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 376 KB Output is correct
3 Correct 3 ms 436 KB Output is correct
4 Correct 458 ms 640 KB Output is correct
5 Execution timed out 1081 ms 640 KB Time limit exceeded
6 Execution timed out 1079 ms 640 KB Time limit exceeded
7 Execution timed out 1073 ms 640 KB Time limit exceeded
8 Execution timed out 1081 ms 640 KB Time limit exceeded
9 Execution timed out 1076 ms 640 KB Time limit exceeded
10 Execution timed out 1079 ms 640 KB Time limit exceeded