結果
問題 |
No.2248 max(C)-min(C)
|
ユーザー |
|
提出日時 | 2023-07-12 22:32:37 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 535 ms / 3,000 ms |
コード長 | 2,237 bytes |
コンパイル時間 | 1,117 ms |
コンパイル使用メモリ | 113,816 KB |
最終ジャッジ日時 | 2025-02-15 10:08:18 |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 51 |
ソースコード
#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; }