Submission #64045

# Submission time Handle Problem Language Result Execution time Memory
64045 2018-08-03T08:49:40 Z 윤교준(#1870) Ranklist Sorting (BOI07_sorting) C++11
30 / 100
5 ms 488 KB
#include <bits/stdc++.h>
#define rf(x) (x)=0;while(*p<48)p++;while(47<*p)(x)=((x)<<3)+((x)<<1)+(*p++&15);
#define pb push_back
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define befv(V) ((V)[(sz(V)-2)])
#define allv(V) ((V).begin()),((V).end())
#define sorv(V) sort(allv(V))
#define revv(V) reverse(allv(V))
#define univ(V) (V).erase(unique(allv(V)),(V).end())
#define clv(V) (V).clear()
#define upmin(a,b) (a)=min((a),(b))
#define upmax(a,b) (a)=max((a),(b))
#define rb(x) ((x)&(-(x)))
#define INF (0x3f3f3f3f)
#define INFLL (0x3f3f3f3f3f3f3f3fll)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

const bool debug = 0;
const int MAXN = 2005;

vector<pii> Ans;

int A[MAXN], B[MAXN];

int N;

int main() {
	ios::sync_with_stdio(false);

	cin >> N;
	{
		vector<int> V;
		for(int i = 1; i <= N; i++) {
			cin >> A[i];
			V.eb(A[i]);
		}
		sorv(V);
		for(int i = 1; i <= N; i++)
			A[i] = N - int(lower_bound(allv(V), A[i]) - V.begin());
	}

	if(debug) {
		for(int i = 1; i <= N; i++) printf("%d ", A[i]); puts("");
	}

	if(N <= 10) {
		int ret = INF;
		for(int key = 0; key < (1<<N); key++) {
			vector<pii> V; int sum = 0;
			for(int i = 1; i <= N; i++) B[i] = A[i];

			for(int i = N; i; i--) if(key & (1<<(i-1))) {
				int idx1 = N+1, idx = -1;
				for(int j = 1; j <= N; j++) if(B[j] == i+1) idx1 = j;
				for(int j = 1; j <= N; j++) if(B[j] == i) idx = j;
				idx1 = max(1, idx1-1);
				if(idx == idx1) continue;

				V.eb(idx, idx1); sum += idx + idx1;
				if(idx < idx1) rotate(B+idx, B+idx+1, B+idx1+1);
				else rotate(B+idx1, B+idx, B+idx+1);
			}

			bool flag = false;
			for(int i = 1; i <= N; i++) if(i != B[i]) flag = true;
			if(flag) continue;

			if(ret <= sum) continue;
			ret = sum; swap(Ans, V);
		}
	} else {
		for(int i = N; i; i--) {
			int idx = -1;
			for(int j = 1; j <= i; j++) if(i == A[j]) idx = j;
			if(idx == i) continue;

			Ans.eb(idx, i);

			rotate(A+idx, A+idx+1, A+i+1);

			if(debug) {
				for(int j = 1; j <= N; j++) printf("%d ", A[j]); puts("");
			}
		}
	}

	printf("%d\n", sz(Ans));
	for(auto &p : Ans) printf("%d %d\n", p.first, p.second);
	return 0;
}

Compilation message

sorting.cpp: In function 'int main()':
sorting.cpp:47:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   for(int i = 1; i <= N; i++) printf("%d ", A[i]); puts("");
   ^~~
sorting.cpp:47:52: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   for(int i = 1; i <= N; i++) printf("%d ", A[i]); puts("");
                                                    ^~~~
sorting.cpp:86:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
     for(int j = 1; j <= N; j++) printf("%d ", A[j]); puts("");
     ^~~
sorting.cpp:86:54: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
     for(int j = 1; j <= N; j++) printf("%d ", A[j]); puts("");
                                                      ^~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 5 ms 488 KB Output is correct
3 Incorrect 3 ms 488 KB Output isn't correct
4 Incorrect 3 ms 488 KB Output isn't correct
5 Incorrect 3 ms 488 KB Output isn't correct
6 Incorrect 3 ms 488 KB Output isn't correct
7 Incorrect 4 ms 488 KB Output isn't correct
8 Incorrect 4 ms 488 KB Output isn't correct
9 Correct 4 ms 488 KB Output is correct
10 Incorrect 5 ms 488 KB Output isn't correct