// This solution has not been proven correct, so it may not be.
// However, it has been tested on a large number of random test cases.
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <cassert>
using namespace std;
int M, N;
struct Segment {
int index, sortedIndex;
int start, length;
bool direction;
Segment(int index, int start, int length) :
index(index),
start(start),
length(length),
direction(0) {
}
// Check if this segment is completely inside another segment
bool isSubsetOf(const Segment& other) const {
int relativeStart = (start - other.start + N) % N;
return relativeStart + length <= other.length;
}
};
// Returns whether the given solution covers the whole circle
bool checkSolution(const vector<Segment>& segments) {
for (int direction = 0; direction < 2; direction++) {
size_t n = segments.size();
int hi = 0;
for (size_t i = 0; i < 2*n; i++) {
size_t j = i%n;
if (segments[j].direction != direction) {
continue;
}
int start = segments[j].start;
if (i < n) {
start -= N;
}
if (start > hi) {
return false;
}
int end = start + segments[j].length;
hi = max(hi, end);
}
if (hi < N) {
return false;
}
}
return true;
}
void printSolution(const vector<Segment>& segments) {
vector<bool> directions(segments.size());
for (const Segment& segment : segments) {
directions[segment.index] = segment.direction;
}
for (bool d : directions) {
cout << d;
}
cout << endl;
}
set<pair<int, pair<int, pair<int, int> > > > checked;
void rec(vector<Segment>& segments, int i, int hi0, int hi1, int inRow) {
if (i == 2*M) {
if (checkSolution(segments)) {
printSolution(segments);
exit(0);
}
return;
}
auto key = make_pair(inRow, make_pair(i, make_pair(hi0, hi1)));
if (checked.count(key))
return;
checked.insert(key);
int j = i%M;
int start = segments[j].start;
if (i < M) {
start -= N;
}
if (start > hi0 || start > hi1) {
return;
}
int end = start + segments[j].length;
if (end > hi1 || end <= hi0) {
int newInRow = inRow;
if (hi1 > hi0) {
++newInRow;
}
else {
newInRow = 0;
}
if (newInRow < 2) {
segments[j].direction = 1;
rec(segments, i+1, hi0, max(hi1, end), newInRow);
}
}
if (end > hi0 || end <= hi1) {
int newInRow = inRow;
if (hi0 > hi1) {
++newInRow;
}
else {
newInRow = 0;
}
if (newInRow < 2) {
segments[j].direction = 0;
rec(segments, i+1, max(hi0, end), hi1, newInRow);
}
}
}
int main() {
ios::sync_with_stdio(0);
cin >> N >> M;
vector<Segment> segments;
for (int i = 0; i < M; i++) {
int a, b;
cin >> a >> b;
segments.emplace_back(i, a-1, ((b-a+N)%N)+1);
}
sort(segments.begin(), segments.end(), [](const Segment& a, const Segment& b) -> bool {
if (a.start == b.start) {
return a.length > b.length;
}
return a.start < b.start;
});
for (int i = 0; i < M; i++) {
segments[i].sortedIndex = i;
}
rec(segments, 0, 0, 0, 0);
cout << "IMPOSSIBLE" << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
360 KB |
Output is correct |
3 |
Correct |
2 ms |
472 KB |
Output is correct |
4 |
Correct |
3 ms |
584 KB |
Output is correct |
5 |
Correct |
2 ms |
696 KB |
Output is correct |
6 |
Correct |
2 ms |
828 KB |
Output is correct |
7 |
Correct |
2 ms |
828 KB |
Output is correct |
8 |
Correct |
2 ms |
828 KB |
Output is correct |
9 |
Correct |
3 ms |
828 KB |
Output is correct |
10 |
Correct |
2 ms |
828 KB |
Output is correct |
11 |
Correct |
2 ms |
828 KB |
Output is correct |
12 |
Correct |
2 ms |
828 KB |
Output is correct |
13 |
Correct |
3 ms |
828 KB |
Output is correct |
14 |
Correct |
3 ms |
828 KB |
Output is correct |
15 |
Correct |
3 ms |
828 KB |
Output is correct |
16 |
Correct |
3 ms |
828 KB |
Output is correct |
17 |
Correct |
3 ms |
828 KB |
Output is correct |
18 |
Correct |
3 ms |
828 KB |
Output is correct |
19 |
Correct |
3 ms |
828 KB |
Output is correct |
20 |
Correct |
3 ms |
828 KB |
Output is correct |
21 |
Correct |
2 ms |
828 KB |
Output is correct |
22 |
Correct |
2 ms |
828 KB |
Output is correct |
23 |
Correct |
2 ms |
828 KB |
Output is correct |
24 |
Correct |
2 ms |
828 KB |
Output is correct |
25 |
Correct |
3 ms |
932 KB |
Output is correct |
26 |
Correct |
2 ms |
932 KB |
Output is correct |
27 |
Correct |
2 ms |
932 KB |
Output is correct |
28 |
Correct |
2 ms |
932 KB |
Output is correct |
29 |
Correct |
2 ms |
976 KB |
Output is correct |
30 |
Correct |
3 ms |
976 KB |
Output is correct |
31 |
Correct |
2 ms |
976 KB |
Output is correct |
32 |
Correct |
2 ms |
976 KB |
Output is correct |
33 |
Correct |
2 ms |
992 KB |
Output is correct |
34 |
Correct |
2 ms |
992 KB |
Output is correct |
35 |
Correct |
3 ms |
992 KB |
Output is correct |
36 |
Correct |
2 ms |
992 KB |
Output is correct |
37 |
Correct |
4 ms |
992 KB |
Output is correct |
38 |
Correct |
3 ms |
992 KB |
Output is correct |
39 |
Correct |
2 ms |
992 KB |
Output is correct |
40 |
Correct |
2 ms |
992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
992 KB |
Output is correct |
2 |
Correct |
2 ms |
992 KB |
Output is correct |
3 |
Correct |
2 ms |
992 KB |
Output is correct |
4 |
Correct |
3 ms |
992 KB |
Output is correct |
5 |
Correct |
2 ms |
992 KB |
Output is correct |
6 |
Correct |
0 ms |
992 KB |
Output is correct |
7 |
Correct |
2 ms |
992 KB |
Output is correct |
8 |
Correct |
2 ms |
1048 KB |
Output is correct |
9 |
Correct |
2 ms |
1048 KB |
Output is correct |
10 |
Correct |
2 ms |
1048 KB |
Output is correct |
11 |
Correct |
2 ms |
1048 KB |
Output is correct |
12 |
Correct |
2 ms |
1048 KB |
Output is correct |
13 |
Correct |
2 ms |
1048 KB |
Output is correct |
14 |
Correct |
2 ms |
1048 KB |
Output is correct |
15 |
Correct |
2 ms |
1048 KB |
Output is correct |
16 |
Correct |
3 ms |
1048 KB |
Output is correct |
17 |
Correct |
2 ms |
1048 KB |
Output is correct |
18 |
Correct |
3 ms |
1096 KB |
Output is correct |
19 |
Correct |
2 ms |
1096 KB |
Output is correct |
20 |
Correct |
2 ms |
1096 KB |
Output is correct |
21 |
Correct |
3 ms |
1096 KB |
Output is correct |
22 |
Correct |
3 ms |
1096 KB |
Output is correct |
23 |
Correct |
2 ms |
1096 KB |
Output is correct |
24 |
Correct |
3 ms |
1096 KB |
Output is correct |
25 |
Correct |
3 ms |
1096 KB |
Output is correct |
26 |
Correct |
2 ms |
1096 KB |
Output is correct |
27 |
Correct |
2 ms |
1116 KB |
Output is correct |
28 |
Correct |
3 ms |
1120 KB |
Output is correct |
29 |
Correct |
2 ms |
1120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1304 KB |
Output is correct |
2 |
Correct |
4 ms |
1444 KB |
Output is correct |
3 |
Correct |
3 ms |
1444 KB |
Output is correct |
4 |
Correct |
3 ms |
1444 KB |
Output is correct |
5 |
Correct |
3 ms |
1444 KB |
Output is correct |
6 |
Correct |
3 ms |
1444 KB |
Output is correct |
7 |
Correct |
3 ms |
1444 KB |
Output is correct |
8 |
Correct |
3 ms |
1444 KB |
Output is correct |
9 |
Correct |
3 ms |
1444 KB |
Output is correct |
10 |
Correct |
3 ms |
1444 KB |
Output is correct |
11 |
Correct |
3 ms |
1444 KB |
Output is correct |
12 |
Correct |
2 ms |
1444 KB |
Output is correct |
13 |
Correct |
3 ms |
1444 KB |
Output is correct |
14 |
Correct |
3 ms |
1444 KB |
Output is correct |
15 |
Correct |
3 ms |
1536 KB |
Output is correct |
16 |
Correct |
3 ms |
1536 KB |
Output is correct |
17 |
Correct |
5 ms |
1536 KB |
Output is correct |
18 |
Correct |
3 ms |
1548 KB |
Output is correct |
19 |
Correct |
3 ms |
1548 KB |
Output is correct |
20 |
Correct |
4 ms |
1548 KB |
Output is correct |
21 |
Correct |
4 ms |
1548 KB |
Output is correct |
22 |
Correct |
3 ms |
1548 KB |
Output is correct |
23 |
Correct |
4 ms |
1548 KB |
Output is correct |
24 |
Correct |
2 ms |
1548 KB |
Output is correct |
25 |
Correct |
3 ms |
1548 KB |
Output is correct |
26 |
Correct |
2 ms |
1548 KB |
Output is correct |
27 |
Correct |
4 ms |
1640 KB |
Output is correct |
28 |
Correct |
4 ms |
1640 KB |
Output is correct |
29 |
Correct |
4 ms |
1640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
204 ms |
38652 KB |
Output is correct |
2 |
Correct |
3 ms |
38652 KB |
Output is correct |
3 |
Correct |
67 ms |
38652 KB |
Output is correct |
4 |
Correct |
94 ms |
38652 KB |
Output is correct |
5 |
Correct |
166 ms |
39936 KB |
Output is correct |
6 |
Correct |
188 ms |
39936 KB |
Output is correct |
7 |
Correct |
165 ms |
39936 KB |
Output is correct |
8 |
Correct |
7 ms |
39936 KB |
Output is correct |
9 |
Correct |
3 ms |
39936 KB |
Output is correct |
10 |
Correct |
161 ms |
45000 KB |
Output is correct |
11 |
Correct |
134 ms |
45000 KB |
Output is correct |
12 |
Correct |
224 ms |
45000 KB |
Output is correct |
13 |
Correct |
2 ms |
45000 KB |
Output is correct |
14 |
Correct |
2 ms |
45000 KB |
Output is correct |
15 |
Correct |
192 ms |
47340 KB |
Output is correct |
16 |
Correct |
170 ms |
47340 KB |
Output is correct |
17 |
Correct |
167 ms |
49096 KB |
Output is correct |
18 |
Correct |
187 ms |
50252 KB |
Output is correct |
19 |
Correct |
10 ms |
50252 KB |
Output is correct |
20 |
Correct |
220 ms |
51252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
51252 KB |
Output is correct |
2 |
Correct |
189 ms |
51252 KB |
Output is correct |
3 |
Correct |
98 ms |
51252 KB |
Output is correct |
4 |
Correct |
3 ms |
51252 KB |
Output is correct |
5 |
Correct |
7 ms |
51252 KB |
Output is correct |
6 |
Correct |
58 ms |
51252 KB |
Output is correct |
7 |
Correct |
43 ms |
51252 KB |
Output is correct |
8 |
Correct |
0 ms |
51252 KB |
Output is correct |
9 |
Correct |
16 ms |
51252 KB |
Output is correct |
10 |
Correct |
165 ms |
51252 KB |
Output is correct |
11 |
Correct |
81 ms |
51252 KB |
Output is correct |
12 |
Correct |
101 ms |
51252 KB |
Output is correct |
13 |
Correct |
3 ms |
51252 KB |
Output is correct |
14 |
Correct |
5 ms |
51252 KB |
Output is correct |
15 |
Correct |
156 ms |
56776 KB |
Output is correct |
16 |
Correct |
45 ms |
56776 KB |
Output is correct |
17 |
Correct |
3 ms |
56776 KB |
Output is correct |
18 |
Correct |
90 ms |
56776 KB |
Output is correct |
19 |
Correct |
256 ms |
64220 KB |
Output is correct |
20 |
Correct |
3 ms |
64220 KB |
Output is correct |