結果

問題 No.2283 Prohibit Three Consecutive
ユーザー risujirohrisujiroh
提出日時 2023-04-28 23:11:47
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 16 ms / 2,000 ms
コード長 2,952 bytes
コンパイル時間 2,931 ms
コンパイル使用メモリ 250,280 KB
実行使用メモリ 11,476 KB
最終ジャッジ日時 2024-04-29 00:26:19
合計ジャッジ時間 3,695 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 11 ms
5,376 KB
testcase_04 AC 10 ms
5,376 KB
testcase_05 AC 15 ms
5,376 KB
testcase_06 AC 15 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 7 ms
5,376 KB
testcase_09 AC 8 ms
5,376 KB
testcase_10 AC 15 ms
11,476 KB
testcase_11 AC 15 ms
11,352 KB
testcase_12 AC 8 ms
5,376 KB
testcase_13 AC 16 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#if __INCLUDE_LEVEL__

void solve() {
  int n = input();
  auto s = input<string>();
  s += s;
  auto set = [&](int i, char c) {
    i %= n;
    if (i < 0) {
      i += n;
    }
    s[i] = c;
    s[n + i] = c;
  };
  auto go = [&](auto go, int i) -> void {
    i %= n;
    if (i < 0) {
      i += n;
    }
    auto t = s.substr(i, 3);
    if (t == "00?" || t == "11?") {
      set(i + 2, s[i] ^ 1);
      go(go, i + 2);
    } else if (t == "?00" || t == "?11") {
      set(i, s[i + 2] ^ 1);
      go(go, i - 2);
    }
  };
  for (i : iota(0, n)) {
    go(go, i);
  }
  bool ans = true;
  show(s);
  for (i : iota(0, n)) {
    auto t = s.substr(i, 3);
    if (t == "000" || t == "111") {
      ans = false;
      break;
    }
  }
  print(ans ? "Yes" : "No");
}

#else  // __INCLUDE_LEVEL__

#include <bits/stdc++.h>

namespace std {

template <class T>
auto operator>>(istream& is, T&& t) -> decltype(tuple_cat(t), is) {
  return apply([&is](auto&... xs) -> istream& { return (is >> ... >> xs); }, t);
}

template <class T>
auto operator<<(ostream& os, T&& t) -> decltype(tuple_cat(t), os) {
  auto f = [&os](auto&... xs) -> ostream& {
    [[maybe_unused]] auto sep = "";
    ((os << exchange(sep, " ") << xs), ...);
    return os;
  };
  return apply(f, t);
}

template <class T>
istream& operator>>(istream& is, vector<T>& v) {
  for (auto&& e : v) {
    is >> e;
  }
  return is;
}

template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  static constexpr auto SEP = is_convertible_v<int, T> ? " " : "\n";
  for (auto sep = ""; const auto& e : v) {
    os << exchange(sep, SEP) << e;
  }
  return os;
}

}  // namespace std

template <class... Ts, class... Args>
auto input(Args&&... args) {
  if constexpr (sizeof...(Ts) == 0) {
    static_assert(sizeof...(Args) == 0);
    return input<int>();
  } else if constexpr (1 < sizeof...(Ts)) {
    static_assert(sizeof...(Args) == 0);
    return input<std::tuple<Ts...>>();
  } else {
    using T = std::tuple_element_t<0, std::tuple<Ts...>>;
    if constexpr (sizeof...(args)) {
      T x(std::forward<Args>(args)...);
      std::cin >> x;
      return x;
    } else {
      T x;
      std::cin >> x;
      return x;
    }
  }
}

template <class T, int N>
std::array<T, N> input() {
  return input<std::array<T, N>>();
}

template <class... Ts>
void print(Ts&&... xs) {
  std::cout << std::tie(xs...) << '\n';
}

using namespace std;

#define show(...) static_cast<void>(0)
#define for(...) for ([[maybe_unused]] auto&& __VA_ARGS__)
#define lambda(...) [&](auto&&... args) { return __VA_ARGS__; }
#define $1 get<0>(tie(args...))
#define $2 get<1>(tie(args...))
#define ALL(f, r, ...) lambda(f(begin($1), end($1), ##__VA_ARGS__))(r)

namespace std::ranges::views {

#include __BASE_FILE__

}  // namespace std::ranges::views

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  for (_ : views::iota(0, input())) {
    views::solve();
  }
}

#endif  // __INCLUDE_LEVEL__
0