답안 #404405

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
404405 2021-05-14T11:28:11 Z rama_pang Alternating Current (BOI18_alternating) C++17
100 / 100
129 ms 17692 KB
#include <bits/stdc++.h>
using namespace std;

class Segment {
 public:
  int N;
  int index;
  int start;
  int length;
  int direction;
  int sortedIndex;
  set<Segment*> children;

  Segment(int _N, int i, int a, int b) {
    N = _N;
    index = i;
    start = a;
    length = (b + N - a) % N + 1;
    direction = 0;
    sortedIndex = -1;
    children.clear();
  }

  bool Include(const Segment &o) {
    int x = (o.start - start + N) % N;
    return x + o.length <= length;
  }

  void SetDirection(int dir) {
    direction = dir;
    for (auto i : children) {
      i->direction = !dir;
    }
  }
};

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int N, M;
  cin >> N >> M;

  vector<Segment> segments;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    segments.emplace_back(Segment(N, i, --a, --b));
  }

  sort(begin(segments), end(segments), [&](const Segment &i, const Segment &j) {
    return i.start < j.start || (i.start == j.start && i.length > j.length);
  });

  for (int i = 0; i < M; i++) {
    segments[i].sortedIndex = i;
  }

  vector<Segment*> mainSegments;
  Segment* last = &segments[0];
  vector<int> inMain(M);
  for (int i_ = 1; i_ < 4 * M; i_++) {
    int i = i_ % M;
    Segment* cur = &segments[i];
    if (cur->index != last->index && last->Include(*cur)) {
      if (inMain[last->index]) {
        last->children.emplace(cur);
      }
    } else {
      last = cur;
      if (i_ >= M && !inMain[cur->index]) {
        inMain[cur->index] = 1;
        mainSegments.emplace_back(cur);
      }
    }
  }

  const auto Alternating = [&](int start_) {
    for (int i_ = 0; i_ < int(mainSegments.size()); i_++) {
      int i = (i_ + start_) % mainSegments.size();
      mainSegments[i]->SetDirection(i_ % 2);
    }
  };

  if (mainSegments.size() % 2 == 0) {
    Alternating(0);
  } else {
    for (int i = 0; i < int(mainSegments.size()); i++) {
      auto S0 = mainSegments[(i + 0) % mainSegments.size()];
      auto S1 = mainSegments[(i + 1) % mainSegments.size()];
      auto S2 = mainSegments[(i + 2) % mainSegments.size()];
      auto S3 = mainSegments[(i + 3) % mainSegments.size()];

      S0->SetDirection(1);
      S1->SetDirection(0);
      S2->SetDirection(0);
      S3->SetDirection(1);

      if ([&]() -> bool {
        // Check if segment S0...S3 is good
        int cur = S0->start;
        int rep = (S3->sortedIndex - S0->sortedIndex + M - 1) % M + 1;
        for (int i_ = 0; i_ < rep; i_++) {
          int i = (S0->sortedIndex + i_) % M;
          if (segments[i].direction == 0) continue;
          int st = segments[i].start;
          if (i < S0->sortedIndex) st += N;
          if (cur < st) return false;
          cur = max(cur, st + segments[i].length);
        }
        int i = S3->sortedIndex;
        int st = segments[i].start;
        if (i <= S0->sortedIndex) st += N;
        if (cur < st) return false;
        return true;
      }()) {
        Alternating((i + 2) % mainSegments.size());
        break;
      }
    }
  }

  if ([&]() -> bool {
    // Check whether solution is valid
    for (int dir = 0; dir < 2; dir++) {
      int cur = 0;
      for (int i = 0; i < 2 * M; i++) if (segments[i % M].direction == dir) {
        int st = segments[i % M].start;
        if (i < M) st -= N;
        if (cur < st) return false;
        cur = max(cur, st + segments[i % M].length);
      }
      if (cur < N) return false;
    }
    return true;
  }()) {
    vector<int> ans(M);
    for (auto seg : segments) {
      ans[seg.index] = seg.direction;
    }
    for (int i = 0; i < M; i++) {
      cout << ans[i];
    }
    cout << '\n';
  } else {
    cout << "impossible\n";
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 1 ms 204 KB Output is correct
37 Correct 1 ms 204 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 1 ms 204 KB Output is correct
37 Correct 1 ms 204 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
41 Correct 1 ms 204 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 1 ms 204 KB Output is correct
45 Correct 1 ms 204 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 1 ms 204 KB Output is correct
50 Correct 1 ms 204 KB Output is correct
51 Correct 1 ms 204 KB Output is correct
52 Correct 1 ms 204 KB Output is correct
53 Correct 1 ms 204 KB Output is correct
54 Correct 1 ms 204 KB Output is correct
55 Correct 1 ms 204 KB Output is correct
56 Correct 1 ms 332 KB Output is correct
57 Correct 1 ms 204 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 1 ms 204 KB Output is correct
61 Correct 1 ms 204 KB Output is correct
62 Correct 1 ms 204 KB Output is correct
63 Correct 1 ms 204 KB Output is correct
64 Correct 1 ms 204 KB Output is correct
65 Correct 1 ms 204 KB Output is correct
66 Correct 1 ms 332 KB Output is correct
67 Correct 1 ms 204 KB Output is correct
68 Correct 1 ms 204 KB Output is correct
69 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 1 ms 204 KB Output is correct
37 Correct 1 ms 204 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
41 Correct 1 ms 204 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 1 ms 204 KB Output is correct
45 Correct 1 ms 204 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 1 ms 204 KB Output is correct
50 Correct 1 ms 204 KB Output is correct
51 Correct 1 ms 204 KB Output is correct
52 Correct 1 ms 204 KB Output is correct
53 Correct 1 ms 204 KB Output is correct
54 Correct 1 ms 204 KB Output is correct
55 Correct 1 ms 204 KB Output is correct
56 Correct 1 ms 332 KB Output is correct
57 Correct 1 ms 204 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 1 ms 204 KB Output is correct
61 Correct 1 ms 204 KB Output is correct
62 Correct 1 ms 204 KB Output is correct
63 Correct 1 ms 204 KB Output is correct
64 Correct 1 ms 204 KB Output is correct
65 Correct 1 ms 204 KB Output is correct
66 Correct 1 ms 332 KB Output is correct
67 Correct 1 ms 204 KB Output is correct
68 Correct 1 ms 204 KB Output is correct
69 Correct 1 ms 204 KB Output is correct
70 Correct 2 ms 460 KB Output is correct
71 Correct 1 ms 332 KB Output is correct
72 Correct 1 ms 204 KB Output is correct
73 Correct 1 ms 204 KB Output is correct
74 Correct 1 ms 332 KB Output is correct
75 Correct 1 ms 204 KB Output is correct
76 Correct 1 ms 332 KB Output is correct
77 Correct 1 ms 332 KB Output is correct
78 Correct 1 ms 204 KB Output is correct
79 Correct 1 ms 204 KB Output is correct
80 Correct 1 ms 332 KB Output is correct
81 Correct 1 ms 332 KB Output is correct
82 Correct 1 ms 332 KB Output is correct
83 Correct 1 ms 332 KB Output is correct
84 Correct 1 ms 332 KB Output is correct
85 Correct 1 ms 204 KB Output is correct
86 Correct 1 ms 204 KB Output is correct
87 Correct 1 ms 332 KB Output is correct
88 Correct 1 ms 204 KB Output is correct
89 Correct 1 ms 332 KB Output is correct
90 Correct 2 ms 460 KB Output is correct
91 Correct 1 ms 204 KB Output is correct
92 Correct 2 ms 332 KB Output is correct
93 Correct 1 ms 204 KB Output is correct
94 Correct 1 ms 204 KB Output is correct
95 Correct 1 ms 204 KB Output is correct
96 Correct 1 ms 332 KB Output is correct
97 Correct 2 ms 460 KB Output is correct
98 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 129 ms 17584 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 46 ms 6316 KB Output is correct
4 Correct 74 ms 8940 KB Output is correct
5 Correct 71 ms 10764 KB Output is correct
6 Correct 64 ms 11196 KB Output is correct
7 Correct 71 ms 11164 KB Output is correct
8 Correct 3 ms 700 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 80 ms 11304 KB Output is correct
11 Correct 56 ms 9628 KB Output is correct
12 Correct 107 ms 15780 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 124 ms 17692 KB Output is correct
16 Correct 27 ms 5052 KB Output is correct
17 Correct 112 ms 15836 KB Output is correct
18 Correct 116 ms 16956 KB Output is correct
19 Correct 5 ms 936 KB Output is correct
20 Correct 81 ms 11040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 1 ms 204 KB Output is correct
37 Correct 1 ms 204 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
41 Correct 1 ms 204 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 1 ms 204 KB Output is correct
45 Correct 1 ms 204 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 1 ms 204 KB Output is correct
50 Correct 1 ms 204 KB Output is correct
51 Correct 1 ms 204 KB Output is correct
52 Correct 1 ms 204 KB Output is correct
53 Correct 1 ms 204 KB Output is correct
54 Correct 1 ms 204 KB Output is correct
55 Correct 1 ms 204 KB Output is correct
56 Correct 1 ms 332 KB Output is correct
57 Correct 1 ms 204 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 1 ms 204 KB Output is correct
61 Correct 1 ms 204 KB Output is correct
62 Correct 1 ms 204 KB Output is correct
63 Correct 1 ms 204 KB Output is correct
64 Correct 1 ms 204 KB Output is correct
65 Correct 1 ms 204 KB Output is correct
66 Correct 1 ms 332 KB Output is correct
67 Correct 1 ms 204 KB Output is correct
68 Correct 1 ms 204 KB Output is correct
69 Correct 1 ms 204 KB Output is correct
70 Correct 2 ms 460 KB Output is correct
71 Correct 1 ms 332 KB Output is correct
72 Correct 1 ms 204 KB Output is correct
73 Correct 1 ms 204 KB Output is correct
74 Correct 1 ms 332 KB Output is correct
75 Correct 1 ms 204 KB Output is correct
76 Correct 1 ms 332 KB Output is correct
77 Correct 1 ms 332 KB Output is correct
78 Correct 1 ms 204 KB Output is correct
79 Correct 1 ms 204 KB Output is correct
80 Correct 1 ms 332 KB Output is correct
81 Correct 1 ms 332 KB Output is correct
82 Correct 1 ms 332 KB Output is correct
83 Correct 1 ms 332 KB Output is correct
84 Correct 1 ms 332 KB Output is correct
85 Correct 1 ms 204 KB Output is correct
86 Correct 1 ms 204 KB Output is correct
87 Correct 1 ms 332 KB Output is correct
88 Correct 1 ms 204 KB Output is correct
89 Correct 1 ms 332 KB Output is correct
90 Correct 2 ms 460 KB Output is correct
91 Correct 1 ms 204 KB Output is correct
92 Correct 2 ms 332 KB Output is correct
93 Correct 1 ms 204 KB Output is correct
94 Correct 1 ms 204 KB Output is correct
95 Correct 1 ms 204 KB Output is correct
96 Correct 1 ms 332 KB Output is correct
97 Correct 2 ms 460 KB Output is correct
98 Correct 1 ms 332 KB Output is correct
99 Correct 129 ms 17584 KB Output is correct
100 Correct 1 ms 204 KB Output is correct
101 Correct 46 ms 6316 KB Output is correct
102 Correct 74 ms 8940 KB Output is correct
103 Correct 71 ms 10764 KB Output is correct
104 Correct 64 ms 11196 KB Output is correct
105 Correct 71 ms 11164 KB Output is correct
106 Correct 3 ms 700 KB Output is correct
107 Correct 2 ms 332 KB Output is correct
108 Correct 80 ms 11304 KB Output is correct
109 Correct 56 ms 9628 KB Output is correct
110 Correct 107 ms 15780 KB Output is correct
111 Correct 1 ms 204 KB Output is correct
112 Correct 1 ms 204 KB Output is correct
113 Correct 124 ms 17692 KB Output is correct
114 Correct 27 ms 5052 KB Output is correct
115 Correct 112 ms 15836 KB Output is correct
116 Correct 116 ms 16956 KB Output is correct
117 Correct 5 ms 936 KB Output is correct
118 Correct 81 ms 11040 KB Output is correct
119 Correct 1 ms 204 KB Output is correct
120 Correct 51 ms 9688 KB Output is correct
121 Correct 28 ms 5056 KB Output is correct
122 Correct 1 ms 204 KB Output is correct
123 Correct 4 ms 1064 KB Output is correct
124 Correct 23 ms 5180 KB Output is correct
125 Correct 20 ms 3324 KB Output is correct
126 Correct 1 ms 332 KB Output is correct
127 Correct 9 ms 1668 KB Output is correct
128 Correct 58 ms 9772 KB Output is correct
129 Correct 37 ms 6112 KB Output is correct
130 Correct 42 ms 6872 KB Output is correct
131 Correct 1 ms 332 KB Output is correct
132 Correct 1 ms 332 KB Output is correct
133 Correct 90 ms 13048 KB Output is correct
134 Correct 23 ms 3880 KB Output is correct
135 Correct 1 ms 204 KB Output is correct
136 Correct 32 ms 5804 KB Output is correct
137 Correct 52 ms 9784 KB Output is correct
138 Correct 2 ms 460 KB Output is correct