結果

問題 No.1767 BLUE to RED
ユーザー rogi52
提出日時 2021-11-26 23:41:27
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 130 ms / 2,000 ms
コード長 1,505 bytes
コンパイル時間 1,814 ms
コンパイル使用メモリ 181,964 KB
実行使用メモリ 19,248 KB
最終ジャッジ日時 2024-06-29 18:38:18
合計ジャッジ時間 4,634 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    int N,M; cin >> N >> M;
    vector<pair<int,int>> v;
    const int RED = 1;
    const int BLUE = 0;
    rep(i,N) {
        int a; cin >> a;
        v.push_back({a, RED});
    }
    rep(i,M) {
        int b; cin >> b;
        v.push_back({b, BLUE});
    }

    sort(v.begin(), v.end());
    vector<pair<int,vector<int>>> a;
    a.push_back({v[0].second, vector<int>{v[0].first}});
    for(int i = 1; i < (int)v.size(); i++) {
        if(v[i].second == a.back().first) {
            a.back().second.push_back({v[i].first});
        } else {
            a.push_back({v[i].second, vector<int>{v[i].first}});
        }
    }


    ll ans = 0;
    for(int i = 0; i < (int)a.size(); i++) {
        int color = a[i].first;
        vector<int> ps = a[i].second;
        if(color == RED) continue;

        if(i == 0) {
            ans += a[1].second[0] - ps[0]; continue;
        }

        if(i == a.size() - 1) {
            ans += ps.back() - a[a.size() - 2].second.back(); continue;
        }

        // L, ... ps[0], ps[1], ps[2] ... R
        int L = a[i - 1].second.back();
        int R = a[i + 1].second[0];
        int ma = 0;
        for(int j = 0; j < (int)ps.size() - 1; j++) ma = max(ma, ps[j + 1] - ps[j]);
        ma = max({ma, ps[0] - L, R - ps.back()});
        ans += R - L - ma;
    }

    cout << ans << endl;
}
0