QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#142366#5665. AA Country and King Dreamoonammardab3an#TL 81ms340236kbC++171.9kb2023-08-19 00:28:482023-08-19 00:28:50

Judging History

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

  • [2023-08-19 00:28:50]
  • 评测
  • 测评结果:TL
  • 用时:81ms
  • 内存:340236kb
  • [2023-08-19 00:28:48]
  • 提交

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; (i < 2 * n - 1) && v[i]; i++) {
		if (!p[v[i]]) {
			p[v[i]] = v[i - 1];
		}
		else {
			s.erase(v[i - 1]);
		}
		s.erase(v[i]);
	}

	for (auto& x : adj) {
		sort(x.begin(), x.end());
	}
	for (; i < v.size() && !v[i];) {
		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: 81ms
memory: 340236kb

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
Time Limit Exceeded

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: