結果

問題 No.1460 Max of Min
ユーザー Mister
提出日時 2021-04-01 07:40:24
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 83 ms / 2,000 ms
コード長 2,195 bytes
コンパイル時間 1,221 ms
コンパイル使用メモリ 86,512 KB
最終ジャッジ日時 2025-01-20 04:54:40
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 91
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 2 "/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/Tools/heap_alias.hpp"

#include <queue>

template <class T>
using MaxHeap = std::priority_queue<T>;
template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
#line 2 "main.cpp"
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;
using lint = long long;
constexpr lint INF = 1LL << 60;

void solve() {
    int k;
    lint n;
    cin >> k >> n;

    vector<lint> xs(k), ys(k);
    for (auto& x : xs) cin >> x;
    for (auto& y : ys) cin >> y;

    if (n < k) {
        cout << xs[n] << "\n";
        return;
    }

    lint ok = -INF, ng = INF;
    while (ng - ok > 1) {
        auto mid = (ok + ng) / 2;

        vector<int> ts;  // { k-j | ys[j] >= mid }
        for (int j = 0; j < k; ++j) {
            if (ys[j] >= mid) ts.push_back(k - j);
        }
        if (ts.empty()) {
            ng = mid;
            continue;
        }

        auto m = ts.back();  // minimum

        // mod m でDijkstra
        vector<lint> dist(m, INF);
        MinHeap<pair<lint, int>> heap;
        for (int i = 0; i < k; ++i) {
            if (xs[i] < mid) continue;

            // 最初の遷移 (n>=kより少なくとも1度は遷移する)
            for (auto t : ts) {
                auto ni = (i + t) % m;
                auto nd = i + t;
                if (nd < k) continue;

                if (nd < dist[ni]) {
                    dist[ni] = nd;
                    heap.emplace(nd, ni);
                }
            }
        }

        while (!heap.empty()) {
            auto [d, i] = heap.top();
            heap.pop();
            if (d > dist[i]) continue;

            for (auto t : ts) {
                auto ni = (i + t) % m;
                auto nd = d + t;

                if (nd < dist[ni]) {
                    dist[ni] = nd;
                    heap.emplace(nd, ni);
                }
            }
        }

        if (dist[n % m] <= n) {
            ok = mid;
        } else {
            ng = mid;
        }
    }

    cout << ok << "\n";
}

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

    solve();

    return 0;
}
0