結果

問題 No.2248 max(C)-min(C)
ユーザー HIcoderHIcoder
提出日時 2023-07-12 22:32:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 483 ms / 3,000 ms
コード長 2,237 bytes
コンパイル時間 1,272 ms
コンパイル使用メモリ 115,556 KB
実行使用メモリ 37,912 KB
最終ジャッジ日時 2023-10-12 12:29:04
合計ジャッジ時間 16,674 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,372 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 1 ms
4,372 KB
testcase_03 AC 2 ms
4,372 KB
testcase_04 AC 2 ms
4,368 KB
testcase_05 AC 2 ms
4,368 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 5 ms
4,372 KB
testcase_08 AC 4 ms
4,372 KB
testcase_09 AC 4 ms
4,372 KB
testcase_10 AC 2 ms
4,368 KB
testcase_11 AC 4 ms
4,372 KB
testcase_12 AC 3 ms
4,368 KB
testcase_13 AC 4 ms
4,372 KB
testcase_14 AC 4 ms
4,376 KB
testcase_15 AC 4 ms
4,368 KB
testcase_16 AC 3 ms
4,372 KB
testcase_17 AC 4 ms
4,372 KB
testcase_18 AC 419 ms
32,244 KB
testcase_19 AC 40 ms
6,808 KB
testcase_20 AC 482 ms
36,780 KB
testcase_21 AC 457 ms
36,096 KB
testcase_22 AC 309 ms
28,636 KB
testcase_23 AC 183 ms
16,836 KB
testcase_24 AC 62 ms
9,012 KB
testcase_25 AC 435 ms
35,132 KB
testcase_26 AC 382 ms
31,132 KB
testcase_27 AC 430 ms
34,600 KB
testcase_28 AC 32 ms
6,140 KB
testcase_29 AC 395 ms
30,952 KB
testcase_30 AC 133 ms
15,440 KB
testcase_31 AC 482 ms
36,420 KB
testcase_32 AC 73 ms
9,488 KB
testcase_33 AC 122 ms
12,756 KB
testcase_34 AC 483 ms
36,372 KB
testcase_35 AC 480 ms
36,908 KB
testcase_36 AC 483 ms
36,796 KB
testcase_37 AC 221 ms
20,308 KB
testcase_38 AC 455 ms
36,932 KB
testcase_39 AC 463 ms
37,840 KB
testcase_40 AC 460 ms
36,932 KB
testcase_41 AC 388 ms
31,368 KB
testcase_42 AC 459 ms
37,912 KB
testcase_43 AC 401 ms
33,684 KB
testcase_44 AC 459 ms
37,568 KB
testcase_45 AC 348 ms
30,180 KB
testcase_46 AC 165 ms
16,632 KB
testcase_47 AC 459 ms
37,664 KB
testcase_48 AC 253 ms
36,940 KB
testcase_49 AC 468 ms
37,460 KB
testcase_50 AC 474 ms
36,748 KB
testcase_51 AC 472 ms
37,196 KB
testcase_52 AC 469 ms
37,576 KB
testcase_53 AC 59 ms
9,304 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>(5));

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

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

    vector<ll> mini;
    for(int i=0;i<N;i++){
        for(int j=1;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]の値(-INF), index, 今見ているインデックス
        pq.push(make_pair(C[i][0],make_pair(i,0)));
    }

    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){
        //while(pq.top().first< nowmini){
            
            auto [v,p]=pq.top();
            pq.pop();
            p.second++;
            if(p.second<=4){
            v=C[p.first][p.second];
            pq.push(make_pair(v,p));

            nowmaxi=max(nowmaxi,v);
            }
            
           /*
            auto np=pq.top();
            pq.pop();
            np.second.second++;
            
            np.first=C[np.second.first][np.second.second];
            pq.push(np);

            nowmaxi=max(nowmaxi,np.first);
            */   
        }

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

    cout<<ans<<endl;

    

}
0