QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#142365#5665. AA Country and King Dreamoonammardab3an#RE 72ms340244kbC++171.9kb2023-08-19 00:27:162023-08-19 00:27:19

Judging History

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

  • [2023-08-19 00:27:19]
  • 评测
  • 测评结果:RE
  • 用时:72ms
  • 内存:340244kb
  • [2023-08-19 00:27:16]
  • 提交

answer

#include <bits/stdc++.h>

#define ll long long

using namespace std;

const ll N = 5e5 + 10, MM = 1e9 + 7, MAX = 1e18;

set<ll>s, cant;
int p[N];
deque<ll>adj[N];

int solve() {
	int n; cin >> n;
	for (int i = 1; i <= n; i++) {
		p[i] = 0;
		adj[i].clear();
	}
	s.clear();
	cant.clear();
	vector<ll>v(2 * n - 1);
	for (int i = 1; i <= n; i++) {
		s.insert(i);
	}

	for (auto& x : v)cin >> x;
	for (int i = 2 * n - 3; v[i]; i--) {
		if (!p[v[i]]) {
			p[v[i]] = v[i + 1];
			adj[v[i + 1]].push_back(v[i]);
		}
		if (p[v[i + 1]] == v[i]) {
			s.erase(v[i + 1]);
		}
		cant.insert(v[i]);
	}

	v[0] = 1;
	p[1] = 100000000;
	s.erase(1);
	int i = 1;

	for (i = 1; v[i]&&i<2*n-1; i++) {
		if (!p[v[i]]) {
			p[v[i]] = v[i - 1];
		}
		else {
			s.erase(v[i - 1]);
		}
		s.erase(v[i]);
	}

	assert(i < v.size());
	for (auto& x : adj) {
		sort(x.begin(), x.end());
	}
	for (; !v[i]&&i<v.size();) {
		while (adj[v[i - 1]].size()) {
			if (!s.count(adj[v[i - 1]].front()))adj[v[i - 1]].pop_front();
			else if (*s.begin() == adj[v[i - 1]].front()) {
				s.erase(s.begin());
				v[i] = adj[v[i - 1]].front();
				p[v[i]] = v[i - 1];
				i++;
				adj[v[i - 1]].pop_front();
			}
			else {
				v[i] = *s.begin();
				p[v[i]] = v[i - 1];
				i++;

				s.erase(s.begin());
			}
		}
		while (s.size() && *s.begin() < p[v[i - 1]]) {
			v[i] = *s.begin();
			p[v[i]] = v[i - 1];
			i++;
			s.erase(s.begin());
		}
		if (!cant.count(v[i - 1])) {
			v[i] = p[v[i - 1]];
			i++;
		}
		else {
			v[i] = *s.begin();
			s.erase(s.begin());
			p[v[i]] = v[i - 1];
			i++;
		}
	}
	for (auto x : v)cout << x << " ";
	cout << '\n';
	return 0;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t = 1;
	cin >> t;
	int i = 1;
	while (t--) {
		//cout << "case " << i << ": ";
		solve();
	}
	return 0;
}
/*

5
0 2 3 2 0 2 1 5 1

*/

详细

Test #1:

score: 100
Accepted
time: 72ms
memory: 340244kb

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
Dangerous Syscalls

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: