Submission #637961

# Submission time Handle Problem Language Result Execution time Memory
637961 2022-09-03T18:04:28 Z BlancaHM Palembang Bridges (APIO15_bridge) C++14
100 / 100
129 ms 6560 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;

long long unPuente(vector<int> & S, vector<int> & T) {
    int N = (int) S.size();
    if (N == 0) return 0;
    vector<int> posiciones(2*N);
    for (int i = 0; i < N; i++) {
        posiciones[2*i] = S[i];
        posiciones[2*i+1] = T[i];
    }
    sort(posiciones.begin(), posiciones.end());
    long long int posicionPuente = (posiciones[N - 1] + posiciones[N])/2;
    long long int total = (long long int) N;
    for (int i = 0; i < N; i++) {
        total += abs(S[i] - posicionPuente);
        total += abs(T[i] - posicionPuente);
    }
    return total;
}

bool comparador(pair<int, int> a, pair<int, int> b) {
    return a.first + a.second < b.first + b.second;
}

long long int sumaIzq, sumaDer;

void incluirEnLaMediana(int a, priority_queue<int> & pqIzq,
    priority_queue<int, vector<int>, greater<int>> & pqDer) {
    if (pqIzq.empty() || a <= pqIzq.top()) {
        pqIzq.push(a);
        sumaIzq += a;
    } else {
        pqDer.push(a);
        sumaDer += a;
    }
    if (pqIzq.size() > pqDer.size() + 1) {
        int valor = pqIzq.top();
        pqIzq.pop();
        sumaIzq -= valor;
        pqDer.push(valor);
        sumaDer += valor;
    } else if (pqIzq.size() < pqDer.size()) {
        int valor = pqDer.top();
        pqIzq.push(valor);
        sumaIzq += valor;
        pqDer.pop();
        sumaDer -= valor;
    }
}

long long int dosPuentes(vector<int> & S, vector<int> & T) {
    int N = (int) S.size();
    if (N == 0) return 0;
    vector<pair<int, int>> posiciones(N);
    for (int i = 0; i < N; i++) {
        posiciones[i] = make_pair(S[i], T[i]);
    }
    sort(posiciones.begin(), posiciones.end(), comparador);
    priority_queue<int> pqIzq;
    priority_queue<int, vector<int>, greater<int>> pqDer;
    sumaIzq = 0, sumaDer = 0;
    vector<long long int> prefSumas(N);
    for (int i = 0; i < N; i++) {
        incluirEnLaMediana(posiciones[i].first, pqIzq, pqDer);
        incluirEnLaMediana(posiciones[i].second, pqIzq, pqDer);
        prefSumas[i] = sumaDer - sumaIzq;
    }
    long long int mejorPosible = prefSumas[N-1];
    pqIzq = priority_queue<int>();
    pqDer = priority_queue<int, vector<int>, greater<int>>();
    sumaIzq = 0;
    sumaDer = 0;
    for (int i = N-1; i > 0; i--) {
        incluirEnLaMediana(posiciones[i].first, pqIzq, pqDer);
        incluirEnLaMediana(posiciones[i].second, pqIzq, pqDer);
        mejorPosible = min(mejorPosible, sumaDer - sumaIzq + prefSumas[i-1]);
    }
    incluirEnLaMediana(posiciones[0].first, pqIzq, pqDer);
    incluirEnLaMediana(posiciones[0].second, pqIzq, pqDer);
    mejorPosible = min(mejorPosible, sumaDer - sumaIzq);
    return mejorPosible+N;
}

int main() {
    int K, N;
    cin >> K >> N;
    vector<int> S, T;
    long long int mismoLado = 0;
    for (int i = 0; i < N; i++) {
        char lado1, lado2;
        int pos1, pos2;
        cin >> lado1 >> pos1 >> lado2 >> pos2;
        if (lado1 == lado2) {
            mismoLado += abs(pos1-pos2);
        } else {
            S.push_back(pos1);
            T.push_back(pos2);
        }
    }
    if (K == 1) {
        cout << mismoLado + unPuente(S, T) << '\n';
    } else {
        cout << mismoLado + dosPuentes(S, T) << '\n';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 2 ms 256 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 2 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 53 ms 2032 KB Output is correct
13 Correct 102 ms 2024 KB Output is correct
14 Correct 66 ms 1884 KB Output is correct
15 Correct 57 ms 1344 KB Output is correct
16 Correct 70 ms 1964 KB Output is correct
17 Correct 89 ms 2020 KB Output is correct
18 Correct 85 ms 1984 KB Output is correct
19 Correct 100 ms 2112 KB Output is correct
20 Correct 77 ms 1952 KB Output is correct
21 Correct 89 ms 2008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 304 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 292 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 312 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 300 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 1 ms 300 KB Output is correct
17 Correct 1 ms 304 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 312 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 308 KB Output is correct
25 Correct 59 ms 4704 KB Output is correct
26 Correct 77 ms 4828 KB Output is correct
27 Correct 119 ms 5620 KB Output is correct
28 Correct 129 ms 6560 KB Output is correct
29 Correct 127 ms 6160 KB Output is correct
30 Correct 68 ms 3488 KB Output is correct
31 Correct 84 ms 5688 KB Output is correct
32 Correct 118 ms 6248 KB Output is correct
33 Correct 127 ms 5952 KB Output is correct
34 Correct 121 ms 6220 KB Output is correct
35 Correct 117 ms 5948 KB Output is correct
36 Correct 111 ms 5948 KB Output is correct
37 Correct 54 ms 4628 KB Output is correct