QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#111824#5665. AA Country and King Dreamoonxaphoenix#WA 32ms9568kbC++172.7kb2023-06-08 21:16:032023-06-08 21:16:20

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-08 21:16:20]
  • 评测
  • 测评结果:WA
  • 用时:32ms
  • 内存:9568kb
  • [2023-06-08 21:16:03]
  • 提交

answer

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

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define LC ch[k][0] 
#define RC ch[k][1]
#define IO cin.sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define rep(i,a,n) for (int i = a; i < n; i++)
#define repn(i,a,n) for (int i = a; i <= n; i++)
#define per(i,a,n) for (int i = n - 1; i >= a; i--)
#define pern(i,a,n) for (int i = n; i >= a; i--)

typedef long long LL;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;

const int N = 600010;
const int M = 610000;
const int mod = 1e9+7;
const int inf = (int)1e9;
const LL INF = (LL)1e18;
const double eps = 1e-9;

int n, a[N], t[N], dep[N];
int fa[N];
vector<int> v1, v2, tmp; 

void work(int mn, int val){
	tmp.clear();
	if (mn > val) {
		repn(i, 1, n) {
			if (dep[i]) continue;
			tmp.pb(i); tmp.pb(val);
		}
	}
	else {
		tmp.pb(mn);
		repn(i, mn + 1, n) {
			if (dep[i]) continue;
			tmp.pb(i); tmp.pb(mn);
		}
		tmp.pb(val);
	}
}

void solve() {
	cin >> n;
	int tot = 2 * n - 1;
	repn(i, 1, n) {
		dep[i] = 0; fa[i] = 0;
	}
	repn(i, 1, tot) cin >> a[i];
	a[1] = 1; a[tot] = 1;
	int cnt = 0;
	repn(i, 1, tot) if (!a[i]) cnt ++;
	if (cnt == 0) {
		repn(i, 1, tot) {
			cout << a[i]; 
			if (i < tot) cout << " "; 
			else cout << "\n";
		}
		return;
	}
	int l, r, pl;
	int d = 0;
	repn(i, 1, n) t[i] = 0;
	repn(i, 1, tot) {
		if (!a[i]) {
			l = a[i - 1]; pl = i;
			break;
		} 
		if (t[a[i]]) d --;
		else {
			fa[a[i]] = a[i - 1];
			d ++; t[a[i]] = 1;
		}
		dep[a[i]] = d;
	}
	d = 0;
	repn(i, 1, n) t[i] = 0;
	pern(i, 1, tot) {
		if (!a[i]) {
			r = a[i + 1];
			break;
		} 
		if (t[a[i]]) d --;
		else {
			fa[a[i]] = a[i + 1];
			d ++; t[a[i]] = 1;
		}
		dep[a[i]] = d;
	}
	v1.clear(); v2.clear();
	if (l != r) {
		v2.pb(r);
		while (l != r) {
			if (dep[l] >= dep[r]) {
				l = fa[l]; if (l != r) v1.pb(l);
			}
			else {
				r = fa[r]; if (l != r) v2.pb(r);
			}
		}
		per(i, 0, v2.size()) v1.pb(v2[i]);
	}
	int mn = n + 1;
	repn(i, 1, n) {
		if (!dep[i]) {
			mn = i; break;
		}
	}
	int val = l;
	bool yes = false;
	for (auto x : v1) {
		if (x > mn && !yes) {
			work(mn, val);
			for (auto num : tmp) {
				a[pl ++] = num;
			}
			yes = true;
		}
		a[pl ++] = x;
		val = x;
	}
	if (!yes) {
		work(mn, val);
		for (auto num : tmp) {
			a[pl ++] = num;
		}
	}
	repn(i, 1, tot) {
		cout << a[i]; 
		if (i < tot) cout << " "; 
		else cout << "\n";
	}
}

int main()
{
	IO;
	int T;
	cin >> T;
	repn(i, 1, T) solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 9568kb

input:

9
5
1 2 3 2 0 2 1 5 1
5
1 2 3 0 0 2 1 5 1
5
1 2 0 0 0 2 1 5 1
5
1 2 0 0 0 0 1 5 1
5
1 0 0 0 0 0 1 5 1
5
1 0 0 0 0 0 0 5 1
5
1 0 0 0 0 0 0 0 1
5
1 0 0 0 0 0 0 0 0
5
0 0 0 0 0 0 0 0 0

output:

1 2 3 2 4 2 1 5 1
1 2 3 2 4 2 1 5 1
1 2 3 2 4 2 1 5 1
1 2 1 3 1 4 1 5 1
1 2 1 3 1 4 1 5 1
1 2 1 3 1 4 1 5 1
1 2 1 3 1 4 1 5 1
1 2 1 3 1 4 1 5 1
1 2 1 3 1 4 1 5 1

result:

ok 9 lines

Test #2:

score: -100
Wrong Answer
time: 32ms
memory: 9516kb

input:

28668
2
0 2 1
2
0 0 1
2
0 0 0
2
1 0 1
2
1 0 0
2
1 2 0
3
0 2 1 3 1
3
0 0 1 3 1
3
0 0 0 3 1
3
0 0 0 0 1
3
0 0 0 0 0
3
1 0 1 3 1
3
1 0 0 3 1
3
1 0 0 0 1
3
1 0 0 0 0
3
1 2 0 3 1
3
1 2 0 0 1
3
1 2 0 0 0
3
1 2 1 0 1
3
1 2 1 0 0
3
1 2 1 3 0
3
0 2 3 2 1
3
0 0 3 2 1
3
0 0 0 2 1
3
1 0 3 2 1
3
1 0 0 2 1
3
1 2 ...

output:

1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 3 2 1
1 2 3 2 1
1 2 3 2 1
1 2 3 2 1
1 2 3 2 1
1 2 3 2 1
1 2 3 2 1
1 2 3 2 1
1 2 3 2 1
1 3 1 2 1
1 3 1 2 1
1 3 ...

result:

wrong answer 79th lines differ - expected: '1 2 1 3 4 3 1', found: '1 2 1 4 1 3 1'