結果

問題 No.2248 max(C)-min(C)
ユーザー hikariri
提出日時 2023-03-17 22:31:14
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,241 ms / 3,000 ms
コード長 1,776 bytes
コンパイル時間 3,899 ms
コンパイル使用メモリ 242,176 KB
実行使用メモリ 99,456 KB
最終ジャッジ日時 2024-09-18 11:38:52
合計ジャッジ時間 33,955 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 51
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
typedef modint998244353 mint;
#endif
typedef long long  ll;
typedef pair<int,int> pii; typedef pair<ll,ll> pll;
const int INF = 1e9; const long long LINF = 1e18;
const int MOD = 998244353; const int MOD1 = 1e9+7;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(x) x.begin(), x.end()
template<typename T> bool chmin(T& a, T b){if(a > b){a = b; return true;} return false;}
template<typename T> bool chmax(T& a, T b){if(a < b){a = b; return true;} return false;}
void yorn(bool ans){cout << (ans?"Yes":"No") << endl; return;}

int main(){
    // 尺取法
    int n; cin >> n;
    vector<int> a(n),b(n);
    rep(i,n) cin >> a.at(i);
    rep(i,n) cin >> b.at(i);

    map<int,set<int>> m;
    rep(i,n){
        m[a.at(i)].insert(i);
        m[b.at(i)].insert(i);
        m[(a.at(i)+b.at(i))/2].insert(i);
    }

    vector<int> num(n);
    set<int> aru;

    auto l = m.begin();
    auto r = m.begin();
    for(auto i:l->second){
        num.at(i)++;
        aru.insert(i);
    }
    
    //rep(i,n) cout << num.at(i) << ' ';
    ll ans = LINF;
    for(;l!=m.end();l++){
        //int f = i.first;
        //auto s = i.second;
        bool ok = true;
        while((int)aru.size()<n){
            r++;
            if(r==m.end()){
                ok = false; break;
            }
            for(auto j:r->second){
                num.at(j)++;
                aru.insert(j);
            }
        }
        if(!ok) break;
        ans = min(ans,(ll)(r->first)-(ll)(l->first));
        for(auto j:l->second){
            num.at(j)--;
            if(num.at(j)==0) aru.erase(j);
        }
    }
    cout << ans << endl;
    //cout << 999 << endl;
}
0