結果

問題 No.808 Kaiten Sushi?
ユーザー lumc_lumc_
提出日時 2019-03-22 23:00:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 503 ms / 2,000 ms
コード長 5,402 bytes
コンパイル時間 1,156 ms
コンパイル使用メモリ 119,972 KB
実行使用メモリ 31,748 KB
最終ジャッジ日時 2023-10-19 10:13:36
合計ジャッジ時間 10,394 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 4 ms
4,348 KB
testcase_08 AC 5 ms
4,348 KB
testcase_09 AC 7 ms
4,368 KB
testcase_10 AC 6 ms
4,348 KB
testcase_11 AC 5 ms
4,348 KB
testcase_12 AC 6 ms
4,348 KB
testcase_13 AC 5 ms
4,348 KB
testcase_14 AC 3 ms
4,348 KB
testcase_15 AC 7 ms
4,348 KB
testcase_16 AC 6 ms
4,348 KB
testcase_17 AC 3 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 99 ms
14,440 KB
testcase_24 AC 79 ms
12,312 KB
testcase_25 AC 85 ms
13,760 KB
testcase_26 AC 79 ms
13,204 KB
testcase_27 AC 280 ms
26,828 KB
testcase_28 AC 74 ms
10,724 KB
testcase_29 AC 273 ms
25,640 KB
testcase_30 AC 166 ms
17,968 KB
testcase_31 AC 298 ms
27,916 KB
testcase_32 AC 340 ms
31,152 KB
testcase_33 AC 162 ms
17,816 KB
testcase_34 AC 189 ms
20,020 KB
testcase_35 AC 238 ms
23,480 KB
testcase_36 AC 158 ms
17,560 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 2 ms
4,348 KB
testcase_39 AC 422 ms
29,240 KB
testcase_40 AC 76 ms
9,868 KB
testcase_41 AC 79 ms
10,604 KB
testcase_42 AC 317 ms
24,828 KB
testcase_43 AC 108 ms
12,912 KB
testcase_44 AC 254 ms
20,012 KB
testcase_45 AC 106 ms
12,924 KB
testcase_46 AC 55 ms
8,824 KB
testcase_47 AC 503 ms
31,748 KB
testcase_48 AC 495 ms
31,748 KB
testcase_49 AC 494 ms
31,748 KB
testcase_50 AC 494 ms
31,748 KB
testcase_51 AC 495 ms
31,748 KB
testcase_52 AC 190 ms
23,628 KB
testcase_53 AC 269 ms
31,128 KB
testcase_54 AC 2 ms
4,348 KB
testcase_55 AC 2 ms
4,348 KB
testcase_56 AC 2 ms
4,348 KB
testcase_57 AC 80 ms
13,368 KB
testcase_58 AC 132 ms
18,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// includes {{{
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<tuple>
#include<cmath>
#include<random>
#include<cassert>
#include<bitset>
#include<cstdlib>
// #include<deque>
// #include<multiset>
// #include<cstring>
// #include<bits/stdc++.h>
// }}}
using namespace std;
using ll = long long;

// #undef DEBUG
// #define DEBUG
// DEBUG {{{
#include <array>
#include <deque>
#include <list>
#include <ostream>
#include <queue>
#include <stack>
#include <tuple>
#include <valarray>
#include <vector>
template < int n, class... T >
typename std::enable_if< (n >= sizeof...(T)) >::type __output_tuple(
    std::ostream &, std::tuple< T... > const &) {}
template < int n, class... T >
typename std::enable_if< (n < sizeof...(T)) >::type __output_tuple(
    std::ostream &os, std::tuple< T... > const &t) {
  os << (n == 0 ? "" : ", ") << std::get< n >(t);
  __output_tuple< n + 1 >(os, t);
}
template < class... T >
std::ostream &operator<<(std::ostream &os, std::tuple< T... > const &t) {
  os << "(";
  __output_tuple< 0 >(os, t);
  os << ")";
  return os;
}
template < class T, class U >
std::ostream &operator<<(std::ostream &os, std::pair< T, U > const &p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
template < class T >
std::ostream &operator<<(std::ostream &os, const std::stack< T > &a) {
  os << "{";
  for(auto tmp = a; tmp.size(); tmp.pop())
    os << (a.size() == tmp.size() ? "" : ", ") << tmp.top();
  os << "}";
  return os;
}
template < class T, class Container, class Compare >
std::ostream &operator<<(std::ostream &os,
    std::priority_queue< T, Container, Compare > a) {
  os << "{ (top) ";
  while(a.size()) os << a.top() << (a.size() == 1 ? "" : ", "), a.pop();
  os << " }";
  return os;
}
template < class T, class Container >
std::ostream &operator<<(std::ostream &os, std::queue< T, Container > a) {
  os << "{ ";
  while(a.size()) os << a.front() << (a.size() == 1 ? "" : ", "), a.pop();
  os << " }";
  return os;
}
#ifdef DEBUG
#if !defined(DEBUG_OUT)
#define DEBUG_OUT std::cerr
#endif
#define dump(...)                                                                \
  [&]() {                                                                        \
    auto __debug_tap = std::make_tuple(__VA_ARGS__);                             \
    DEBUG_OUT << "[" << __LINE__ << "] " << #__VA_ARGS__ << " = " << __debug_tap \
    << std::endl;                                                      \
  }()
template < class T >
inline void dump2D(T &d, size_t sizey, size_t sizex) {
  for(size_t i = 0; i < sizey; i++) {
    DEBUG_OUT << "\t";
    for(size_t j = 0; j < sizex; j++)
      DEBUG_OUT << d[i][j] << (j + 1 == sizex ? "" : "\t");
    DEBUG_OUT << std::endl;
  }
}
template < class T >
inline void dump1D(T &d, size_t sizey) {
  for(size_t i = 0; i < sizey; i++) {
    DEBUG_OUT << d[i] << (i + 1 == sizey ? "" : " ");
  }
  DEBUG_OUT << std::endl;
}
template <
class T, class = typename std::iterator_traits< decltype(begin(T())) >::value_type,
      class = typename std::enable_if< !std::is_same< T, std::string >::value >::type >
      std::ostream &operator<<(std::ostream &os, const T &a) {
        os << "{";
        for(auto ite = begin(a); ite != end(a); ++ite)
          os << (ite == begin(a) ? "" : ", ") << *ite;
        os << "}";
        return os;
      }
#else
#define dump(...) (42)
#define dump2D(...) (42)
#define dump1D(...) (42)
template <
class T, class = typename std::iterator_traits< decltype(begin(T())) >::value_type,
      class = typename std::enable_if< !std::is_same< T, std::string >::value >::type >
      std::ostream &operator<<(std::ostream &os, const T &a) {
        for(auto ite = begin(a); ite != end(a); ++ite)
          os << (ite == begin(a) ? "" : " ") << *ite;
        return os;
      }
#endif
// }}}

// int x[112345];
// int y[112345];

ll n, l;

int main() {
  std::ios::sync_with_stdio(false), std::cin.tie(0);
  cin >> n >> l;
  ll xx, yy;
  set<ll> x, y;
  for(int i = 0; i < n; i++) cin >> xx, x.insert(xx), x.insert(xx + l), x.insert(xx + l + l);
  for(int i = 0; i < n; i++) cin >> yy, y.insert(yy), y.insert(yy + l), y.insert(yy + l + l);

  ll now = 0;
  ll ans = 0;
  ll nex = *y.lower_bound(*x.begin());
  for(int i = 0; i < n; i++) {
    dump(x, y);
    assert(now < l);
    assert(now <= nex);
    {
      auto ite0 = x.lower_bound(now);
      auto ite = x.lower_bound(nex);
      if(ite0 == ite) {
        ans += *ite - now;
        now = *ite;
      } else {
        ite--;
        ans += *ite - now;
        now = *ite;
      }
      while(now >= l) now -= l;
      x.erase(now);
      x.erase(now + l);
      x.erase(now + l + l);
      if(x.size() && y.size()) nex = *x.lower_bound(*y.lower_bound(now));
      else nex = now;
      if(nex - now > l) nex -= l;
    }

    {
      auto ite0 = y.lower_bound(now);
      auto ite = y.lower_bound(nex);
      if(ite0 == ite) {
        ans += *ite - now;
        now = *ite;
      } else {
        ite--;
        ans += *ite - now;
        now = *ite;
      }
      while(now >= l) now -= l;
      y.erase(now);
      y.erase(now + l);
      y.erase(now + l + l);
      if(x.size() && y.size()) nex = *y.lower_bound(*x.lower_bound(now));
      else nex = now;
      if(nex - now > l) nex -= l;
    }
  }

  cout << ans << endl;
  return 0;
}
0