QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#142444#5665. AA Country and King DreamoonThe_HallakWA 104ms340996kbC++171.8kb2023-08-19 06:27:462023-08-19 06:27:49

Judging History

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

  • [2023-08-19 06:27:49]
  • 评测
  • 测评结果:WA
  • 用时:104ms
  • 内存:340996kb
  • [2023-08-19 06:27:46]
  • 提交

answer

#include <bits/stdc++.h>

#define ll long long

using namespace std;

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

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

int solve() {

	int n; cin >> n;
	vector<ll>v(2 * n - 1);
	for (auto& x : v)cin >> x;
	
	for (int i = 2; i <= n; i++)s.insert(i);

	if (v.back()) {
		for (int i = v.size() - 2; i >= 0 && v[i]; i--) {
			if (!p[v[i]]) {
				p[v[i]] = v[i + 1];
				adj[v[i + 1]].emplace_back(v[i]);
			}
			if (p[v[i]] == v[i - 1])s.erase(v[i]);
			cant.insert(v[i]);
			
		}
	}
	else {
		v.back() = 1;
	}
	int i = 1;
	if (v[0]) {
		for (; i < v.size() && v[i]; i++) {
			s.erase(v[i]);
			if (!p[v[i]])p[v[i]] = v[i - 1];
		}
	}
	else {
		v[0] = 1;
	}
	p[1] = 1e8;
	for (i; i < v.size() && !v[i]; i++) {
		if (adj[v[i-1]].size()) {
			if (!s.count(adj[v[i - 1]].front())) {
				adj[v[i - 1]].pop_front();
				i--;
				continue;
			}

			if (*s.begin() == adj[v[i-1]].front()) {
				adj[v[i-1]].pop_front();
			}

			v[i] = *s.begin();
			p[v[i]] = v[i - 1];
			s.erase(s.begin());

			continue;
		}

		if (!cant.count(v[i-1])) {
			if (s.size()&&*s.begin() < p[v[i-1]]) {
				v[i] = *s.begin();
				p[v[i]] = v[i - 1];
				s.erase(s.begin());
			}
			else {
				v[i] = p[v[i - 1]];
			}
		}
		else {
			if (s.empty())break;
			v[i] = *s.begin();
			p[v[i]] = v[i - 1];
			s.erase(s.begin());
		}

	}
	for (auto x : v)cout << x << " ";
	cout << '\n';
	for (int i = 1; i <= n; i++) {
		p[i] = 0;
		adj[i].clear();
	}
	cant.clear();
	s.clear();
	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: 74ms
memory: 339960kb

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: 104ms
memory: 340996kb

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...

result:

wrong answer 41st lines differ - expected: '1 3 2 3 1', found: '1 2 2 3 1 '