結果

問題 No.921 ずんだアロー
ユーザー ngtkanangtkana
提出日時 2019-11-08 21:39:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 13 ms / 2,000 ms
コード長 4,186 bytes
コンパイル時間 2,196 ms
コンパイル使用メモリ 208,188 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-15 01:19:51
合計ジャッジ時間 3,231 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 13 ms
5,376 KB
testcase_11 AC 13 ms
5,376 KB
testcase_12 AC 5 ms
5,376 KB
testcase_13 AC 10 ms
5,376 KB
testcase_14 AC 11 ms
5,376 KB
testcase_15 AC 7 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 11 ms
5,376 KB
testcase_18 AC 11 ms
5,376 KB
testcase_19 AC 11 ms
5,376 KB
testcase_20 AC 11 ms
5,376 KB
testcase_21 AC 11 ms
5,376 KB
testcase_22 AC 11 ms
5,376 KB
testcase_23 AC 13 ms
5,376 KB
testcase_24 AC 11 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

  #include <bits/stdc++.h>
  #define loop(n) for (int ngtkana_is_genius = 0; ngtkana_is_genius < int(n); ngtkana_is_genius++)
  #define rep(i, begin, end) for(int i = int(begin); i < int(end); i++)
  #define all(v) v.begin(), v.end()
  #define rand(l, r) std::uniform_int_distribution<>(l, r)(mt)
  using lint = long long;
  auto mt = std::mt19937_64(std::random_device{}());
  auto cmn = [](auto& a, auto b){if (a > b) {a = b; return true;} return false;};
  auto cmx = [](auto& a, auto b){if (a < b) {a = b; return true;} return false;};
  void debug_impl() { std::cerr << std::endl; }
  template <typename Head, typename... Tail>
  void debug_impl(Head head, Tail... tail){
    std::cerr << " " << head;
    debug_impl(tail...);
  }
  #ifdef STOPIT
  #define debug(...) 0
  #else
  #define debug(...)\
  do {\
    std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\
    debug_impl(__VA_ARGS__);\
    std::cerr << std::noboolalpha;\
  } while (false)
  #endif

  template < typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr>
  std::istream& operator>> (std::istream& is, Container& v)
    { for (auto & x : v) { is >> x; } return is; }

  template < typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr >
  std::ostream& operator<< (std::ostream& os, Container const& v) {
  os << "{";
    for (auto it = v.begin(); it != v.end(); it++)
      {os << (it != v.begin() ? "," : "") << *it;}
    return os << "}";
  }

  template < template < typename ... > class Tuple,  typename... Args, std::size_t ... Inds, std::size_t = std::tuple_size< Tuple < Args ... > >::value >
  std::istream& tuple_input_impl(std::istream& os, Tuple<Args...>& tuple, std::integer_sequence<std::size_t, Inds...>)
    { (void)std::initializer_list<int>{((void)(os >> std::get< Inds >(tuple)), 0)...}; return os; }
  template < template < typename ... > class Tuple,  typename... Args, std::size_t = std::tuple_size< Tuple < Args ... > >::value >
  std::istream& operator>> (std::istream& os, Tuple<Args...>& tuple)
    { return tuple_input_impl(os, tuple, std::index_sequence_for<Args...>()); }

  template < template < typename ... > class Tuple,  typename... Args, std::size_t ... Inds, std::size_t = std::tuple_size< Tuple < Args ... > >::value >
  std::ostream& tuple_output_impl(std::ostream& os, const Tuple<Args...>& tuple, std::integer_sequence<std::size_t, Inds...>)
    { os << "("; (void)std::initializer_list<int>{((void)(os << (Inds > 0 ? "," : "") << std::get< Inds >(tuple)), 0)...}; return os << ")"; }
  template < template < typename ... > class Tuple,  typename... Args, std::size_t = std::tuple_size< Tuple < Args ... > >::value >
  std::ostream& operator<< (std::ostream& os, const Tuple<Args...>& tuple)
  { return tuple_output_impl(os, tuple, std::index_sequence_for<Args...>()); }

  template < template < typename ... > class Container, typename Value >
  auto runlength(Container< Value > const& v) {
    auto n = (int)v.size();
    if (n == 0)
      { return Container< std::pair< Value, int > >{}; }
    auto pos = Container< int >{};
    for (auto i = 1; i < n; i++) {
      if (v.at(i - 1) != v.at(i))
        { pos.emplace_back(i); }
    }
    pos.emplace_back(n);
    auto m = (int)pos.size();
    auto len = Container< int >(m);
    std::adjacent_difference(pos.begin(), pos.end(), len.begin());
    auto ret = Container< std::pair< Value, int > >(m);
    for (auto i = 0; i != m; i++)
      { ret.at(i) = {v.at(pos.at(i) - 1), len.at(i)}; }
    return ret;
  }

  int main() {
    std::cin.tie(0); std::cin.sync_with_stdio(false);
    int n; std::cin >> n;
    std::vector< int > a(n); std::cin >> a;
    auto rle = runlength(a);
    // debug(rle);
    int x = 0, y = 0;
    for (auto pair : rle) {
      int z = pair.second;
      int new_x = std::max({x - z - 1, y, y + z - 2});
      int new_y = std::max(x + z, y + z - 1);
      x = new_x;
      y = new_y;
    }
    int ans = std::max(x, y);
    std::cout << ans << std::endl;
    return 0;
  }
0