結果

問題 No.808 Kaiten Sushi?
ユーザー rpy3cpprpy3cpp
提出日時 2019-03-31 12:50:43
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 136 ms / 2,000 ms
コード長 1,371 bytes
コンパイル時間 2,032 ms
コンパイル使用メモリ 171,668 KB
実行使用メモリ 12,928 KB
最終ジャッジ日時 2024-11-21 12:41:58
合計ジャッジ時間 7,892 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 56
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

auto find_next_core(int p, set<int> &ps){
    auto it_next = ps.upper_bound(p);
    if (it_next == ps.end()) it_next = ps.begin();
    return it_next;
}

auto find_prev_core(int p, set<int> &ps){
    auto it_next = ps.lower_bound(p);
    if (it_next == ps.begin()) it_next = ps.end();
    auto it_prev = prev(it_next);
    return it_prev;
}

int find_and_erase_next(int y0, set<int> &xs, set<int> &ys) {
    auto it_x = find_next_core(y0, xs);
    auto it_y = find_next_core(*it_x, ys);
    auto it_next = find_prev_core(*it_y, xs);
    int next = *it_next;
    xs.erase(it_next);
    return next;
}

ll solve(int N, int L, set<int> &xs, set<int> &ys){
    ll ans = 0;
    int x = 0;
    int y = 0;
    for (int i = 0; i < N; ++i){
        x = find_and_erase_next(y, xs, ys);
        ans += (x > y) ? (x - y):(L + x - y);
        y = find_and_erase_next(x, ys, xs);
        ans += (y > x) ? (y - x):(L + y - x);
    }
    return ans;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, L;
    cin >> N >> L;
    int x, y;
    set<int> xs, ys;
    for (int i = 0; i < N; ++i){
        cin >> x;
        xs.insert(xs.end(), x);
    }
    for (int i = 0; i < N; ++i){
        cin >> y;
        ys.insert(ys.end(), y);
    }
    cout << solve(N, L, xs, ys) << endl;
    return 0;
}
0