CSES - Datatähti 2017 alku - Results
Submission details
Task:Järjestys
Sender:peltola
Submission time:2016-10-16 23:29:34 +0300
Language:C++
Status:BUSTED

Code

//#include <iostream>
//#include <string>
//#include <algorithm>
//#include <vector>

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


int main() {
	
	int n;
	cin >> n;
	
//	string op;
//	op = "";
	
//	unsigned int h;
//	h = 0;
	
	unsigned int x[n];
	
	vector<unsigned int> a;
	
	for(int i = 0; i < n; i++) {
	
		int o;
		cin >> o;
		
		a.push_back(o);
		x[o - 1] = i;
		
	}
	
	vector<int> op;
	
	for(unsigned int i = n; i > 1; i--) {
	
		for(unsigned int j = x[i - 1]; j >= 0; j--) {
			
//			j = min(j, a.size() - 1);
			
			if(j >= a.size()) {
				j = a.size() - 1;
			} 
			
			if(a[j] == i) {
				
				if(j == i - 1) break;
				
				if(j + 1 > 1) {
					op.push_back(j + 1);
//					h++;
				}
				
				if(i > 1) {
					op.push_back(i);
//					h++;
				}
				
				if(i - 1 > 1) {
					op.push_back(i - 1);
//					h++;
				}
				
				if(j > 1) {
					op.push_back(j);
//					h++;
				}

				a.erase(a.begin() + j);
				break;
				
			}
			
		}
		
	}

	cout << op.size();
	cout << "\n";
	
//	for(int i = 0; i < op.size(); i++) {
//		cout << op[i];
//		cout << " ";
//	}

	for(int z : op)
		cout << z << " ";
	
	cout << "\n";

	return 0;
}