Submission #63899

# Submission time Handle Problem Language Result Execution time Memory
63899 2018-08-03T06:32:52 Z khsoo01 Ranklist Sorting (BOI07_sorting) C++11
100 / 100
10 ms 632 KB
#include<bits/stdc++.h>
#define X first
#define Y second
using namespace std;
typedef pair<int,int> pii;

const int N = 1005, L = 1024;

int n, a[N], dt[N], prv[N], inv[N];
bool stb[N];

vector<int> cpr;
vector<pii> ans;

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) {
		scanf("%d",&a[i]);
		cpr.push_back(a[i]);
	}
	sort(cpr.begin(), cpr.end());
	for(int i=1;i<=n;i++) {
		a[i] = n - (lower_bound(cpr.begin(), cpr.end(), a[i]) - cpr.begin());
	}
	for(int i=1;i<=n;i++) {
		for(int j=i+1;j<=n;j++) {
			if(a[i] > a[j]) {
				inv[i]++;
				inv[j]++;
			}
		}
	}
	int X = 0;
	for(int i=1;i<=n;i++) {
		int P = i + a[i] - inv[i];
		for(int j=0;j<i;j++) {
			if(a[i] < a[j] || dt[i] > dt[j] + P) continue;
			dt[i] = dt[j] + P;
			prv[i] = j;
		}
		if(dt[X] < dt[i]) X = i;
	}
	for(int i=X;i;i=prv[i]) {
		stb[i] = true;
	}
	for(int i=1;i<=n;i++) {
		if(stb[i]) continue;
		if(i > 1 && stb[i-1] && a[i] < a[i-1]) {
			int j;
			for(j=i-1;j>=1;j--) {
				if(a[j] < a[j+1]) break;
				swap(a[j], a[j+1]);
			}
			ans.push_back({i, j+1});
			stb[i] = true;
		}
		else {
			int j;
			for(j=i+1;j<=n;j++) {
				if(stb[j] && a[j] > a[j-1]) break;
				swap(a[j], a[j-1]);
				swap(stb[j], stb[j-1]);
			}
			ans.push_back({i, j-1});
			stb[j-1] = true;
			i--;
		}
	}
	printf("%d\n",(int)ans.size());
	for(auto &T : ans) {
		printf("%d %d\n",T.X,T.Y);
	}
}

Compilation message

sorting.cpp: In function 'int main()':
sorting.cpp:17:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
  ~~~~~^~~~~~~~~
sorting.cpp:19:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&a[i]);
   ~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 248 KB Output is correct
2 Correct 2 ms 484 KB Output is correct
3 Correct 3 ms 508 KB Output is correct
4 Correct 3 ms 508 KB Output is correct
5 Correct 3 ms 568 KB Output is correct
6 Correct 3 ms 568 KB Output is correct
7 Correct 5 ms 616 KB Output is correct
8 Correct 8 ms 616 KB Output is correct
9 Correct 5 ms 616 KB Output is correct
10 Correct 10 ms 632 KB Output is correct