답안 #799665

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
799665 2023-07-31T18:23:03 Z GusterGoose27 Parking (CEOI22_parking) C++17
100 / 100
113 ms 20776 KB
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 2e5+5;
int n, m;
vector<int> config[MAXN];
int inv[2*MAXN]; // 2*i+0 for bottom 1 for top
bool vis[MAXN];

typedef pair<int, int> pii;

vector<pii> res;
set<int> avail;

int get_empty() {
	if (avail.empty()) {
		// assert(false);
		cout << -1 << endl;
		exit(0);
	}
	return *avail.begin();
}

void move(int a, int b) {
	assert(config[a].size());
	assert(config[b].size() < 2);
	assert(config[b].empty() || config[a].back() == (config[b].back()^1));

	res.push_back(pii(a+1, b+1));
	if (config[b].empty()) avail.erase(b);
	config[b].push_back(config[a].back());
	config[a].pop_back();
	if (config[a].empty()) avail.insert(a);
	inv[config[b].back()] = 2*b+config[b].size()-1;
}

void dfs(int cur, bool picky = 0) {
	int v = config[cur][0];
	int p = inv[v^1]/2;
	if (config[p].size() == 1) {
		move(p, cur);
		return;
	}
	if (inv[v^1]&1) {
		move(p, cur);
		return dfs(p, picky);
	}
	if (picky) return;
	// if we are going to be picky, then we might as well do it from the other direction
	int s = p;
	vector<pii> qu;
	while (config[s].size() == 2 && !(inv[config[s][1]^1] & 1)) {
		int nxt = inv[config[s][1]^1]/2;
		qu.push_back(pii(s, nxt));
		s = nxt;
	}
	if (config[s].size() == 2) {
		int e = get_empty();
		move(s, e);
		s = e;
	}
	for (int i = qu.size()-1; i >= 0; i--) move(qu[i].first, qu[i].second);
	move(p, cur);
	if (config[s].size() == 1) dfs(s, picky);
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int x, y; cin >> x >> y;
		if (x > 0) {
			config[i].push_back(2*(x-1)+vis[x-1]);
			vis[x-1] = 1;
		}
		if (y > 0) {
			config[i].push_back(2*(y-1)+vis[y-1]);
			vis[y-1] = 1;
		}
	}
	for (int i = 0; i < m; i++) {
		for (int j = 0; j < config[i].size(); j++) {
			inv[config[i][j]] = 2*i+j;
		}
		if (config[i].empty()) avail.insert(i);
	}
	// first step: combine anything that can be combined without extra space usage
	/*
	Clearly, this will only be possible if we have two endpoints that can just be simply connected
	(i.e)
	a
	b a
	c b
	d c
	d
	*/
	for (int i = 0; i < m; i++) {
		if (config[i].size() == 1) {
			dfs(i, 1);
		}
	}

	// now, for each single bottom, combine it (again, this frees space).
	// this will always take exactly 1 empty spot, so this is the best option
	for (int i = 0; i < m; i++) {
		if (config[i].size() == 1) {
			dfs(i);
		}
	}
	// nothing else will free up space, so order no longer matters

	// ERROR HERE DUE TO SUBTASK 2
	for (int i = 0; i < m; i++) {
		if (config[i].empty()) continue;
		assert(config[i].size() == 2);
		if (config[i][0] == (config[i][1]^1)) continue;
		if (!(inv[config[i][1]^1] & 1)) continue; // for any cycle that has two top nodes, we want to start from one of those
		int e = get_empty();
		move(i, e);
		dfs(i, 0);
	}
	for (int i = 0; i < m; i++) {
		if (config[i].empty()) continue;
		assert(config[i].size() == 2);
		if (config[i][0] == (config[i][1]^1)) continue;
		// if (!(inv[config[i][1]^1] & 1)) continue; // for any cycle that has two top nodes, we want to start from one of those
		int e = get_empty();
		move(i, e);
		dfs(i, 0);
	}
	for (int i = 0; i < m; i++) assert(config[i].empty() || (config[i].size() == 2 && (config[i][0]^1) == config[i][1]));
	cout << res.size() << '\n';
	for (pii p: res) {
		cout << p.first << ' ' << p.second << '\n';
	}
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:83:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |   for (int j = 0; j < config[i].size(); j++) {
      |                   ~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5036 KB Output is correct
2 Correct 2 ms 5000 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 4 ms 4948 KB Output is correct
10 Correct 2 ms 5032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 96 ms 17120 KB Output is correct
2 Correct 96 ms 17276 KB Output is correct
3 Correct 76 ms 16312 KB Output is correct
4 Correct 72 ms 16212 KB Output is correct
5 Correct 85 ms 17620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 2 ms 5040 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 2 ms 5040 KB Output is correct
6 Correct 2 ms 5040 KB Output is correct
7 Correct 2 ms 5036 KB Output is correct
8 Correct 2 ms 5076 KB Output is correct
9 Correct 3 ms 5040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 2 ms 5040 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 2 ms 5040 KB Output is correct
6 Correct 2 ms 5040 KB Output is correct
7 Correct 2 ms 5036 KB Output is correct
8 Correct 2 ms 5076 KB Output is correct
9 Correct 3 ms 5040 KB Output is correct
10 Correct 99 ms 20556 KB Output is correct
11 Correct 43 ms 15212 KB Output is correct
12 Correct 55 ms 15220 KB Output is correct
13 Correct 86 ms 19456 KB Output is correct
14 Correct 47 ms 15436 KB Output is correct
15 Correct 43 ms 15116 KB Output is correct
16 Correct 110 ms 20776 KB Output is correct
17 Correct 42 ms 15008 KB Output is correct
18 Correct 104 ms 20280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 2 ms 5076 KB Output is correct
3 Correct 3 ms 5036 KB Output is correct
4 Correct 2 ms 5076 KB Output is correct
5 Correct 2 ms 5040 KB Output is correct
6 Correct 4 ms 5076 KB Output is correct
7 Correct 3 ms 5076 KB Output is correct
8 Correct 3 ms 5076 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5036 KB Output is correct
11 Correct 3 ms 5040 KB Output is correct
12 Correct 3 ms 5036 KB Output is correct
13 Correct 2 ms 5036 KB Output is correct
14 Correct 2 ms 5040 KB Output is correct
15 Correct 2 ms 5076 KB Output is correct
16 Correct 2 ms 5076 KB Output is correct
17 Correct 2 ms 5076 KB Output is correct
18 Correct 3 ms 5076 KB Output is correct
19 Correct 2 ms 5076 KB Output is correct
20 Correct 2 ms 5076 KB Output is correct
21 Correct 2 ms 4980 KB Output is correct
22 Correct 2 ms 5036 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5036 KB Output is correct
2 Correct 2 ms 5000 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 2 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 4 ms 4948 KB Output is correct
10 Correct 2 ms 5032 KB Output is correct
11 Correct 96 ms 17120 KB Output is correct
12 Correct 96 ms 17276 KB Output is correct
13 Correct 76 ms 16312 KB Output is correct
14 Correct 72 ms 16212 KB Output is correct
15 Correct 85 ms 17620 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 2 ms 5040 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 2 ms 5040 KB Output is correct
21 Correct 2 ms 5040 KB Output is correct
22 Correct 2 ms 5036 KB Output is correct
23 Correct 2 ms 5076 KB Output is correct
24 Correct 3 ms 5040 KB Output is correct
25 Correct 99 ms 20556 KB Output is correct
26 Correct 43 ms 15212 KB Output is correct
27 Correct 55 ms 15220 KB Output is correct
28 Correct 86 ms 19456 KB Output is correct
29 Correct 47 ms 15436 KB Output is correct
30 Correct 43 ms 15116 KB Output is correct
31 Correct 110 ms 20776 KB Output is correct
32 Correct 42 ms 15008 KB Output is correct
33 Correct 104 ms 20280 KB Output is correct
34 Correct 3 ms 5076 KB Output is correct
35 Correct 2 ms 5076 KB Output is correct
36 Correct 3 ms 5036 KB Output is correct
37 Correct 2 ms 5076 KB Output is correct
38 Correct 2 ms 5040 KB Output is correct
39 Correct 4 ms 5076 KB Output is correct
40 Correct 3 ms 5076 KB Output is correct
41 Correct 3 ms 5076 KB Output is correct
42 Correct 3 ms 5076 KB Output is correct
43 Correct 3 ms 5036 KB Output is correct
44 Correct 3 ms 5040 KB Output is correct
45 Correct 3 ms 5036 KB Output is correct
46 Correct 2 ms 5036 KB Output is correct
47 Correct 2 ms 5040 KB Output is correct
48 Correct 2 ms 5076 KB Output is correct
49 Correct 2 ms 5076 KB Output is correct
50 Correct 2 ms 5076 KB Output is correct
51 Correct 3 ms 5076 KB Output is correct
52 Correct 2 ms 5076 KB Output is correct
53 Correct 2 ms 5076 KB Output is correct
54 Correct 2 ms 4980 KB Output is correct
55 Correct 2 ms 5036 KB Output is correct
56 Correct 3 ms 5076 KB Output is correct
57 Correct 3 ms 5076 KB Output is correct
58 Correct 105 ms 20196 KB Output is correct
59 Correct 113 ms 20584 KB Output is correct
60 Correct 98 ms 19960 KB Output is correct
61 Correct 95 ms 19696 KB Output is correct
62 Correct 43 ms 15436 KB Output is correct
63 Correct 88 ms 19140 KB Output is correct
64 Correct 44 ms 15508 KB Output is correct
65 Correct 94 ms 20080 KB Output is correct
66 Correct 101 ms 20644 KB Output is correct
67 Correct 42 ms 14932 KB Output is correct
68 Correct 88 ms 19708 KB Output is correct
69 Correct 86 ms 19264 KB Output is correct
70 Correct 43 ms 15096 KB Output is correct
71 Correct 43 ms 15348 KB Output is correct
72 Correct 58 ms 15176 KB Output is correct
73 Correct 102 ms 20776 KB Output is correct
74 Correct 45 ms 15096 KB Output is correct
75 Correct 103 ms 20244 KB Output is correct
76 Correct 94 ms 20624 KB Output is correct
77 Correct 102 ms 20144 KB Output is correct
78 Correct 43 ms 15328 KB Output is correct
79 Correct 103 ms 20008 KB Output is correct
80 Correct 42 ms 15028 KB Output is correct
81 Correct 89 ms 20024 KB Output is correct