結果

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
using ll=long long int;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}



int main(){
    int n,m;
    cin>>n>>m;
    vector<int> a(n),b(m);
    rep(i,0,n)cin>>a[i];
    rep(i,0,m)cin>>b[i];
    
    ll ret=0;
    vector pos(n+1,vector<int>());
    rep(i,0,m){
        int id=lower_bound(ALL(a),b[i])-a.begin();
        pos[id].push_back(b[i]);
    }
    rep(i,0,n+1){
        if(pos[i].empty())continue;
        if(i==0)ret+=a[0]-pos[i].front();
        else if(i==n)ret+=pos[i].back()-a[n-1];
        else{
            int add=min(a[i]-pos[i].front(),pos[i].back()-a[i-1]);
            rep(j,0,pos[i].size()-1)chmin(add,a[i]-a[i-1]-(pos[i][j+1]-pos[i][j]));
            ret+=add;
        }
    }
    cout<<ret<<'\n';
    return 0;
}
0