Submission #705217

#TimeUsernameProblemLanguageResultExecution timeMemory
705217bebraPalembang Bridges (APIO15_bridge)C++17
31 / 100
2079 ms2112 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define dbg(x) cerr << #x << ": " << x << endl;


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int k, n;
    cin >> k >> n;

    ll ans = 0;
    vector<int> coords;
    vector<pair<int, int>> pairs;
    for (int i = 0; i < n; ++i) {
        char c1;
        cin >> c1;
        int x1;
        cin >> x1;

        char c2;
        cin >> c2;
        int x2;
        cin >> x2;

        if (c1 == c2) {
            ans += abs(x1 - x2);
        } else {
            coords.push_back(x1);
            coords.push_back(x2);
            pairs.emplace_back(x1, x2);
            ++ans;
        }
    }
    sort(coords.begin(), coords.end());
    int m = coords.size();

    if (k == 1) {
        for (auto x : coords) {
            ans += abs(x - coords[m / 2]);
        }
    } else {
        const ll INF = 1e17;
        ll mn = INF;
        for (int i = 0; i < m; ++i) {
            for (int j = i; j < m; ++j) {
                ll curr_ans = 0;
                for (const auto& [x1, x2] : pairs) {
                    curr_ans += min(abs(x1 - coords[i]) + abs(x2 - coords[i]), abs(x1 - coords[j]) + abs(x2 - coords[j]));
                }
                mn = min(mn, curr_ans);
            }
        }
        if (mn < INF) {
            ans += mn;
        }
    }
    cout << ans << '\n';
    return 0;
}


/*
1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7


2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...