Submission #527969

# Submission time Handle Problem Language Result Execution time Memory
527969 2022-02-18T21:17:34 Z LucaDantas Alternating Current (BOI18_alternating) C++17
32 / 100
42 ms 5592 KB
#include <bits/stdc++.h>
using namespace std;

constexpr int maxn = 3e5+10;

int pref[maxn];
int mx = 0, ini_mx = 0, n, m;

struct Wire {
	int l, r, id;
	int sz() const { return r >= l ? r - l + 1 : r + (n - l + 1); }
	bool operator<(const Wire& o) const {
		if(l == o.l) return this->sz() > o.sz();
		if((l >= ini_mx) == (o.l >= ini_mx)) return l < o.l; // retorno o que tem o menor l já que eles são da mesma metade
		return l > o.l; // já que eles são de metades diferentes eu retorno o que tiver na metade maior
	}
} wr[maxn];

int pai[maxn];

int main() {
	scanf("%d %d", &n, &m);
	for(int i = 0; i < m; i++) {
		scanf("%d %d", &wr[i].l, &wr[i].r); wr[i].id = i;
		if(wr[i].l <= wr[i].r) {
			pref[wr[i].l]++;
			pref[wr[i].r+1]--;
		} else {
			pref[wr[i].l]++;
			pref[1]++;
			pref[wr[i].r+1]--;
		}
		
		if(wr[i].sz() > mx) {
			mx = wr[i].sz();
			ini_mx = wr[i].l;
		}
	}

	for(int i = 1; i <= n; i++) {
		pref[i] += pref[i-1];
		pref[i+n] = pref[i];
		if(pref[i] < 2) return puts("impossible"), 0;
	}

	sort(wr, wr+m);

	int virou = 0;
	int fim = 0, aberto = -1;

	vector<Wire> brabos;

	memset(pai, -1, sizeof pai);
	for(int i = 0; i < m; i++) {
		auto [l, r, id] = wr[i];
		if(make_pair(virou, fim) < make_pair((l < ini_mx) + (r < l), r)) {
			brabos.push_back(wr[i]);
			aberto = id;
			fim = r;
			virou |= (l < ini_mx) + (r < l);
		} else pai[id] = aberto;
	}

	/* puts("BRABOS: ");
	printf("Size: %ld\n", brabos.size());
	for(Wire x : brabos)
		printf("(%d %d %d) ", x.l, x.r, x.id);
	puts(""); */

	vector<int> ans(m);

	auto print = [&]() {
		for(int i = 0; i < m; i++)
			printf("%d", ans[i]);
		puts("");
		exit(0);
	};

	if(brabos[0].sz() == n) {
		ans[brabos[0].id] = 1;
		print();
	}

	if(brabos.size() % 2 == 0) {
		for(int i = 0; i < brabos.size(); i++)
			ans[brabos[i].id] = i&1;
		
		for(int i = 0; i < m; i++)
			if(pai[i] != -1) ans[i] = 1^ans[pai[i]];

		print();
	}

	sort(brabos.begin(), brabos.end(), [](const Wire& a, const Wire& b) { return a.l < b.l; });

	for(int i = 0; i < brabos.size(); i++) {
		auto nxt = [&](int x) { return (x+1) % brabos.size(); };
		auto eu = brabos[i], prox = brabos[nxt(i)];
		int l = prox.l, r = eu.r;
		
		if(r < l) r += n;

		bool tem = 1;
		for(int j = l; j <= r; j++)
			tem &= pref[j] >= 3;

		auto no_intersection = [](Wire a, Wire b) {
			if(a.r < a.l) a.r += n;
			if(b.r < b.l) b.r += n;

			if(a.r < b.l) return 1;
			return 0;
		};
		if(tem || no_intersection(eu, prox)) {
			// printf("BOM %d | %d %d\n", i, l, r);

			bool opa = 1;
			ans[ brabos[i].id ] = opa;

			for(int j = nxt(i); j != i; j = nxt(j)) {
				ans[ brabos[j].id ] = opa;
				opa ^= 1;
			}

			for(int i = 0; i < m; i++)
				if(pai[i] != -1) ans[i] = 1^ans[pai[i]];

			print();
		}
	}

	puts("impossible");
}

Compilation message

alternating.cpp: In function 'int main()':
alternating.cpp:85:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Wire>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |   for(int i = 0; i < brabos.size(); i++)
      |                  ~~^~~~~~~~~~~~~~~
alternating.cpp:96:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Wire>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |  for(int i = 0; i < brabos.size(); i++) {
      |                 ~~^~~~~~~~~~~~~~~
alternating.cpp:22:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |  scanf("%d %d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~~
alternating.cpp:24:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |   scanf("%d %d", &wr[i].l, &wr[i].r); wr[i].id = i;
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1456 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 1 ms 1360 KB Output is correct
4 Correct 1 ms 1496 KB Output is correct
5 Correct 1 ms 1456 KB Output is correct
6 Correct 1 ms 1460 KB Output is correct
7 Correct 1 ms 1356 KB Output is correct
8 Correct 1 ms 300 KB Output is correct
9 Correct 1 ms 1484 KB Output is correct
10 Correct 1 ms 296 KB Output is correct
11 Correct 1 ms 296 KB Output is correct
12 Correct 2 ms 1356 KB Output is correct
13 Correct 1 ms 300 KB Output is correct
14 Correct 1 ms 1488 KB Output is correct
15 Correct 1 ms 1460 KB Output is correct
16 Correct 1 ms 1480 KB Output is correct
17 Correct 1 ms 1456 KB Output is correct
18 Correct 1 ms 1428 KB Output is correct
19 Correct 1 ms 1488 KB Output is correct
20 Correct 1 ms 1456 KB Output is correct
21 Correct 1 ms 1584 KB Output is correct
22 Correct 1 ms 1488 KB Output is correct
23 Correct 1 ms 1464 KB Output is correct
24 Correct 2 ms 1360 KB Output is correct
25 Correct 1 ms 1452 KB Output is correct
26 Correct 1 ms 1488 KB Output is correct
27 Correct 1 ms 208 KB Output is correct
28 Correct 2 ms 1452 KB Output is correct
29 Correct 1 ms 208 KB Output is correct
30 Correct 1 ms 208 KB Output is correct
31 Correct 1 ms 1488 KB Output is correct
32 Correct 1 ms 304 KB Output is correct
33 Correct 1 ms 1488 KB Output is correct
34 Correct 0 ms 208 KB Output is correct
35 Correct 1 ms 208 KB Output is correct
36 Correct 1 ms 1408 KB Output is correct
37 Correct 1 ms 1488 KB Output is correct
38 Correct 1 ms 208 KB Output is correct
39 Correct 1 ms 1360 KB Output is correct
40 Correct 1 ms 1488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1456 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 1 ms 1360 KB Output is correct
4 Correct 1 ms 1496 KB Output is correct
5 Correct 1 ms 1456 KB Output is correct
6 Correct 1 ms 1460 KB Output is correct
7 Correct 1 ms 1356 KB Output is correct
8 Correct 1 ms 300 KB Output is correct
9 Correct 1 ms 1484 KB Output is correct
10 Correct 1 ms 296 KB Output is correct
11 Correct 1 ms 296 KB Output is correct
12 Correct 2 ms 1356 KB Output is correct
13 Correct 1 ms 300 KB Output is correct
14 Correct 1 ms 1488 KB Output is correct
15 Correct 1 ms 1460 KB Output is correct
16 Correct 1 ms 1480 KB Output is correct
17 Correct 1 ms 1456 KB Output is correct
18 Correct 1 ms 1428 KB Output is correct
19 Correct 1 ms 1488 KB Output is correct
20 Correct 1 ms 1456 KB Output is correct
21 Correct 1 ms 1584 KB Output is correct
22 Correct 1 ms 1488 KB Output is correct
23 Correct 1 ms 1464 KB Output is correct
24 Correct 2 ms 1360 KB Output is correct
25 Correct 1 ms 1452 KB Output is correct
26 Correct 1 ms 1488 KB Output is correct
27 Correct 1 ms 208 KB Output is correct
28 Correct 2 ms 1452 KB Output is correct
29 Correct 1 ms 208 KB Output is correct
30 Correct 1 ms 208 KB Output is correct
31 Correct 1 ms 1488 KB Output is correct
32 Correct 1 ms 304 KB Output is correct
33 Correct 1 ms 1488 KB Output is correct
34 Correct 0 ms 208 KB Output is correct
35 Correct 1 ms 208 KB Output is correct
36 Correct 1 ms 1408 KB Output is correct
37 Correct 1 ms 1488 KB Output is correct
38 Correct 1 ms 208 KB Output is correct
39 Correct 1 ms 1360 KB Output is correct
40 Correct 1 ms 1488 KB Output is correct
41 Correct 2 ms 1488 KB Output is correct
42 Correct 2 ms 1360 KB Output is correct
43 Correct 2 ms 1488 KB Output is correct
44 Incorrect 2 ms 1452 KB no wires in direction 1 between segments 4 and 22
45 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1456 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 1 ms 1360 KB Output is correct
4 Correct 1 ms 1496 KB Output is correct
5 Correct 1 ms 1456 KB Output is correct
6 Correct 1 ms 1460 KB Output is correct
7 Correct 1 ms 1356 KB Output is correct
8 Correct 1 ms 300 KB Output is correct
9 Correct 1 ms 1484 KB Output is correct
10 Correct 1 ms 296 KB Output is correct
11 Correct 1 ms 296 KB Output is correct
12 Correct 2 ms 1356 KB Output is correct
13 Correct 1 ms 300 KB Output is correct
14 Correct 1 ms 1488 KB Output is correct
15 Correct 1 ms 1460 KB Output is correct
16 Correct 1 ms 1480 KB Output is correct
17 Correct 1 ms 1456 KB Output is correct
18 Correct 1 ms 1428 KB Output is correct
19 Correct 1 ms 1488 KB Output is correct
20 Correct 1 ms 1456 KB Output is correct
21 Correct 1 ms 1584 KB Output is correct
22 Correct 1 ms 1488 KB Output is correct
23 Correct 1 ms 1464 KB Output is correct
24 Correct 2 ms 1360 KB Output is correct
25 Correct 1 ms 1452 KB Output is correct
26 Correct 1 ms 1488 KB Output is correct
27 Correct 1 ms 208 KB Output is correct
28 Correct 2 ms 1452 KB Output is correct
29 Correct 1 ms 208 KB Output is correct
30 Correct 1 ms 208 KB Output is correct
31 Correct 1 ms 1488 KB Output is correct
32 Correct 1 ms 304 KB Output is correct
33 Correct 1 ms 1488 KB Output is correct
34 Correct 0 ms 208 KB Output is correct
35 Correct 1 ms 208 KB Output is correct
36 Correct 1 ms 1408 KB Output is correct
37 Correct 1 ms 1488 KB Output is correct
38 Correct 1 ms 208 KB Output is correct
39 Correct 1 ms 1360 KB Output is correct
40 Correct 1 ms 1488 KB Output is correct
41 Correct 2 ms 1488 KB Output is correct
42 Correct 2 ms 1360 KB Output is correct
43 Correct 2 ms 1488 KB Output is correct
44 Incorrect 2 ms 1452 KB no wires in direction 1 between segments 4 and 22
45 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 42 ms 4784 KB Output is correct
2 Correct 2 ms 2252 KB Output is correct
3 Correct 11 ms 1836 KB Output is correct
4 Correct 16 ms 3600 KB Output is correct
5 Correct 38 ms 5372 KB Output is correct
6 Correct 20 ms 3108 KB Output is correct
7 Correct 35 ms 4972 KB Output is correct
8 Correct 4 ms 2252 KB Output is correct
9 Correct 2 ms 2124 KB Output is correct
10 Correct 42 ms 5592 KB Output is correct
11 Correct 29 ms 4704 KB Output is correct
12 Correct 34 ms 4656 KB Output is correct
13 Correct 1 ms 1064 KB Output is correct
14 Correct 1 ms 1068 KB Output is correct
15 Correct 37 ms 4968 KB Output is correct
16 Correct 10 ms 2252 KB Output is correct
17 Correct 38 ms 5020 KB Output is correct
18 Correct 35 ms 4060 KB Output is correct
19 Correct 2 ms 708 KB Output is correct
20 Correct 38 ms 5308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1456 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 1 ms 1360 KB Output is correct
4 Correct 1 ms 1496 KB Output is correct
5 Correct 1 ms 1456 KB Output is correct
6 Correct 1 ms 1460 KB Output is correct
7 Correct 1 ms 1356 KB Output is correct
8 Correct 1 ms 300 KB Output is correct
9 Correct 1 ms 1484 KB Output is correct
10 Correct 1 ms 296 KB Output is correct
11 Correct 1 ms 296 KB Output is correct
12 Correct 2 ms 1356 KB Output is correct
13 Correct 1 ms 300 KB Output is correct
14 Correct 1 ms 1488 KB Output is correct
15 Correct 1 ms 1460 KB Output is correct
16 Correct 1 ms 1480 KB Output is correct
17 Correct 1 ms 1456 KB Output is correct
18 Correct 1 ms 1428 KB Output is correct
19 Correct 1 ms 1488 KB Output is correct
20 Correct 1 ms 1456 KB Output is correct
21 Correct 1 ms 1584 KB Output is correct
22 Correct 1 ms 1488 KB Output is correct
23 Correct 1 ms 1464 KB Output is correct
24 Correct 2 ms 1360 KB Output is correct
25 Correct 1 ms 1452 KB Output is correct
26 Correct 1 ms 1488 KB Output is correct
27 Correct 1 ms 208 KB Output is correct
28 Correct 2 ms 1452 KB Output is correct
29 Correct 1 ms 208 KB Output is correct
30 Correct 1 ms 208 KB Output is correct
31 Correct 1 ms 1488 KB Output is correct
32 Correct 1 ms 304 KB Output is correct
33 Correct 1 ms 1488 KB Output is correct
34 Correct 0 ms 208 KB Output is correct
35 Correct 1 ms 208 KB Output is correct
36 Correct 1 ms 1408 KB Output is correct
37 Correct 1 ms 1488 KB Output is correct
38 Correct 1 ms 208 KB Output is correct
39 Correct 1 ms 1360 KB Output is correct
40 Correct 1 ms 1488 KB Output is correct
41 Correct 2 ms 1488 KB Output is correct
42 Correct 2 ms 1360 KB Output is correct
43 Correct 2 ms 1488 KB Output is correct
44 Incorrect 2 ms 1452 KB no wires in direction 1 between segments 4 and 22
45 Halted 0 ms 0 KB -