Submission #332288

# Submission time Handle Problem Language Result Execution time Memory
332288 2020-12-01T22:01:33 Z alien_lover Palembang Bridges (APIO15_bridge) C++14
100 / 100
343 ms 14440 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <climits>

using namespace std;

typedef long long ll;

#define all(x) begin(x), end(x)
#define sz(x) (int) x.size()

const int PRIME = 1e9 + 7;


bool locationCmp(pair<int, int>& a, pair<int, int>& b){
    // close to overflow
    return a.first + a.second < b.first + b.second;
}

template<class T>
vector<ll> calcDist(T start, T end, int size){
    vector<ll> out(size);
    int ind = 0;
    multiset<int> smaller, bigger;
    smaller.insert(start->first); bigger.insert(start->second);
    ll smallSum, bigSum;

    smallSum = start->first; bigSum = start->second;
    out[ind] = bigSum - smallSum;
    start++;
    while(start != end){
        smaller.insert(start->first);
        bigger.insert(start->second);
        smallSum += start->first;
        bigSum += start->second;

        while(*smaller.rbegin() > *bigger.begin()){
            ll sumChange = *bigger.begin() - *smaller.rbegin();
            smallSum += sumChange;
            bigSum -= sumChange;
            bigger.insert(*smaller.rbegin());
            smaller.insert(*bigger.begin());
            bigger.erase(bigger.begin());
            smaller.erase(--smaller.end());
        }

        ind++;
        start++;
        out[ind] = bigSum - smallSum;        
    }
    return out;
}

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

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

    vector<pair<int, int>> paths;
    ll sameSideAns = 0;

    for(int i = 0; i < n; i++){
        char side1, side2;
        int loc1, loc2;
        cin >> side1 >> loc1 >> side2 >> loc2;
        if(side1 == side2){
            sameSideAns += abs(loc1 - loc2);
            continue;
        }
        if(loc1 > loc2){
            swap(loc1, loc2);
        }
        
        paths.push_back({loc1, loc2});
    }

    sort(all(paths), locationCmp);

    if(!sz(paths)){
        cout << sameSideAns << '\n';
        return 0;
    }

    vector<ll> fromLeft = calcDist(paths.begin(), paths.end(), sz(paths));

    if(k == 1){
        cout << sameSideAns + fromLeft[sz(paths)-1] + sz(paths) << '\n';
        return 0;
    }

    vector<ll> fromRight = calcDist(paths.rbegin(), paths.rend(), sz(paths));

    ll bridgeAns = LLONG_MAX;
    for(int i = 0; i < sz(paths) - 1; i++){
        bridgeAns = min(bridgeAns, fromLeft[i] + fromRight[sz(paths) - i - 2]);
    }
    // for(int el: fromLeft){
    //     cout << el << ' ';
    // }
    // cout << '\n';
    // for(int el: fromRight){
    //     cout << el << ' ';
    // }
    // cout << '\n';



    cout << sameSideAns + bridgeAns + sz(paths) << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 2 ms 492 KB Output is correct
10 Correct 2 ms 492 KB Output is correct
11 Correct 2 ms 492 KB Output is correct
12 Correct 79 ms 12136 KB Output is correct
13 Correct 171 ms 13672 KB Output is correct
14 Correct 180 ms 11504 KB Output is correct
15 Correct 86 ms 8208 KB Output is correct
16 Correct 76 ms 12904 KB Output is correct
17 Correct 111 ms 13672 KB Output is correct
18 Correct 84 ms 13288 KB Output is correct
19 Correct 118 ms 13800 KB Output is correct
20 Correct 100 ms 13180 KB Output is correct
21 Correct 116 ms 13416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 2 ms 492 KB Output is correct
15 Correct 2 ms 492 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 1 ms 492 KB Output is correct
20 Correct 2 ms 512 KB Output is correct
21 Correct 2 ms 620 KB Output is correct
22 Correct 2 ms 492 KB Output is correct
23 Correct 2 ms 492 KB Output is correct
24 Correct 3 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 2 ms 492 KB Output is correct
15 Correct 2 ms 492 KB Output is correct
16 Correct 1 ms 364 KB Output is correct
17 Correct 2 ms 364 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 2 ms 492 KB Output is correct
20 Correct 2 ms 620 KB Output is correct
21 Correct 1 ms 492 KB Output is correct
22 Correct 2 ms 492 KB Output is correct
23 Correct 2 ms 492 KB Output is correct
24 Correct 2 ms 512 KB Output is correct
25 Correct 131 ms 12904 KB Output is correct
26 Correct 241 ms 13068 KB Output is correct
27 Correct 343 ms 13964 KB Output is correct
28 Correct 334 ms 14440 KB Output is correct
29 Correct 336 ms 14440 KB Output is correct
30 Correct 161 ms 7820 KB Output is correct
31 Correct 120 ms 13800 KB Output is correct
32 Correct 188 ms 14440 KB Output is correct
33 Correct 116 ms 14184 KB Output is correct
34 Correct 186 ms 14440 KB Output is correct
35 Correct 159 ms 14248 KB Output is correct
36 Correct 217 ms 14312 KB Output is correct
37 Correct 113 ms 12904 KB Output is correct