結果

問題 No.808 Kaiten Sushi?
ユーザー fooractalfooractal
提出日時 2019-03-23 09:32:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 51 ms / 2,000 ms
コード長 3,316 bytes
コンパイル時間 1,233 ms
コンパイル使用メモリ 112,736 KB
実行使用メモリ 9,520 KB
最終ジャッジ日時 2023-10-21 20:48:01
合計ジャッジ時間 6,259 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
4,348 KB
testcase_08 AC 3 ms
4,348 KB
testcase_09 AC 3 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 3 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 3 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 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 19 ms
4,520 KB
testcase_24 AC 16 ms
4,348 KB
testcase_25 AC 19 ms
5,312 KB
testcase_26 AC 18 ms
5,048 KB
testcase_27 AC 41 ms
7,672 KB
testcase_28 AC 13 ms
4,528 KB
testcase_29 AC 39 ms
7,408 KB
testcase_30 AC 26 ms
5,576 KB
testcase_31 AC 44 ms
9,256 KB
testcase_32 AC 50 ms
7,936 KB
testcase_33 AC 26 ms
5,840 KB
testcase_34 AC 28 ms
6,368 KB
testcase_35 AC 37 ms
7,672 KB
testcase_36 AC 25 ms
6,104 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 2 ms
4,348 KB
testcase_39 AC 45 ms
7,672 KB
testcase_40 AC 11 ms
4,528 KB
testcase_41 AC 14 ms
4,348 KB
testcase_42 AC 38 ms
6,880 KB
testcase_43 AC 17 ms
5,048 KB
testcase_44 AC 29 ms
6,368 KB
testcase_45 AC 17 ms
5,048 KB
testcase_46 AC 11 ms
4,348 KB
testcase_47 AC 50 ms
7,672 KB
testcase_48 AC 51 ms
9,520 KB
testcase_49 AC 50 ms
8,464 KB
testcase_50 AC 50 ms
8,200 KB
testcase_51 AC 51 ms
8,200 KB
testcase_52 AC 26 ms
6,616 KB
testcase_53 AC 35 ms
7,936 KB
testcase_54 AC 2 ms
4,348 KB
testcase_55 AC 2 ms
4,348 KB
testcase_56 AC 3 ms
4,348 KB
testcase_57 AC 12 ms
4,348 KB
testcase_58 AC 18 ms
4,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <numeric>
#include <string>
#include <sstream>
#include <cmath>
#include <cstring>
#include <assert.h>
#include <utility>
#include <tuple>
#include <array>
#include <bitset>
#include <cstdlib>

using int64 = long long;

template <typename T = int>
struct Range {
    struct RangeIterator {
        T i, step;

        RangeIterator(T i, T step): i{i}, step{step} {}

        T& operator*() { return i; }
        RangeIterator& operator++() {
            i += step;
            return *this;
        }
        bool operator!=(const RangeIterator& rhs) const {
            return (step > 0 && i < rhs.i) || (step < 0 && i > rhs.i);
        }
    };

    T start, stop, step;

    Range(T start, T stop, T step): start{start}, stop{stop}, step{step} {}
    Range(T start, T stop): Range(start, stop, 1) {}
    Range(T stop): Range(0, stop, 1) {}
    
    RangeIterator begin() const { return RangeIterator(start, step); }
    RangeIterator end() const { return RangeIterator(stop, step); }
};

struct Item {
    int x;
    int type;

    Item(int x, int type): x{x}, type{type} {}

    bool operator<(const Item& rhs) const {
        return x < rhs.x;
    }
};

static int N;
static int64 L;

int64 solveSpecialProblem(std::vector<Item>& items) {
    std::vector<int> d(items.size(), 0);

    for (const int i : Range<>(items.size())) {
        d[i] += items[i].type;
        if (i > 0) d[i] += d[i - 1];
    }

    int m = *std::max_element(d.begin(), d.end());
    int p = std::max_element(d.rbegin(), d.rend()) - d.rbegin();

    return (m - 1) * L + items[items.size() - p].x;
}

int64 solveProblem(std::vector<Item>& items) {
    std::vector<int> d(items.size(), 0);

    for (const int i : Range<>(items.size())) {
        d[i] += items[i].type;
        if (i > 0) d[i] += d[i - 1];
    }

    auto itr = std::min_element(d.begin(), d.end());

    int64 res = 0;

    if (*itr < 0) {
        int pos = itr - d.begin();
        res = items[pos + 1].x;

        // [0, pos)に寿司があれば
        std::vector<Item> new_items(items.begin() + pos + 1, items.end());

        bool sushi = false;
        for (const int i : Range<>(pos)) {
            if (!sushi && items[i].type == 1) {
                sushi = true;
            } else {
                new_items.push_back(items[i]);
            }
        }
        if (!sushi) new_items.push_back(items[pos]);

        // new_itemsの距離をresを用いて更新
        for (Item& item : new_items) {
            item.x = (item.x - items[pos + 1].x + L) % L;
        }
        items = new_items;
    }
    res += solveSpecialProblem(items);
    return res;
}

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

    std::cin >> N >> L;

    std::vector<Item> items;

    for (const int i : Range<>(N)) {
        int x;
        std::cin >> x;
        items.emplace_back(x, 1);
    }

    for (const int i : Range<>(N)) {
        int x;
        std::cin >> x;
        items.emplace_back(x, -1);
    }

    std::sort(items.begin(), items.end());

    std::cout << solveProblem(items) << std::endl;

    return 0;
}
0