結果

問題 No.2248 max(C)-min(C)
ユーザー HIcoderHIcoder
提出日時 2023-07-12 22:38:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 502 ms / 3,000 ms
コード長 1,974 bytes
コンパイル時間 1,248 ms
コンパイル使用メモリ 116,080 KB
実行使用メモリ 37,616 KB
最終ジャッジ日時 2023-10-12 12:35:15
合計ジャッジ時間 16,783 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,372 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 3 ms
4,352 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 4 ms
4,352 KB
testcase_07 AC 5 ms
4,348 KB
testcase_08 AC 5 ms
4,352 KB
testcase_09 AC 4 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 4 ms
4,348 KB
testcase_12 AC 3 ms
4,352 KB
testcase_13 AC 3 ms
4,352 KB
testcase_14 AC 4 ms
4,348 KB
testcase_15 AC 5 ms
4,356 KB
testcase_16 AC 3 ms
4,348 KB
testcase_17 AC 4 ms
4,352 KB
testcase_18 AC 412 ms
32,088 KB
testcase_19 AC 42 ms
6,816 KB
testcase_20 AC 493 ms
37,080 KB
testcase_21 AC 466 ms
35,416 KB
testcase_22 AC 305 ms
28,548 KB
testcase_23 AC 182 ms
16,952 KB
testcase_24 AC 65 ms
9,028 KB
testcase_25 AC 453 ms
34,176 KB
testcase_26 AC 386 ms
30,532 KB
testcase_27 AC 439 ms
35,352 KB
testcase_28 AC 33 ms
6,144 KB
testcase_29 AC 403 ms
30,860 KB
testcase_30 AC 144 ms
15,292 KB
testcase_31 AC 502 ms
36,840 KB
testcase_32 AC 75 ms
9,480 KB
testcase_33 AC 129 ms
12,944 KB
testcase_34 AC 494 ms
37,600 KB
testcase_35 AC 492 ms
37,616 KB
testcase_36 AC 497 ms
37,204 KB
testcase_37 AC 235 ms
20,180 KB
testcase_38 AC 470 ms
37,072 KB
testcase_39 AC 471 ms
37,352 KB
testcase_40 AC 469 ms
36,272 KB
testcase_41 AC 401 ms
31,612 KB
testcase_42 AC 466 ms
36,840 KB
testcase_43 AC 399 ms
34,152 KB
testcase_44 AC 462 ms
37,228 KB
testcase_45 AC 353 ms
30,428 KB
testcase_46 AC 166 ms
16,952 KB
testcase_47 AC 466 ms
37,292 KB
testcase_48 AC 274 ms
36,800 KB
testcase_49 AC 477 ms
37,076 KB
testcase_50 AC 486 ms
36,612 KB
testcase_51 AC 484 ms
36,552 KB
testcase_52 AC 483 ms
36,412 KB
testcase_53 AC 61 ms
9,044 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<set>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<numeric>
#include<queue>
#include<cmath>
using namespace std;
typedef long long ll;
const ll INF=1LL<<55;
typedef pair<ll,ll> P;
typedef pair<ll,P> PP;

const ll MOD=998244353;
const double PI=acos(-1);


int main(){
    int N;
    cin>>N;
    vector<ll> A(N),B(N);
    for(int i=0;i<N;i++){
        cin>>A[i];
    }
    for(int i=0;i<N;i++){
        cin>>B[i];
    }   

    vector<vector<ll>> C(N,vector<ll>(4));

    for(int i=0;i<N;i++){
        if(A[i]>B[i]) swap(A[i],B[i]);
        //A[i]<=B[i]
    }

    //C[i][0]<=C[i][1]<=C[i][2]
    for(int i=0;i<N;i++){
        C[i][0]=A[i];
        C[i][1]=(A[i]+B[i])/2;
        C[i][2]=B[i];
        C[i][3]=INF;
    }

    vector<ll> mini;
    for(int i=0;i<N;i++){
        for(int j=0;j<3;j++){
            mini.push_back(C[i][j]);
        }
    }

    sort(mini.begin(),mini.end());//C[i][j]が小さい順に

    priority_queue<PP,vector<PP>,greater<PP>> pq;
    for(int i=0;i<N;i++){
        //C[i][0]の値, index, 今見ている一つ前のインデックス
        pq.push(make_pair(-INF,make_pair(i,-1)));
    }

    ll ans=INF;

    ll nowmaxi=-INF;//現状,出ている値の最大値

    for(ll e:mini){//C[i][j]が小さい順
        ll nowmini=e;

        /*
        pq.top().first<nowmini である限り,
        つまり,nowmini より小さい値が
        queueにあるかぎり,pqから取り出し続ける
        */
        while(!pq.empty() && pq.top().first< nowmini){
            
            auto [v,p]=pq.top();
            pq.pop();
            p.second++;
            if(p.second<4){
                //p.second=0,1,2
                v=C[p.first][p.second];
                pq.push(make_pair(v,p));

                nowmaxi=max(nowmaxi,v);
            }
            
          
        }

        ans=min(ans,nowmaxi-nowmini);
    }

    cout<<ans<<endl;

    

}
0