QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#270033#5665. AA Country and King DreamoonSTnofarjo#RE 0ms3640kbC++201.0kb2023-11-30 14:09:252023-11-30 14:09:26

Judging History

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

  • [2023-11-30 14:09:26]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3640kb
  • [2023-11-30 14:09:25]
  • 提交

answer

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

void solve() {
	int n;
	cin >> n;
	int m = 2*n-1;
	vector<int> a(m);
	for (int i=0; i<m; i++) cin >> a[i];

	a[0] = a[m-1] = 1;
	set<int> belum;
	for (int i=1; i<=n; i++) belum.insert(i);
	for (int i=0; i<m; i++) belum.erase(a[i]);

	set<pair<int, int>> udah;
	for (int i=1; i<m; i++) {
		if (a[i-1] != 0 && a[i] != 0) udah.emplace(a[i-1], a[i]);
	}

	stack<int> st;
	int cur = 1;
	for (int i=1; i<m; i++) {
		if (a[i] == 0) {
			if (belum.empty() || (!st.empty() && st.top() < *belum.begin() && !udah.count({cur, st.top()}))) {
				a[i] = st.top();
				st.pop();
			} else {
				a[i] = *belum.begin();
				belum.erase(belum.begin());
				st.push(cur);
			}
		} else {
			if (!st.empty() && st.top() == a[i]) {
				st.pop();
			} else {
				st.push(cur);
			}
		}
		cur = a[i];
	}

	for (int i=0; i<m; i++) cout << a[i] << ' ';
	cout << '\n';
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	int tcs;
	cin >> tcs;
	while (tcs--) {
		solve();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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
Runtime Error

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:


result: