Submission #760574

# Submission time Handle Problem Language Result Execution time Memory
760574 2023-06-17T20:44:53 Z kingfran1907 Parking (CEOI22_parking) C++14
85 / 100
281 ms 46764 KB
#include <bits/stdc++.h>
#define X first
#define Y second

using namespace std;
typedef long long llint;

const int maxn = 2e5+10;
const int base = 31337;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const int logo = 20;
const int off = 1 << logo;
const int treesiz = off << 1;

int n, m;
int niz[maxn][2];
vector< pair<int, int> > pos[maxn];
vector< pair<int, int> > sol;
int nex[maxn], rev[maxn];
bool bio[2 * maxn];
set< int > emps;
vector< int > graph[2 * maxn];
vector< vector< int > > chains, cycles;

void stop() {
	printf("-1\n");
	exit(0);
}

bool istop(pair<int, int> t) {
	int x = t.X;
	int wh = t.Y;
	if (wh == 1 || niz[x][1] == 0) return true;
	return false;
}

void move(int x, int y) {
	//printf("moving: %d %d\n", x + 1, y + 1);
	int ind1 = 0, ind2 = -1;
	if (niz[x][1] != 0) ind1 = 1;
	if (niz[y][0] == 0) ind2 = 0;
	else if (niz[y][1] == 0) ind2 = 1;
	assert(ind2 >= 0 && niz[x][ind1] != 0);

	sol.push_back({x + 1, y + 1});
	if (ind2 == 0) emps.erase(y);
	if (ind1 == 0) emps.insert(x);
	swap(niz[x][ind1], niz[y][ind2]);
}

void calc_pos() {
	emps.clear();
	for (int i = 0; i < m; i++) {
		for (int j = 0; j < 2; j++) {
			if (niz[i][j] != 0) pos[niz[i][j]].push_back({i, j});
		}
		if (niz[i][0] == 0 && niz[i][1] == 0) emps.insert(i);
	}
}

vector< int > get_path(int x) {
	vector< int > v;
	int ptr = x;
	int las = -1;

	while (true) {
		bio[ptr] = true;
		v.push_back(ptr);
		bool flag = false;
		for (int tren : graph[ptr]) {
			if (tren != las && !bio[tren]) {
				las = ptr;
				ptr = tren;
				flag = true;
				break;
			}
		}
		if (!flag) break;
	}
	return v;
}

int price_chain(vector< int > &v) {
	int cnt = 0;
	for (int i = 0; i < v.size(); i += 2) {
		if (!istop({v[i + 1] / 2, v[i + 1] % 2})) cnt++;
	}
	if (cnt <= 1) return 0;

	cnt = 0;
	reverse(v.begin(), v.end());
	for (int i = 0; i < v.size(); i += 2) {
		if (!istop({v[i + 1] / 2, v[i + 1] % 2})) cnt++;
	} 
	reverse(v.begin(), v.end());
	if (cnt <= 1) return 0;
	return 1;
}

void solve_chain(vector< int > &v, int l, int r) {
	//printf("chain solving\n");
	if (l > r) return;
	for (int i = l; i <= r; i += 2) {
		if (v[i] % 2 == 1 && v[i + 1] % 2 == 1) {
			if (emps.empty()) stop();
			int ac = *emps.begin();

			move(v[i] / 2, ac);
			move(v[i + 1] / 2, ac);
			solve_chain(v, l, i - 1);
			solve_chain(v, i + 2, r);
			return;
		}
	}

	for (int i = l; i <= r; i += 2) {
		if (v[i] % 2 == 0 && v[i + 1] % 2 == 0) {
			for (int j = l; j < i; j += 2) 
				move(v[j + 1] / 2, v[j] / 2);
			for (int j = r; j > i + 1; j -= 2) 
				move(v[j - 1] / 2, v[j] / 2);
			move(v[i] / 2, v[i + 1] / 2);
			return;
		}
	}

	if (v[l + 1] % 2 == 1) {
		for (int i = l; i <= r; i += 2) 
			move(v[i + 1] / 2, v[i] / 2);
	} else {
		for (int i = r; i >= l; i -= 2) 
			move(v[i - 1] / 2, v[i] / 2);
	}
} 

inline int get(int x) {
	return niz[x / 2][x % 2];
}

int price_cycle(vector< int > &v) {
	for (int i = 0; i < v.size(); i++) {
		if (get(v[i]) == get(v[(i + 1) % v.size()]) && v[i] % 2 == 1 && v[i + 1] % 2 == 1) {
			vector< int > ne;
			for (int j = 0; j < v.size() - 2; j++) ne.push_back(v[(i + j + 2) % v.size()]);
			return price_chain(ne) + 1;
		}
	}
	return 1;
}

void solve_cycle(vector< int > &v) {
	//printf("cycle solving\n");
	for (int i = 0; i < v.size(); i++) {
		if (get(v[i]) == get(v[(i + 1) % v.size()]) && v[i] % 2 == 1 && v[i + 1] % 2 == 1) {
			vector< int > ne;
			for (int j = 0; j < v.size() - 2; j++) ne.push_back(v[(i + j + 2) % v.size()]);

			if (emps.empty()) stop();
			int x = *emps.begin();
			move(v[i] / 2, x);
			move(v[(i + 1) % v.size()] / 2, x);
			solve_chain(ne, 0, ne.size() - 1);
			return;
		}
	}

	if (emps.empty()) stop();
	int x = *emps.begin();
	int ind = -1;
	for (int i = 0; i < v.size(); i++) {
		if (v[i] % 2 == 1) ind = i;
	}

	move(v[ind] / 2, x);
	for (int i = 0; i < v.size() - 2; i += 2) {
		move(v[(ind + 2 + i) % v.size()] / 2, v[(ind + 1 + i) % v.size()] / 2);
	}
	move(v[(ind + v.size() - 1) % v.size()] / 2, x);
} 

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 0; i < m; i++)
		for (int j = 0; j < 2; j++)
			scanf("%d", &niz[i][j]);
	
	calc_pos();
	for (int i = 1; i <= n; i++) {
		int ax = pos[i][0].X;
		int ay = pos[i][0].Y;
		int bx = pos[i][1].X;
		int by = pos[i][1].Y;

		graph[2 * ax + ay].push_back(2 * bx + by);
		graph[2 * bx + by].push_back(2 * ax + ay);
	}
	for (int i = 0; i < m; i++) {
		if (niz[i][0] > 0 && niz[i][1] > 0 && niz[i][0] != niz[i][1]) {
			graph[i * 2].push_back(2 * i + 1);
			graph[i * 2 + 1].push_back(2 * i);
		}
	}

	for (int i = 0; i < 2 * m; i++) {
		if (graph[i].size() == 1 && !bio[i]) {
			vector< int > v = get_path(i);
			//printf("chain: ");
			//for (int tren : v) printf("%d ", tren);
			//printf("\n");

			if (v.size() == 2) {
				if (v[0] / 2 == v[1] / 2) continue;
				move(v[0] / 2, v[1] / 2);
				continue;
			}
			chains.push_back(v);
		}
	}

	for (int i = 0; i < 2 * m; i++) {
		if (!bio[i] && graph[i].size() == 2) {
			vector< int > v = get_path(i);
			//printf("cycle: ");
			//for (int tren : v) printf("%d ", tren);
			//printf("\n");

			cycles.push_back(v);
		} 
	}

	vector< vector< int > > tr;
	for (auto &iter : chains) {
		if (price_chain(iter) == 0) solve_chain(iter, 0, iter.size() - 1);
		else tr.push_back(iter);
	}
	for (auto &iter : tr) solve_chain(iter, 0, iter.size() - 1);

	tr.clear();
	for (auto &iter : cycles) {
		if (price_cycle(iter) == 1) solve_cycle(iter);
		else tr.push_back(iter);
	}
	for (auto &iter : tr) solve_cycle(iter);

	printf("%d\n", sol.size());
	for (auto iter : sol) printf("%d %d\n", iter.X, iter.Y);
	return 0;
}

Compilation message

Main.cpp: In function 'int price_chain(std::vector<int>&)':
Main.cpp:86:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |  for (int i = 0; i < v.size(); i += 2) {
      |                  ~~^~~~~~~~~~
Main.cpp:93:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |  for (int i = 0; i < v.size(); i += 2) {
      |                  ~~^~~~~~~~~~
Main.cpp: In function 'int price_cycle(std::vector<int>&)':
Main.cpp:142:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  142 |  for (int i = 0; i < v.size(); i++) {
      |                  ~~^~~~~~~~~~
Main.cpp:145:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  145 |    for (int j = 0; j < v.size() - 2; j++) ne.push_back(v[(i + j + 2) % v.size()]);
      |                    ~~^~~~~~~~~~~~~~
Main.cpp: In function 'void solve_cycle(std::vector<int>&)':
Main.cpp:154:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  154 |  for (int i = 0; i < v.size(); i++) {
      |                  ~~^~~~~~~~~~
Main.cpp:157:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  157 |    for (int j = 0; j < v.size() - 2; j++) ne.push_back(v[(i + j + 2) % v.size()]);
      |                    ~~^~~~~~~~~~~~~~
Main.cpp:171:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  171 |  for (int i = 0; i < v.size(); i++) {
      |                  ~~^~~~~~~~~~
Main.cpp:176:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  176 |  for (int i = 0; i < v.size() - 2; i += 2) {
      |                  ~~^~~~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:246:11: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wformat=]
  246 |  printf("%d\n", sol.size());
      |          ~^     ~~~~~~~~~~
      |           |             |
      |           int           std::vector<std::pair<int, int> >::size_type {aka long unsigned int}
      |          %ld
Main.cpp:183:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  183 |  scanf("%d%d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:186:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  186 |    scanf("%d", &niz[i][j]);
      |    ~~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14292 KB Output is correct
2 Correct 7 ms 14292 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 7 ms 14292 KB Output is correct
5 Correct 9 ms 14292 KB Output is correct
6 Correct 7 ms 14292 KB Output is correct
7 Correct 7 ms 14404 KB Output is correct
8 Correct 7 ms 14292 KB Output is correct
9 Correct 7 ms 14408 KB Output is correct
10 Correct 7 ms 14404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 137 ms 31932 KB Output is correct
2 Correct 169 ms 33624 KB Output is correct
3 Correct 109 ms 29976 KB Output is correct
4 Correct 104 ms 29568 KB Output is correct
5 Correct 147 ms 33780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 9 ms 14436 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 7 ms 14548 KB Output is correct
5 Correct 9 ms 14408 KB Output is correct
6 Correct 10 ms 14548 KB Output is correct
7 Correct 7 ms 14548 KB Output is correct
8 Correct 7 ms 14540 KB Output is correct
9 Correct 8 ms 14532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 9 ms 14436 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 7 ms 14548 KB Output is correct
5 Correct 9 ms 14408 KB Output is correct
6 Correct 10 ms 14548 KB Output is correct
7 Correct 7 ms 14548 KB Output is correct
8 Correct 7 ms 14540 KB Output is correct
9 Correct 8 ms 14532 KB Output is correct
10 Correct 255 ms 46764 KB Output is correct
11 Correct 141 ms 36876 KB Output is correct
12 Correct 205 ms 38440 KB Output is correct
13 Correct 245 ms 42860 KB Output is correct
14 Correct 218 ms 40320 KB Output is correct
15 Correct 213 ms 39816 KB Output is correct
16 Correct 251 ms 46012 KB Output is correct
17 Correct 216 ms 39568 KB Output is correct
18 Correct 281 ms 44560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 14524 KB Output is correct
2 Correct 7 ms 14532 KB Output is correct
3 Correct 7 ms 14504 KB Output is correct
4 Correct 7 ms 14420 KB Output is correct
5 Correct 7 ms 14408 KB Output is correct
6 Correct 8 ms 14548 KB Output is correct
7 Correct 7 ms 14416 KB Output is correct
8 Correct 7 ms 14548 KB Output is correct
9 Correct 7 ms 14548 KB Output is correct
10 Correct 8 ms 14412 KB Output is correct
11 Correct 7 ms 14548 KB Output is correct
12 Correct 7 ms 14536 KB Output is correct
13 Correct 7 ms 14420 KB Output is correct
14 Correct 7 ms 14412 KB Output is correct
15 Correct 8 ms 14420 KB Output is correct
16 Correct 7 ms 14548 KB Output is correct
17 Correct 7 ms 14548 KB Output is correct
18 Correct 7 ms 14548 KB Output is correct
19 Correct 9 ms 14548 KB Output is correct
20 Correct 8 ms 14556 KB Output is correct
21 Correct 7 ms 14420 KB Output is correct
22 Correct 7 ms 14548 KB Output is correct
23 Correct 9 ms 14544 KB Output is correct
24 Correct 8 ms 14548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 14292 KB Output is correct
2 Correct 7 ms 14292 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 7 ms 14292 KB Output is correct
5 Correct 9 ms 14292 KB Output is correct
6 Correct 7 ms 14292 KB Output is correct
7 Correct 7 ms 14404 KB Output is correct
8 Correct 7 ms 14292 KB Output is correct
9 Correct 7 ms 14408 KB Output is correct
10 Correct 7 ms 14404 KB Output is correct
11 Correct 137 ms 31932 KB Output is correct
12 Correct 169 ms 33624 KB Output is correct
13 Correct 109 ms 29976 KB Output is correct
14 Correct 104 ms 29568 KB Output is correct
15 Correct 147 ms 33780 KB Output is correct
16 Correct 8 ms 14420 KB Output is correct
17 Correct 9 ms 14436 KB Output is correct
18 Correct 8 ms 14420 KB Output is correct
19 Correct 7 ms 14548 KB Output is correct
20 Correct 9 ms 14408 KB Output is correct
21 Correct 10 ms 14548 KB Output is correct
22 Correct 7 ms 14548 KB Output is correct
23 Correct 7 ms 14540 KB Output is correct
24 Correct 8 ms 14532 KB Output is correct
25 Correct 255 ms 46764 KB Output is correct
26 Correct 141 ms 36876 KB Output is correct
27 Correct 205 ms 38440 KB Output is correct
28 Correct 245 ms 42860 KB Output is correct
29 Correct 218 ms 40320 KB Output is correct
30 Correct 213 ms 39816 KB Output is correct
31 Correct 251 ms 46012 KB Output is correct
32 Correct 216 ms 39568 KB Output is correct
33 Correct 281 ms 44560 KB Output is correct
34 Correct 8 ms 14524 KB Output is correct
35 Correct 7 ms 14532 KB Output is correct
36 Correct 7 ms 14504 KB Output is correct
37 Correct 7 ms 14420 KB Output is correct
38 Correct 7 ms 14408 KB Output is correct
39 Correct 8 ms 14548 KB Output is correct
40 Correct 7 ms 14416 KB Output is correct
41 Correct 7 ms 14548 KB Output is correct
42 Correct 7 ms 14548 KB Output is correct
43 Correct 8 ms 14412 KB Output is correct
44 Correct 7 ms 14548 KB Output is correct
45 Correct 7 ms 14536 KB Output is correct
46 Correct 7 ms 14420 KB Output is correct
47 Correct 7 ms 14412 KB Output is correct
48 Correct 8 ms 14420 KB Output is correct
49 Correct 7 ms 14548 KB Output is correct
50 Correct 7 ms 14548 KB Output is correct
51 Correct 7 ms 14548 KB Output is correct
52 Correct 9 ms 14548 KB Output is correct
53 Correct 8 ms 14556 KB Output is correct
54 Correct 7 ms 14420 KB Output is correct
55 Correct 7 ms 14548 KB Output is correct
56 Correct 9 ms 14544 KB Output is correct
57 Correct 8 ms 14548 KB Output is correct
58 Correct 247 ms 43932 KB Output is correct
59 Correct 243 ms 45144 KB Output is correct
60 Correct 221 ms 41504 KB Output is correct
61 Correct 262 ms 43324 KB Output is correct
62 Correct 144 ms 37568 KB Output is correct
63 Correct 234 ms 43744 KB Output is correct
64 Correct 207 ms 39020 KB Output is correct
65 Correct 242 ms 44552 KB Output is correct
66 Incorrect 211 ms 39244 KB Output isn't correct
67 Halted 0 ms 0 KB -