結果

問題 No.1767 BLUE to RED
ユーザー rogi52rogi52
提出日時 2021-11-26 23:41:27
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 130 ms / 2,000 ms
コード長 1,505 bytes
コンパイル時間 1,855 ms
コンパイル使用メモリ 179,132 KB
実行使用メモリ 21,448 KB
最終ジャッジ日時 2023-09-12 05:39:07
合計ジャッジ時間 5,129 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 64 ms
12,644 KB
testcase_10 AC 77 ms
17,912 KB
testcase_11 AC 68 ms
12,752 KB
testcase_12 AC 66 ms
12,256 KB
testcase_13 AC 84 ms
19,112 KB
testcase_14 AC 127 ms
20,252 KB
testcase_15 AC 128 ms
19,676 KB
testcase_16 AC 130 ms
19,688 KB
testcase_17 AC 127 ms
19,060 KB
testcase_18 AC 127 ms
19,440 KB
testcase_19 AC 129 ms
18,988 KB
testcase_20 AC 127 ms
21,448 KB
testcase_21 AC 128 ms
21,048 KB
testcase_22 AC 128 ms
18,976 KB
testcase_23 AC 127 ms
19,808 KB
権限があれば一括ダウンロードができます

ソースコード

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