Submission #2123614


Source Code Expand

#include <bits/stdc++.h>
// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <cmath>
#include <iomanip>
#include <time.h>
#include <unistd.h>
#define dibs reserve
#define OVER9000 1234567890
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-8
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):x)
#define uint unsigned int
#define dbl long double
#define pi 3.14159265358979323846
using namespace std;
// mylittledoge

typedef long long cat;

#ifdef DONLINE_JUDGE
	// palindromic tree is better than splay tree!
	#define lld I64d
#endif

int ops[25000];
int cnt_ops =0;

void move(int cur, vector<int> &P, vector<int> &par) {
	assert(cnt_ops < 14000);
	ops[cnt_ops++] =cur;
	int v =cur, tmp =P[0];
	while(true) {
		swap(P[v], tmp);
		if(v == 0) break;
		v =par[v];
	}
}

int main() {
	cin.sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(10);
	int N;
	cin >> N;
	vector<int> P(N), par(N, 0), D(N, 0);
	vector< vector<int> > G(N);
	for(int i =1; i < N; i++) {
		cin >> par[i];
		G[par[i]].push_back(i);
		D[par[i]]++;
	}
	for(int i =0; i < N; i++) cin >> P[i];

	vector<bool> bl(N, false);
	while(!bl[0]) {
		vector<int> in_path(N, -1), in_path_id(N, -1);
		vector< vector< pair<int, int> > > paths;
		for(int i =N-1; i >= 0; i--) if(!bl[i] && D[i] == 0) {
			vector< pair<int, int> > path;
			int v =i;
			while(D[v] <= 1) {
				in_path[v] =paths.size();
				path.push_back(make_pair(v, -1));
				if(v == 0) break;
				v =par[v];
			}
			reverse(begin(path), end(path));
			for(int j =0; j < (int)path.size(); j++) in_path_id[path[j].ff] =j;
			paths.push_back(path);
		}

		int cur =N-1;
		while(true) {
//			for(int i =0; i < N; i++) cerr << P[i] << " "; cerr << "\n";
			int v =P[0];
			if(in_path[v] != -1) {
				int p =in_path[v], x =(int)paths[in_path[v]].size()-1;
				while(paths[p][x].ss > v) x--;
				for(int i =1; i <= x; i++) paths[p][i-1].ss =paths[p][i].ss;
				paths[p][x].ss =v;
				move(paths[p][x].ff, P, par);
			}
			while(cur >= 0) {
				if(bl[cur]) cur--;
				else if(in_path[cur] != -1 && paths[in_path[cur]][in_path_id[cur]].ss != -1) cur--;
				else break;
			}
			if(cur < 0) break;
			if(in_path[v] == -1) {
				move(cur, P, par);
				cur--;
			}
		}
//		for(int i =0; i < N; i++) cerr << P[i] << " "; cerr << ".\n";

		for(int i =0; i < N; i++) if(in_path[i] != -1) {
			bl[i] =true;
			D[par[i]]--;
		}
	}

	cout << cnt_ops << "\n";
	for(int i =0; i < cnt_ops; i++) cout << ops[i] << "\n";
	return 0;}

// look at my code
// my code is amazing

Submission Info

Submission Time
Task H - Generalized Insertion Sort
User xellos
Language C++14 (GCC 5.4.1)
Score 0
Code Size 2930 Byte
Status RE
Exec Time 120 ms
Memory 512 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 2000
Status
AC × 2
AC × 98
RE × 1
Set Name Test Cases
Sample example_0, example_1
All anti_0, anti_1, anti_2, anti_3, anti_4, example_0, example_1, houki2_0, houki2_1, houki2_2, houki2_3, houki2_4, houki2_5, houki2_6, houki2_7, houki2_8, houki2_9, houki_0, houki_1, houki_2, houki_3, houki_4, houki_5, houki_6, houki_7, houki_8, houki_9, houkiex_0, houkiex_1, houkiex_2, houkiex_3, houkiex_4, houkiex_5, houkiex_6, houkiex_7, houkiex_8, houkiex_9, line_0, line_1, line_2, lineex_0, lineex_1, lineex_2, lineex_3, lineex_4, lineex_5, lineex_6, lineex_7, lineex_8, maxrand_0, maxrand_1, maxrand_2, maxrand_3, maxrand_4, maxrand_5, maxrand_6, maxrand_7, maxrand_8, maxrand_9, mosa_0, mosa_1, mosa_2, mosa_3, mosa_4, mosa_5, mosa_6, perfect_0, perfect_1, perfect_2, perfect_3, perfect_4, rand_0, rand_1, rand_2, rand_3, rand_4, smallrand_0, smallrand_1, smallrand_2, smallrand_3, smallrand_4, treearr_0, treearr_1, treearr_2, treearr_3, treearr_4, treearr_5, treearr_6, treearr_7, treearr_8, treearr_9, treelinetree_0, two_0, two_1, two_2, two_3, two_4, two_5, two_6
Case Name Status Exec Time Memory
anti_0 AC 6 ms 512 KB
anti_1 AC 6 ms 512 KB
anti_2 AC 6 ms 512 KB
anti_3 AC 6 ms 512 KB
anti_4 AC 6 ms 512 KB
example_0 AC 1 ms 256 KB
example_1 AC 1 ms 256 KB
houki2_0 AC 3 ms 512 KB
houki2_1 AC 4 ms 512 KB
houki2_2 AC 4 ms 512 KB
houki2_3 AC 6 ms 512 KB
houki2_4 AC 7 ms 512 KB
houki2_5 AC 8 ms 512 KB
houki2_6 AC 9 ms 512 KB
houki2_7 AC 11 ms 512 KB
houki2_8 AC 11 ms 512 KB
houki2_9 AC 3 ms 512 KB
houki_0 AC 3 ms 512 KB
houki_1 AC 4 ms 512 KB
houki_2 AC 5 ms 512 KB
houki_3 AC 7 ms 512 KB
houki_4 AC 8 ms 512 KB
houki_5 AC 9 ms 512 KB
houki_6 AC 11 ms 512 KB
houki_7 AC 12 ms 512 KB
houki_8 AC 13 ms 512 KB
houki_9 AC 3 ms 512 KB
houkiex_0 AC 5 ms 512 KB
houkiex_1 AC 8 ms 512 KB
houkiex_2 AC 11 ms 512 KB
houkiex_3 AC 15 ms 512 KB
houkiex_4 AC 18 ms 512 KB
houkiex_5 AC 22 ms 512 KB
houkiex_6 RE 120 ms 512 KB
houkiex_7 AC 29 ms 512 KB
houkiex_8 AC 30 ms 512 KB
houkiex_9 AC 5 ms 512 KB
line_0 AC 10 ms 512 KB
line_1 AC 10 ms 512 KB
line_2 AC 10 ms 512 KB
lineex_0 AC 10 ms 512 KB
lineex_1 AC 12 ms 512 KB
lineex_2 AC 13 ms 512 KB
lineex_3 AC 11 ms 512 KB
lineex_4 AC 9 ms 512 KB
lineex_5 AC 8 ms 512 KB
lineex_6 AC 6 ms 512 KB
lineex_7 AC 5 ms 512 KB
lineex_8 AC 4 ms 512 KB
maxrand_0 AC 2 ms 512 KB
maxrand_1 AC 2 ms 512 KB
maxrand_2 AC 2 ms 512 KB
maxrand_3 AC 2 ms 512 KB
maxrand_4 AC 2 ms 512 KB
maxrand_5 AC 2 ms 512 KB
maxrand_6 AC 2 ms 512 KB
maxrand_7 AC 2 ms 512 KB
maxrand_8 AC 2 ms 512 KB
maxrand_9 AC 2 ms 512 KB
mosa_0 AC 4 ms 512 KB
mosa_1 AC 4 ms 512 KB
mosa_2 AC 5 ms 512 KB
mosa_3 AC 5 ms 512 KB
mosa_4 AC 4 ms 512 KB
mosa_5 AC 5 ms 512 KB
mosa_6 AC 5 ms 512 KB
perfect_0 AC 2 ms 512 KB
perfect_1 AC 2 ms 512 KB
perfect_2 AC 2 ms 512 KB
perfect_3 AC 2 ms 512 KB
perfect_4 AC 2 ms 512 KB
rand_0 AC 1 ms 256 KB
rand_1 AC 2 ms 384 KB
rand_2 AC 2 ms 384 KB
rand_3 AC 2 ms 384 KB
rand_4 AC 2 ms 384 KB
smallrand_0 AC 1 ms 256 KB
smallrand_1 AC 1 ms 256 KB
smallrand_2 AC 1 ms 256 KB
smallrand_3 AC 1 ms 256 KB
smallrand_4 AC 1 ms 256 KB
treearr_0 AC 3 ms 512 KB
treearr_1 AC 6 ms 512 KB
treearr_2 AC 3 ms 512 KB
treearr_3 AC 3 ms 512 KB
treearr_4 AC 3 ms 512 KB
treearr_5 AC 3 ms 512 KB
treearr_6 AC 3 ms 512 KB
treearr_7 AC 3 ms 512 KB
treearr_8 AC 3 ms 512 KB
treearr_9 AC 6 ms 512 KB
treelinetree_0 AC 3 ms 512 KB
two_0 AC 6 ms 512 KB
two_1 AC 7 ms 512 KB
two_2 AC 7 ms 512 KB
two_3 AC 6 ms 512 KB
two_4 AC 7 ms 512 KB
two_5 AC 6 ms 512 KB
two_6 AC 8 ms 512 KB