답안 #404403

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
404403 2021-05-14T11:24:26 Z rama_pang Alternating Current (BOI18_alternating) C++17
19 / 100
128 ms 17616 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 + int(mainSegments.size()) - 1) % mainSegments.size() + 1;
        for (int i_ = 0; i_ < rep; i_++) {
          int i = (S0->sortedIndex + i_) % mainSegments.size();
          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 268 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 Incorrect 1 ms 204 KB 'impossible' claimed, but there is a solution
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 268 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 Incorrect 1 ms 204 KB 'impossible' claimed, but there is a solution
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 268 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 Incorrect 1 ms 204 KB 'impossible' claimed, but there is a solution
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 128 ms 17616 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 48 ms 6400 KB Output is correct
4 Correct 54 ms 8856 KB Output is correct
5 Correct 70 ms 10736 KB Output is correct
6 Correct 67 ms 11288 KB Output is correct
7 Correct 71 ms 11136 KB Output is correct
8 Correct 3 ms 700 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 85 ms 11200 KB Output is correct
11 Correct 63 ms 9676 KB Output is correct
12 Correct 109 ms 15720 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 123 ms 17560 KB Output is correct
16 Correct 22 ms 5052 KB Output is correct
17 Correct 114 ms 15812 KB Output is correct
18 Correct 117 ms 16924 KB Output is correct
19 Correct 5 ms 1028 KB Output is correct
20 Correct 77 ms 10980 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 268 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 Incorrect 1 ms 204 KB 'impossible' claimed, but there is a solution
16 Halted 0 ms 0 KB -