結果

問題 No.1767 BLUE to RED
ユーザー umezo
提出日時 2021-11-26 23:55:50
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 98 ms / 2,000 ms
コード長 850 bytes
コンパイル時間 1,991 ms
コンパイル使用メモリ 202,944 KB
最終ジャッジ日時 2025-01-26 02:00:44
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
 
#include<bits/stdc++.h>
using namespace std;

int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  ll n,m;
  cin>>n>>m;
  vector<ll> A(n),B(m);
  rep(i,n) cin>>A[i];
  rep(i,m) cin>>B[i];
  vector<ll> C;
  rep(i,m){
    if(B[i]>A[0] && B[i]<A[n-1]) C.push_back(B[i]);
  }
  ll l=C.size();
  ll ans=0;
  if(B[0]<A[0]) ans+=A[0]-B[0];
  if(B[m-1]>A[n-1]) ans+=B[m-1]-A[n-1];
  
  vector<pair<ll,ll>> P;
  rep(i,n) P.push_back({A[i],0});
  rep(i,l) P.push_back({C[i],1});
  sort(ALL(P));
  
  ll p=P.size();
  ll pre=P[0].first,ma=0;
  for(int i=1;i<p;i++){
    ma=max(ma,P[i].first-P[i-1].first);
    if(P[i].second==0){
      ans+=P[i].first-pre-ma;
      pre=P[i].first;
      ma=0;
    }
  }
  cout<<ans<<endl;
  
  return 0;
}
0