CSES - Datatähti 2017 alku - Results
Submission details
Task:Järjestys
Sender:kh
Submission time:2016-10-16 21:19:30 +0300
Language:C++
Status:BUSTED

Code

#include <bits/stdc++.h>
using namespace std;

int main() {
	
	int luku;
	cin >> luku;

	unsigned int d[luku];
	vector<unsigned int> x;

	for(int i = 0; i < luku; i++) {
		int t;
		cin >> t;
		x.push_back(t);
		d[t - 1] = i;
	}

	vector<int> m;
	for(unsigned int i = luku; i > 1; i--) {
		for(unsigned int ii = d[i - 1]; ii >= 0; ii--) {
			if(ii >= x.size()) ii = x.size() - 1;
			if(x[ii] == i) {
				if(ii == i - 1) break;
				if(ii + 1 > 1) m.push_back(ii + 1);
				if(i      > 1) m.push_back(i);
				if(i - 1  > 1) m.push_back(i - 1);
				if(ii     > 1) m.push_back(ii);

				x.erase(x.begin() + ii);
				break;
			}
		}
	}

	cout << m.size() << "\n";
	for(int i : m)
		cout << i << " ";
	cout << "\n";

	return 0;
}