Submission #1058921

# Submission time Handle Problem Language Result Execution time Memory
1058921 2024-08-14T15:07:31 Z sammyuri Palembang Bridges (APIO15_bridge) C++17
22 / 100
128 ms 15844 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

ll base_cost;
int n, k;
vector<pair<int, int>> paths;

ll check(int firstbridge, bool skip = false) {
    // all paths that start on the left of firstbridge must use it
    // so we only care about paths entirely to the right
    // for each of those paths, keep track of when cost stops decreasing,
    // and when it starts increasing
    ll overhead = 0;
    ll diff = 0;
    map<int, ll> dx;
    for (int i = 0; i < n; i ++) {
        if (paths[i].second < firstbridge || paths[i].first > firstbridge)
            overhead += (ll)abs(firstbridge - paths[i].first) + abs(firstbridge - paths[i].second);
        else
            overhead += (ll)abs(paths[i].second - paths[i].first);
        if (skip || paths[i].first > firstbridge) {
            diff -= 2;
            dx[paths[i].first] += 2;
            dx[paths[i].second] += 2;
        }
    }
    int last = firstbridge;
    ll best = overhead;
    for (auto a : dx) {
        int curdist = a.first, dd = a.second;
        overhead += (ll)(curdist - last) * diff;
        diff += dd;
        best = min(best, overhead);
        // cout << a.first << " " << overhead + base_cost << endl;
        last = curdist;
    }
    return best;
}


int main() {
    cin >> k >> n;
    base_cost = 0;
    for (int i = 0; i < n; i ++) {
        char a, b;
        int x, y;
        cin >> a >> x >> b >> y;
        if (a == b) {
            base_cost += abs(y - x);
        } else {
            base_cost ++;
            paths.push_back({min(x, y), max(x, y)});
        }
    }
    n = paths.size();
    sort(paths.begin(), paths.end());
    if (n == 0) {
        cout << base_cost << endl; return 0;
    }
    if (k == 1) {
        cout << check(0, true) + base_cost << endl; return 0;
    }
    // brute force
    ll best = 1e18;
    for (int i = 0; i < n; i ++) {
        best = min(best, check(paths[i].first, false));
        best = min(best, check(paths[i].second, false));
    }
    cout << best + base_cost << endl; return 0;
}

/*
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 472 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 23 ms 2256 KB Output is correct
13 Correct 128 ms 15808 KB Output is correct
14 Correct 41 ms 2764 KB Output is correct
15 Correct 90 ms 9416 KB Output is correct
16 Correct 37 ms 3020 KB Output is correct
17 Correct 85 ms 15844 KB Output is correct
18 Correct 78 ms 15556 KB Output is correct
19 Correct 113 ms 15296 KB Output is correct
20 Correct 42 ms 3016 KB Output is correct
21 Correct 85 ms 15812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Incorrect 1 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 436 KB Output is correct
4 Incorrect 1 ms 432 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Incorrect 1 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -