답안 #416682

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
416682 2021-06-02T18:36:45 Z noshi91 The Collection Game (BOI21_swaps) C++17
100 / 100
7 ms 496 KB
#include <cstdint>
#include <numeric>
#include <vector>


namespace bitonic_sorter_impl {

using u32 = std::uint32_t;

std::vector<std::vector<std::pair<u32, u32>>> bitonic_sorter(const u32 n) {
  u32 m = 1;
  while (m < n) {
    m *= 2;
  }
  std::vector<u32> map(m);
  std::iota(map.begin(), map.end(), static_cast<u32>(0));
  std::vector<std::vector<std::pair<u32, u32>>> ret;
  const auto push = [&](const u32 x, const u32 y) {
    if (map[y] >= n) {
      return;
    }
    if (map[x] < n) {
      ret.back().push_back({map[x], map[y]});
    } else {
      std::swap(map[x], map[y]);
    }
  };
  for (u32 w = 1; w != m; w *= 2) {
    for (u32 s = w; s != 0; s /= 2) {
      ret.push_back({});
      bool rev = false;
      for (u32 t = 0; t != m; t += 2 * w) {
        for (u32 k = 0; k != 2 * w; k += 2 * s) {
          for (u32 i = 0; i != s; i += 1) {
            u32 a = t + k + i;
            u32 b = t + k + i + s;
            if (rev) {
              push(b, a);
            } else {
              push(a, b);
            }
          }
        }
        rev = !rev;
      }
    }
  }
  return ret;
}

} // namespace bitonic_sorter_impl

using bitonic_sorter_impl::bitonic_sorter;

#include <algorithm>
#include <numeric>
#include <utility>

#if 1
#include <swaps.h>
#else
#include <cassert>
#include <iostream>

void solve(int, int);

int j_N = 5;
std::vector<int> j_A = {0, 4, 3, 1, 2};
std::vector<std::pair<int, int>> j_buf;

int main() {
  solve(j_N, 0);
  return 0;
}

void schedule(int i, int j) {
  assert(1 <= i && i <= j_N);
  assert(1 <= j && j <= j_N);
  assert(i != j);
  j_buf.push_back({i - 1, j - 1});
}

std::vector<int> visit() {
  std::vector<int> ret;
  for (const auto &[i, j] : j_buf) {
    ret.push_back(j_A[i] > j_A[j]);
  }
  j_buf.clear();
  return ret;
}

void answer(std::vector<int> r) {
  for (const auto &e : r) {
    std::cout << e - 1 << " ";
  }
  std::cout << "\n";
}
#endif

void solve(int N, int) {
  std::vector<int> a(N);
  std::iota(a.begin(), a.end(), 0);
  for (const auto &v : bitonic_sorter(N)) {
    for (const auto &[x, y] : v) {
      schedule(a[x] + 1, a[y] + 1);
    }
    const auto res = visit();
    for (int i = 0; i != v.size(); i += 1) {
      if (res[i]) {
        std::swap(a[v[i].first], a[v[i].second]);
      }
    }
  }
  std::reverse(a.begin(), a.end());
  for (auto &e : a) {
    e += 1;
  }
  answer(a);
}

Compilation message

swaps.cpp: In function 'void solve(int, int)':
swaps.cpp:108:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<unsigned int, unsigned int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  108 |     for (int i = 0; i != v.size(); i += 1) {
      |                     ~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 200 KB Correct
2 Correct 1 ms 200 KB Correct
3 Correct 2 ms 200 KB Correct
4 Correct 5 ms 380 KB Correct
5 Correct 5 ms 444 KB Correct
6 Correct 6 ms 384 KB Correct
7 Correct 5 ms 380 KB Correct
8 Correct 5 ms 376 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 200 KB Correct
2 Correct 1 ms 200 KB Correct
3 Correct 2 ms 200 KB Correct
4 Correct 5 ms 376 KB Correct
5 Correct 5 ms 388 KB Correct
6 Correct 5 ms 376 KB Correct
7 Correct 6 ms 372 KB Correct
8 Correct 4 ms 384 KB Correct
9 Correct 5 ms 384 KB Correct
10 Correct 5 ms 384 KB Correct
11 Correct 5 ms 384 KB Correct
12 Correct 5 ms 380 KB Correct
13 Correct 5 ms 380 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 200 KB Correct
2 Correct 1 ms 200 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 200 KB Correct
2 Correct 1 ms 200 KB Correct
3 Correct 1 ms 200 KB Correct
4 Correct 1 ms 200 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 200 KB Correct
2 Correct 1 ms 200 KB Correct
3 Correct 3 ms 200 KB Correct
4 Correct 5 ms 380 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 200 KB Correct
2 Correct 1 ms 200 KB Correct
3 Correct 3 ms 200 KB Correct
4 Correct 5 ms 380 KB Correct
5 Correct 1 ms 200 KB Correct
6 Correct 1 ms 200 KB Correct
7 Correct 3 ms 200 KB Correct
8 Correct 5 ms 380 KB Correct
9 Correct 5 ms 380 KB Correct
10 Correct 5 ms 376 KB Correct
11 Correct 6 ms 380 KB Correct
12 Correct 5 ms 376 KB Correct
13 Correct 1 ms 200 KB Correct
14 Correct 1 ms 200 KB Correct
15 Correct 3 ms 200 KB Correct
16 Correct 5 ms 380 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 200 KB Correct
2 Correct 1 ms 200 KB Correct
3 Correct 2 ms 200 KB Correct
4 Correct 5 ms 380 KB Correct
5 Correct 5 ms 376 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 200 KB Correct
2 Correct 1 ms 200 KB Correct
3 Correct 2 ms 200 KB Correct
4 Correct 5 ms 380 KB Correct
5 Correct 5 ms 376 KB Correct
6 Correct 1 ms 200 KB Correct
7 Correct 1 ms 200 KB Correct
8 Correct 2 ms 200 KB Correct
9 Correct 7 ms 376 KB Correct
10 Correct 5 ms 376 KB Correct
11 Correct 5 ms 384 KB Correct
12 Correct 5 ms 380 KB Correct
13 Correct 5 ms 376 KB Correct
14 Correct 5 ms 380 KB Correct
15 Correct 5 ms 380 KB Correct
16 Correct 5 ms 380 KB Correct
17 Correct 5 ms 384 KB Correct
18 Correct 5 ms 380 KB Correct
19 Correct 0 ms 200 KB Correct
20 Correct 1 ms 200 KB Correct
21 Correct 3 ms 200 KB Correct
22 Correct 6 ms 380 KB Correct
23 Correct 5 ms 384 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 200 KB Correct
2 Correct 1 ms 200 KB Correct
3 Correct 2 ms 200 KB Correct
4 Correct 5 ms 384 KB Correct
5 Correct 6 ms 488 KB Correct
6 Correct 5 ms 360 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 200 KB Correct
2 Correct 1 ms 200 KB Correct
3 Correct 2 ms 200 KB Correct
4 Correct 5 ms 384 KB Correct
5 Correct 6 ms 488 KB Correct
6 Correct 5 ms 360 KB Correct
7 Correct 1 ms 200 KB Correct
8 Correct 1 ms 200 KB Correct
9 Correct 2 ms 200 KB Correct
10 Correct 5 ms 376 KB Correct
11 Correct 5 ms 376 KB Correct
12 Correct 5 ms 380 KB Correct
13 Correct 5 ms 372 KB Correct
14 Correct 5 ms 376 KB Correct
15 Correct 4 ms 380 KB Correct
16 Correct 5 ms 376 KB Correct
17 Correct 5 ms 380 KB Correct
18 Correct 5 ms 376 KB Correct
19 Correct 5 ms 376 KB Correct
20 Correct 0 ms 200 KB Correct
21 Correct 1 ms 200 KB Correct
22 Correct 2 ms 200 KB Correct
23 Correct 5 ms 376 KB Correct
24 Correct 5 ms 356 KB Correct
25 Correct 5 ms 360 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 200 KB Correct
2 Correct 1 ms 200 KB Correct
3 Correct 2 ms 200 KB Correct
4 Correct 5 ms 376 KB Correct
5 Correct 5 ms 360 KB Correct
6 Correct 5 ms 460 KB Correct
7 Correct 5 ms 360 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 200 KB Correct
2 Correct 1 ms 200 KB Correct
3 Correct 2 ms 200 KB Correct
4 Correct 5 ms 376 KB Correct
5 Correct 5 ms 360 KB Correct
6 Correct 5 ms 460 KB Correct
7 Correct 5 ms 360 KB Correct
8 Correct 0 ms 200 KB Correct
9 Correct 0 ms 200 KB Correct
10 Correct 1 ms 200 KB Correct
11 Correct 2 ms 200 KB Correct
12 Correct 5 ms 376 KB Correct
13 Correct 5 ms 384 KB Correct
14 Correct 5 ms 380 KB Correct
15 Correct 5 ms 376 KB Correct
16 Correct 6 ms 380 KB Correct
17 Correct 5 ms 496 KB Correct
18 Correct 5 ms 376 KB Correct
19 Correct 6 ms 380 KB Correct
20 Correct 5 ms 380 KB Correct
21 Correct 6 ms 376 KB Correct
22 Correct 0 ms 200 KB Correct
23 Correct 1 ms 200 KB Correct
24 Correct 3 ms 200 KB Correct
25 Correct 5 ms 380 KB Correct
26 Correct 5 ms 364 KB Correct
27 Correct 5 ms 356 KB Correct
28 Correct 5 ms 360 KB Correct