Submission #760587

# Submission time Handle Problem Language Result Execution time Memory
760587 2023-06-17T21:53:25 Z kingfran1907 Parking (CEOI22_parking) C++14
100 / 100
308 ms 102284 KB
#include <bits/stdc++.h>
#define X first
#define Y second
 
using namespace std;
typedef long long llint;
 
const int maxn = 1e6+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 (v[i] % 2 == 1 && v[i + 1] % 2 == 1) return 1;
	}
	return 0;
}
 
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:85:6: warning: unused variable 'cnt' [-Wunused-variable]
   85 |  int cnt = 0;
      |      ^~~
Main.cpp: In function 'int price_cycle(std::vector<int>&)':
Main.cpp:133:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  133 |  for (int i = 0; i < v.size(); i++) {
      |                  ~~^~~~~~~~~~
Main.cpp:136:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  136 |    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:145:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  145 |  for (int i = 0; i < v.size(); i++) {
      |                  ~~^~~~~~~~~~
Main.cpp:148:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  148 |    for (int j = 0; j < v.size() - 2; j++) ne.push_back(v[(i + j + 2) % v.size()]);
      |                    ~~^~~~~~~~~~~~~~
Main.cpp:162:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  162 |  for (int i = 0; i < v.size(); i++) {
      |                  ~~^~~~~~~~~~
Main.cpp:167:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  167 |  for (int i = 0; i < v.size() - 2; i += 2) {
      |                  ~~^~~~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:237: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=]
  237 |  printf("%d\n", sol.size());
      |          ~^     ~~~~~~~~~~
      |           |             |
      |           int           std::vector<std::pair<int, int> >::size_type {aka long unsigned int}
      |          %ld
Main.cpp:174:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  174 |  scanf("%d%d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:177:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  177 |    scanf("%d", &niz[i][j]);
      |    ~~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 35 ms 70732 KB Output is correct
2 Correct 30 ms 70772 KB Output is correct
3 Correct 30 ms 70756 KB Output is correct
4 Correct 30 ms 70740 KB Output is correct
5 Correct 29 ms 70732 KB Output is correct
6 Correct 34 ms 70752 KB Output is correct
7 Correct 30 ms 70732 KB Output is correct
8 Correct 32 ms 70668 KB Output is correct
9 Correct 35 ms 70660 KB Output is correct
10 Correct 31 ms 70740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 170 ms 88360 KB Output is correct
2 Correct 177 ms 90172 KB Output is correct
3 Correct 144 ms 86536 KB Output is correct
4 Correct 126 ms 86012 KB Output is correct
5 Correct 173 ms 90360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 70740 KB Output is correct
2 Correct 32 ms 70824 KB Output is correct
3 Correct 31 ms 70840 KB Output is correct
4 Correct 31 ms 70868 KB Output is correct
5 Correct 32 ms 70884 KB Output is correct
6 Correct 31 ms 70868 KB Output is correct
7 Correct 32 ms 70868 KB Output is correct
8 Correct 32 ms 70772 KB Output is correct
9 Correct 32 ms 70860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 70740 KB Output is correct
2 Correct 32 ms 70824 KB Output is correct
3 Correct 31 ms 70840 KB Output is correct
4 Correct 31 ms 70868 KB Output is correct
5 Correct 32 ms 70884 KB Output is correct
6 Correct 31 ms 70868 KB Output is correct
7 Correct 32 ms 70868 KB Output is correct
8 Correct 32 ms 70772 KB Output is correct
9 Correct 32 ms 70860 KB Output is correct
10 Correct 284 ms 100484 KB Output is correct
11 Correct 172 ms 90932 KB Output is correct
12 Correct 229 ms 92420 KB Output is correct
13 Correct 255 ms 96764 KB Output is correct
14 Correct 223 ms 94132 KB Output is correct
15 Correct 213 ms 93752 KB Output is correct
16 Correct 305 ms 99844 KB Output is correct
17 Correct 290 ms 93572 KB Output is correct
18 Correct 259 ms 98412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 70868 KB Output is correct
2 Correct 32 ms 70868 KB Output is correct
3 Correct 33 ms 70888 KB Output is correct
4 Correct 31 ms 70772 KB Output is correct
5 Correct 32 ms 70816 KB Output is correct
6 Correct 32 ms 70800 KB Output is correct
7 Correct 36 ms 70868 KB Output is correct
8 Correct 33 ms 70892 KB Output is correct
9 Correct 32 ms 70900 KB Output is correct
10 Correct 32 ms 70872 KB Output is correct
11 Correct 32 ms 70792 KB Output is correct
12 Correct 32 ms 70868 KB Output is correct
13 Correct 31 ms 70864 KB Output is correct
14 Correct 37 ms 70840 KB Output is correct
15 Correct 32 ms 70868 KB Output is correct
16 Correct 32 ms 70808 KB Output is correct
17 Correct 32 ms 70868 KB Output is correct
18 Correct 33 ms 70904 KB Output is correct
19 Correct 32 ms 70816 KB Output is correct
20 Correct 34 ms 70904 KB Output is correct
21 Correct 32 ms 70860 KB Output is correct
22 Correct 33 ms 70860 KB Output is correct
23 Correct 37 ms 70868 KB Output is correct
24 Correct 34 ms 70868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 70732 KB Output is correct
2 Correct 30 ms 70772 KB Output is correct
3 Correct 30 ms 70756 KB Output is correct
4 Correct 30 ms 70740 KB Output is correct
5 Correct 29 ms 70732 KB Output is correct
6 Correct 34 ms 70752 KB Output is correct
7 Correct 30 ms 70732 KB Output is correct
8 Correct 32 ms 70668 KB Output is correct
9 Correct 35 ms 70660 KB Output is correct
10 Correct 31 ms 70740 KB Output is correct
11 Correct 170 ms 88360 KB Output is correct
12 Correct 177 ms 90172 KB Output is correct
13 Correct 144 ms 86536 KB Output is correct
14 Correct 126 ms 86012 KB Output is correct
15 Correct 173 ms 90360 KB Output is correct
16 Correct 31 ms 70740 KB Output is correct
17 Correct 32 ms 70824 KB Output is correct
18 Correct 31 ms 70840 KB Output is correct
19 Correct 31 ms 70868 KB Output is correct
20 Correct 32 ms 70884 KB Output is correct
21 Correct 31 ms 70868 KB Output is correct
22 Correct 32 ms 70868 KB Output is correct
23 Correct 32 ms 70772 KB Output is correct
24 Correct 32 ms 70860 KB Output is correct
25 Correct 284 ms 100484 KB Output is correct
26 Correct 172 ms 90932 KB Output is correct
27 Correct 229 ms 92420 KB Output is correct
28 Correct 255 ms 96764 KB Output is correct
29 Correct 223 ms 94132 KB Output is correct
30 Correct 213 ms 93752 KB Output is correct
31 Correct 305 ms 99844 KB Output is correct
32 Correct 290 ms 93572 KB Output is correct
33 Correct 259 ms 98412 KB Output is correct
34 Correct 32 ms 70868 KB Output is correct
35 Correct 32 ms 70868 KB Output is correct
36 Correct 33 ms 70888 KB Output is correct
37 Correct 31 ms 70772 KB Output is correct
38 Correct 32 ms 70816 KB Output is correct
39 Correct 32 ms 70800 KB Output is correct
40 Correct 36 ms 70868 KB Output is correct
41 Correct 33 ms 70892 KB Output is correct
42 Correct 32 ms 70900 KB Output is correct
43 Correct 32 ms 70872 KB Output is correct
44 Correct 32 ms 70792 KB Output is correct
45 Correct 32 ms 70868 KB Output is correct
46 Correct 31 ms 70864 KB Output is correct
47 Correct 37 ms 70840 KB Output is correct
48 Correct 32 ms 70868 KB Output is correct
49 Correct 32 ms 70808 KB Output is correct
50 Correct 32 ms 70868 KB Output is correct
51 Correct 33 ms 70904 KB Output is correct
52 Correct 32 ms 70816 KB Output is correct
53 Correct 34 ms 70904 KB Output is correct
54 Correct 32 ms 70860 KB Output is correct
55 Correct 33 ms 70860 KB Output is correct
56 Correct 37 ms 70868 KB Output is correct
57 Correct 34 ms 70868 KB Output is correct
58 Correct 248 ms 97968 KB Output is correct
59 Correct 272 ms 98972 KB Output is correct
60 Correct 256 ms 96104 KB Output is correct
61 Correct 308 ms 97432 KB Output is correct
62 Correct 179 ms 91460 KB Output is correct
63 Correct 253 ms 96128 KB Output is correct
64 Correct 221 ms 94464 KB Output is correct
65 Correct 278 ms 98516 KB Output is correct
66 Correct 294 ms 99644 KB Output is correct
67 Correct 235 ms 94392 KB Output is correct
68 Correct 261 ms 99684 KB Output is correct
69 Correct 248 ms 98616 KB Output is correct
70 Correct 215 ms 95188 KB Output is correct
71 Correct 219 ms 96824 KB Output is correct
72 Correct 215 ms 96572 KB Output is correct
73 Correct 308 ms 102284 KB Output is correct
74 Correct 213 ms 96192 KB Output is correct
75 Correct 302 ms 101304 KB Output is correct
76 Correct 279 ms 101980 KB Output is correct
77 Correct 254 ms 100972 KB Output is correct
78 Correct 214 ms 95848 KB Output is correct
79 Correct 256 ms 100072 KB Output is correct
80 Correct 240 ms 96200 KB Output is correct
81 Correct 296 ms 100308 KB Output is correct