結果

問題 No.2248 max(C)-min(C)
ユーザー eve__fuyukieve__fuyuki
提出日時 2024-04-09 12:41:13
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 341 ms / 3,000 ms
コード長 1,288 bytes
コンパイル時間 3,147 ms
コンパイル使用メモリ 216,380 KB
実行使用メモリ 22,688 KB
最終ジャッジ日時 2024-10-01 17:13:42
合計ジャッジ時間 15,777 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 51
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
void fast_io() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
}

int main() {
    fast_io();
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    vector<vector<int>> c(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> b[i];
        c[i].push_back(a[i]);
        c[i].push_back(b[i]);
        c[i].push_back((a[i] + b[i]) / 2);
    }
    const int INF = 2e9;
    vector<int> vals;
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
    int ma = -INF;
    for (int i = 0; i < n; i++) {
        sort(c[i].begin(), c[i].end());
        for (int j : c[i]) {
            vals.push_back(j);
        }
        c[i].push_back(INF);
        pq.push({c[i][0], i});
        ma = max(ma, c[i][0]);
    }
    sort(vals.begin(), vals.end());
    vals.erase(unique(vals.begin(), vals.end()), vals.end());
    vector<int> idx(n, 0);
    int ans = INF;
    for (int mi : vals) {
        while (pq.top().first < mi) {
            int i = pq.top().second;
            pq.pop();
            idx[i]++;
            pq.push({c[i][idx[i]], i});
            ma = max(ma, c[i][idx[i]]);
        }
        ans = min(ans, ma - mi);
    }
    cout << ans << endl;
}
0