結果

問題 No.5022 XOR Printer
ユーザー risujiroh
提出日時 2025-07-26 15:58:29
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 4,492 bytes
コンパイル時間 3,786 ms
コンパイル使用メモリ 300,132 KB
実行使用メモリ 7,716 KB
スコア 5,146,397,359
最終ジャッジ日時 2025-07-26 15:58:35
合計ジャッジ時間 5,852 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #

#if __INCLUDE_LEVEL__ == 0

#include __BASE_FILE__

int N;
int T;

void Solve() {
  IN(N, T);
  vector<int> A(N * N);
  IN(A);

  vector<int> order;
  {
    order.reserve(N * N);
    assert(N % 2 == 0);
    for (int i : Rep(0, N)) {
      if (i % 2 == 0) {
        for (int j : Rep(1, N)) {
          order.push_back(i * N + j);
        }
      } else {
        for (int j : Rev(Rep(1, N))) {
          order.push_back(i * N + j);
        }
      }
    }
    for (int i : Rev(Rep(0, N))) {
      order.push_back(i * N);
    }
    assert(Sz(order) == N * N);
  }

  auto get_dist = [&](int x, int y) -> int {
    assert(0 <= x && x < N * N);
    assert(0 <= y && y < N * N);
    int xi = x / N, xj = x % N;
    int yi = y / N, yj = y % N;
    return abs(yi - xi) + abs(yj - xj);
  };

  auto get_moves = [&](int x, int y) -> string {
    assert(0 <= x && x < N * N);
    assert(0 <= y && y < N * N);
    int xi = x / N, xj = x % N;
    int yi = y / N, yj = y % N;
    string moves;
    moves.reserve(abs(yi - xi) + abs(yj - xj));
    for (; xj < yj; ++xj) moves += 'R';
    for (; xi < yi; ++xi) moves += 'D';
    for (; xj > yj; --xj) moves += 'L';
    for (; xi > yi; --xi) moves += 'U';
    return moves;
  };

  int cur_x = 0;
  int cur_s = 0;
  int cur_turn = 0;
  auto move_to = [&](int x) {
    assert(0 <= cur_x && cur_x < N * N);
    assert(0 <= x && x < N * N);
    for (char c : get_moves(cur_x, x)) {
      OUT(c);
      ++cur_turn;
    }
    cur_x = x;
  };
  auto copy = [&] {
    assert(0 <= cur_x && cur_x < N * N);
    OUT('C');
    ++cur_turn;
    cur_s ^= A[cur_x];
  };
  auto write = [&] {
    assert(0 <= cur_x && cur_x < N * N);
    OUT('W');
    ++cur_turn;
    A[cur_x] ^= cur_s;
  };

  vector<int> pivot_x(20, -1);
  for (int k : Rev(Rep(0, 20))) {
    int& P = pivot_x[k];
    for (int x : Rep(0, N * N)) {
      if ((A[x] >> k) == 1) {
        if (P == -1 || get_dist(cur_x, x) < get_dist(cur_x, P)) {
          P = x;
        }
      }
    }
    assert(P != -1);

    move_to(P);
    copy();
    ranges::rotate(order, ranges::find(order, P));
    bool should_break = false;
    for (int x : order | views::drop(1)) {
      auto estimate = [&, order] mutable {
        int ret = cur_turn;
        int tx = cur_x;
        ret += get_dist(exchange(tx, x), x) + 1;
        ret += get_dist(exchange(tx, P), P) + 1;
        int P = pivot_x.back();
        ret += get_dist(exchange(tx, P), P) + 1;
        ranges::rotate(order, ranges::find(order, P));
        for (int y : order | views::drop(1)) {
          ret += get_dist(exchange(tx, y), y) + 1;
        }
        P = pivot_x.end()[-2];
        ret += get_dist(exchange(tx, P), P) + 2;
        return ret;
      };
      int nA = A[x] ^ cur_s;
      if (x == pivot_x.back() ? nA > A[x] : nA < A[x]) {
        if (k < 15 && estimate() > T) {
          should_break = true;
          break;
        }
        move_to(x);
        write();
      }
    }
    move_to(P);
    copy();
    assert(cur_s == 0);
    if (should_break) {
      break;
    }
  }

  {
    int P = pivot_x.back();
    move_to(P);
    copy();
    ranges::rotate(order, ranges::find(order, P));
    for (int x : order | views::drop(1)) {
      move_to(x);
      write();
    }
    P = pivot_x.end()[-2];
    move_to(P);
    copy();
    write();
  }
}

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

  Solve();
}

#elif __INCLUDE_LEVEL__ == 1

#include <bits/stdc++.h>

template <class T> concept MyRange = std::ranges::range<T> && !std::convertible_to<T, std::string_view>;
template <class T> concept MyTuple = std::__is_tuple_like<T>::value && !MyRange<T>;

namespace std {

istream& operator>>(istream& is, MyRange auto&& r) {
  for (auto&& e : r) is >> e;
  return is;
}
istream& operator>>(istream& is, MyTuple auto&& t) {
  apply([&](auto&... xs) { (is >> ... >> xs); }, t);
  return is;
}

ostream& operator<<(ostream& os, MyRange auto&& r) {
  auto sep = "";
  for (auto&& e : r) os << exchange(sep, " ") << e;
  return os;
}
ostream& operator<<(ostream& os, MyTuple auto&& t) {
  auto sep = "";
  apply([&](auto&... xs) { ((os << exchange(sep, " ") << xs), ...); }, t);
  return os;
}

}  // namespace std

using namespace std;

#define Rev views::reverse
#define Rep(...) [](int l, int r) { return views::iota(min(l, r), r); }(__VA_ARGS__)
#define Sz(r) int(size(r))
#define IN(...) (cin >> forward_as_tuple(__VA_ARGS__))
#define OUT(...) (cout << forward_as_tuple(__VA_ARGS__) << '\n')

#endif  // __INCLUDE_LEVEL__ == 1
0