답안 #111287

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
111287 2019-05-14T15:31:32 Z atoiz Alternating Current (BOI18_alternating) C++14
52 / 100
57 ms 5104 KB
/*input
5 4
1 3
2 4
4 5
5 1
*/

#include <iostream>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cassert>
#include <algorithm>
#include <cstdlib>
#include <numeric>
#include <climits>
#include <fstream>

using namespace std;

#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORA(i, a) for (auto &i : a)
#define FORB(i, a, b) for (int i = a; i >= b; --i)
#define SZ(a) ((int) a.size())
#define ALL(a) begin(a), end(a)

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
#define fi first
#define se second

// start of code


struct Seg
{
	int l, r, i;
	bool operator< (const Seg &s) const
	{
		return (l == s.l ? r > s.r : l < s.l);
	}
};

const int MAXN = 200007;
int N, M;
int cover[MAXN], par[MAXN], newIdx[MAXN], cnt, tmp, ptr;
vector<Seg> segs, segs1;
bool color[MAXN];

void addCover(Seg s, int _N = N)
{
	assert(s.l <= _N);
	if (s.r <= _N) {
		++ cover[s.l];
		-- cover[s.r + 1];
	} else {
		++ cover[s.l];
		-- cover[_N + 1];
		++ cover[1];
		-- cover[s.r - _N + 1];
	}
}

bool isConsec(Seg sl, Seg sr, int _N)
{
	if (sl.l >= sr.l) sr.l += _N, sr.r += _N;
	return sl.r + 1 >= sr.l;
}

signed main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	// read
	cin >> N >> M;
	segs.resize(M);
	for (int i = 0; i < M; ++i) {
		cin >> segs[i].l >> segs[i].r;
		if (segs[i].l > segs[i].r) segs[i].r += N;
		segs[i].i = i;
		par[i] = -1;
	}

	// re-organize
	ptr = 0;
	for (int i = 1; i < M; ++i) {
		if (segs[i].r - segs[i].l > segs[ptr].r - segs[ptr].l) {
			ptr = i;
		}
	}

	int shiftBack = segs[ptr].l - 1;
	for (int i = 0; i < M; ++i) {
		segs[i].l -= shiftBack, segs[i].r -= shiftBack;
		if (segs[i].l <= 0) segs[i].l += N, segs[i].r += N;
	}

	// sort
	sort(segs.begin(), segs.end());

	// cout << "-1-" << endl;
	// for (Seg &s : segs) cout << s.l << ' ' << s.r << ": " << s.i << endl;
	// cout << "--" << endl;

	// remove interiors
	for (int i = 1; i <= N; ++i) cover[i] = 0;
	int ptr = 0; segs1.push_back(segs[0]);
	for (int i = 1; i < M; ++i) {
		if (segs[i].r <= segs[ptr].r) {
			par[segs[i].i] = segs[ptr].i;
			addCover(segs[i]);
		} else {
			ptr = i;
			segs1.push_back(segs[i]);
		}
	}
	segs1.swap(segs); segs1.clear();

	// calculate
	for (int i = 1; i <= N; ++i) {
		cover[i] += cover[i - 1];

		newIdx[i] = newIdx[i - 1];
		if (!cover[i]) ++newIdx[i];
	}
	int N2 = newIdx[N];
	for (int i = 1; i <= N; ++i) {
		newIdx[i + N] = newIdx[i] + N2;
	}

	// cout << "-2-" << endl;
	// for (Seg &s : segs) cout << s.l << ' ' << s.r << ": " << s.i << endl;
	// cout << "--" << endl;

	// for (int i = 1; i <= N; ++i) cout << cover[i]; cout << endl;

	for (Seg &s : segs) {
		s.l = newIdx[s.l - 1] + 1;
		s.r = newIdx[s.r];
		if (s.l > N2) s.l -= N2, s.r -= N2;
		if (s.l <= s.r) segs1.push_back(s);
	}

	// cout << "-3-" << endl;
	// for (Seg &s : segs1) cout << s.l << ' ' << s.r << ": " << s.i << endl;
	// cout << "--" << endl;

	for (int i = 1; i <= N; ++i) cover[i] = 0;
	for (Seg &s : segs1) addCover(s, N2);
	for (int i = 1; i <= N2; ++i) cover[i] += cover[i - 1];

	// check
	for (int i = 1; i <= N2; ++i) {
		if (cover[i] <= 1) return cout << "impossible\n", 0;
	}

	if (segs1.size() & 1) {
		if (segs1.size() == 1) return cout << "impossible\n", 0;

		bool flag = 0;
		for (int i = 0; i < (int) segs1.size() && !flag; ++i) {
			int il = i - 2; if (il < 0) il += segs1.size();
			int ir = i + 1; if (ir >= (int) segs1.size()) ir -= segs1.size();

			if (isConsec(segs[il], segs[ir], N2)) {
				rotate(segs1.begin(), segs1.begin() + i, segs1.end());
				flag = 1;

				// cout << "-4-" << ' ' << i << endl;
				// for (Seg &s : segs1) cout << s.l << ' ' << s.r << ": " << s.i << endl;
				// cout << "--" << endl;
			}
		}

		if (!flag) return cout << "impossible\n", 0;
	}

	for (int i = 0; i < (int) segs1.size(); ++i) {
		color[segs1[i].i] = i & 1;
	}

	for (int i = 0; i < M; ++i) {
		if (par[i] >= 0) {
			color[i] = !color[par[i]];
		}
		cout << color[i];
	}
	cout << endl;

	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 3 ms 384 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 3 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 3 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 3 ms 384 KB Output is correct
25 Correct 2 ms 384 KB Output is correct
26 Correct 2 ms 384 KB Output is correct
27 Correct 2 ms 384 KB Output is correct
28 Correct 2 ms 512 KB Output is correct
29 Correct 3 ms 384 KB Output is correct
30 Correct 3 ms 384 KB Output is correct
31 Correct 2 ms 384 KB Output is correct
32 Correct 2 ms 384 KB Output is correct
33 Correct 3 ms 384 KB Output is correct
34 Correct 3 ms 384 KB Output is correct
35 Correct 2 ms 384 KB Output is correct
36 Correct 2 ms 384 KB Output is correct
37 Correct 3 ms 384 KB Output is correct
38 Correct 2 ms 384 KB Output is correct
39 Correct 2 ms 384 KB Output is correct
40 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 3 ms 384 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 3 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 3 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 3 ms 384 KB Output is correct
25 Correct 2 ms 384 KB Output is correct
26 Correct 2 ms 384 KB Output is correct
27 Correct 2 ms 384 KB Output is correct
28 Correct 2 ms 512 KB Output is correct
29 Correct 3 ms 384 KB Output is correct
30 Correct 3 ms 384 KB Output is correct
31 Correct 2 ms 384 KB Output is correct
32 Correct 2 ms 384 KB Output is correct
33 Correct 3 ms 384 KB Output is correct
34 Correct 3 ms 384 KB Output is correct
35 Correct 2 ms 384 KB Output is correct
36 Correct 2 ms 384 KB Output is correct
37 Correct 3 ms 384 KB Output is correct
38 Correct 2 ms 384 KB Output is correct
39 Correct 2 ms 384 KB Output is correct
40 Correct 2 ms 384 KB Output is correct
41 Correct 2 ms 384 KB Output is correct
42 Correct 2 ms 384 KB Output is correct
43 Correct 2 ms 384 KB Output is correct
44 Correct 2 ms 392 KB Output is correct
45 Correct 2 ms 384 KB Output is correct
46 Correct 2 ms 384 KB Output is correct
47 Correct 2 ms 384 KB Output is correct
48 Correct 2 ms 384 KB Output is correct
49 Correct 3 ms 412 KB Output is correct
50 Correct 2 ms 384 KB Output is correct
51 Correct 2 ms 384 KB Output is correct
52 Correct 2 ms 384 KB Output is correct
53 Correct 3 ms 384 KB Output is correct
54 Correct 2 ms 384 KB Output is correct
55 Correct 3 ms 384 KB Output is correct
56 Correct 3 ms 384 KB Output is correct
57 Correct 3 ms 384 KB Output is correct
58 Correct 2 ms 384 KB Output is correct
59 Correct 2 ms 384 KB Output is correct
60 Correct 2 ms 384 KB Output is correct
61 Correct 2 ms 384 KB Output is correct
62 Correct 2 ms 384 KB Output is correct
63 Correct 2 ms 384 KB Output is correct
64 Correct 2 ms 384 KB Output is correct
65 Correct 2 ms 384 KB Output is correct
66 Correct 3 ms 384 KB Output is correct
67 Correct 2 ms 384 KB Output is correct
68 Correct 3 ms 384 KB Output is correct
69 Correct 3 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 3 ms 384 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 3 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 3 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 3 ms 384 KB Output is correct
25 Correct 2 ms 384 KB Output is correct
26 Correct 2 ms 384 KB Output is correct
27 Correct 2 ms 384 KB Output is correct
28 Correct 2 ms 512 KB Output is correct
29 Correct 3 ms 384 KB Output is correct
30 Correct 3 ms 384 KB Output is correct
31 Correct 2 ms 384 KB Output is correct
32 Correct 2 ms 384 KB Output is correct
33 Correct 3 ms 384 KB Output is correct
34 Correct 3 ms 384 KB Output is correct
35 Correct 2 ms 384 KB Output is correct
36 Correct 2 ms 384 KB Output is correct
37 Correct 3 ms 384 KB Output is correct
38 Correct 2 ms 384 KB Output is correct
39 Correct 2 ms 384 KB Output is correct
40 Correct 2 ms 384 KB Output is correct
41 Correct 2 ms 384 KB Output is correct
42 Correct 2 ms 384 KB Output is correct
43 Correct 2 ms 384 KB Output is correct
44 Correct 2 ms 392 KB Output is correct
45 Correct 2 ms 384 KB Output is correct
46 Correct 2 ms 384 KB Output is correct
47 Correct 2 ms 384 KB Output is correct
48 Correct 2 ms 384 KB Output is correct
49 Correct 3 ms 412 KB Output is correct
50 Correct 2 ms 384 KB Output is correct
51 Correct 2 ms 384 KB Output is correct
52 Correct 2 ms 384 KB Output is correct
53 Correct 3 ms 384 KB Output is correct
54 Correct 2 ms 384 KB Output is correct
55 Correct 3 ms 384 KB Output is correct
56 Correct 3 ms 384 KB Output is correct
57 Correct 3 ms 384 KB Output is correct
58 Correct 2 ms 384 KB Output is correct
59 Correct 2 ms 384 KB Output is correct
60 Correct 2 ms 384 KB Output is correct
61 Correct 2 ms 384 KB Output is correct
62 Correct 2 ms 384 KB Output is correct
63 Correct 2 ms 384 KB Output is correct
64 Correct 2 ms 384 KB Output is correct
65 Correct 2 ms 384 KB Output is correct
66 Correct 3 ms 384 KB Output is correct
67 Correct 2 ms 384 KB Output is correct
68 Correct 3 ms 384 KB Output is correct
69 Correct 3 ms 384 KB Output is correct
70 Correct 4 ms 512 KB Output is correct
71 Correct 4 ms 396 KB Output is correct
72 Correct 2 ms 384 KB Output is correct
73 Correct 3 ms 384 KB Output is correct
74 Correct 2 ms 384 KB Output is correct
75 Correct 3 ms 384 KB Output is correct
76 Correct 2 ms 396 KB Output is correct
77 Correct 2 ms 384 KB Output is correct
78 Correct 2 ms 384 KB Output is correct
79 Correct 2 ms 384 KB Output is correct
80 Correct 2 ms 384 KB Output is correct
81 Incorrect 3 ms 384 KB no wires in direction 1 between segments 265 and 265
82 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 4216 KB Output is correct
2 Correct 4 ms 1536 KB Output is correct
3 Correct 22 ms 2936 KB Output is correct
4 Correct 17 ms 2944 KB Output is correct
5 Correct 56 ms 4900 KB Output is correct
6 Correct 39 ms 4724 KB Output is correct
7 Correct 45 ms 4492 KB Output is correct
8 Correct 5 ms 1664 KB Output is correct
9 Correct 5 ms 1536 KB Output is correct
10 Correct 41 ms 5104 KB Output is correct
11 Correct 37 ms 4084 KB Output is correct
12 Correct 57 ms 4216 KB Output is correct
13 Correct 4 ms 1536 KB Output is correct
14 Correct 4 ms 1536 KB Output is correct
15 Correct 44 ms 4604 KB Output is correct
16 Correct 16 ms 3696 KB Output is correct
17 Correct 56 ms 4472 KB Output is correct
18 Correct 55 ms 3320 KB Output is correct
19 Correct 8 ms 1664 KB Output is correct
20 Correct 37 ms 4720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 3 ms 384 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 3 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 3 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 3 ms 384 KB Output is correct
25 Correct 2 ms 384 KB Output is correct
26 Correct 2 ms 384 KB Output is correct
27 Correct 2 ms 384 KB Output is correct
28 Correct 2 ms 512 KB Output is correct
29 Correct 3 ms 384 KB Output is correct
30 Correct 3 ms 384 KB Output is correct
31 Correct 2 ms 384 KB Output is correct
32 Correct 2 ms 384 KB Output is correct
33 Correct 3 ms 384 KB Output is correct
34 Correct 3 ms 384 KB Output is correct
35 Correct 2 ms 384 KB Output is correct
36 Correct 2 ms 384 KB Output is correct
37 Correct 3 ms 384 KB Output is correct
38 Correct 2 ms 384 KB Output is correct
39 Correct 2 ms 384 KB Output is correct
40 Correct 2 ms 384 KB Output is correct
41 Correct 2 ms 384 KB Output is correct
42 Correct 2 ms 384 KB Output is correct
43 Correct 2 ms 384 KB Output is correct
44 Correct 2 ms 392 KB Output is correct
45 Correct 2 ms 384 KB Output is correct
46 Correct 2 ms 384 KB Output is correct
47 Correct 2 ms 384 KB Output is correct
48 Correct 2 ms 384 KB Output is correct
49 Correct 3 ms 412 KB Output is correct
50 Correct 2 ms 384 KB Output is correct
51 Correct 2 ms 384 KB Output is correct
52 Correct 2 ms 384 KB Output is correct
53 Correct 3 ms 384 KB Output is correct
54 Correct 2 ms 384 KB Output is correct
55 Correct 3 ms 384 KB Output is correct
56 Correct 3 ms 384 KB Output is correct
57 Correct 3 ms 384 KB Output is correct
58 Correct 2 ms 384 KB Output is correct
59 Correct 2 ms 384 KB Output is correct
60 Correct 2 ms 384 KB Output is correct
61 Correct 2 ms 384 KB Output is correct
62 Correct 2 ms 384 KB Output is correct
63 Correct 2 ms 384 KB Output is correct
64 Correct 2 ms 384 KB Output is correct
65 Correct 2 ms 384 KB Output is correct
66 Correct 3 ms 384 KB Output is correct
67 Correct 2 ms 384 KB Output is correct
68 Correct 3 ms 384 KB Output is correct
69 Correct 3 ms 384 KB Output is correct
70 Correct 4 ms 512 KB Output is correct
71 Correct 4 ms 396 KB Output is correct
72 Correct 2 ms 384 KB Output is correct
73 Correct 3 ms 384 KB Output is correct
74 Correct 2 ms 384 KB Output is correct
75 Correct 3 ms 384 KB Output is correct
76 Correct 2 ms 396 KB Output is correct
77 Correct 2 ms 384 KB Output is correct
78 Correct 2 ms 384 KB Output is correct
79 Correct 2 ms 384 KB Output is correct
80 Correct 2 ms 384 KB Output is correct
81 Incorrect 3 ms 384 KB no wires in direction 1 between segments 265 and 265
82 Halted 0 ms 0 KB -