답안 #699351

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
699351 2023-02-16T15:53:36 Z CatalinT Palembang Bridges (APIO15_bridge) C++17
49 / 100
2000 ms 27320 KB
#include <vector>
#include <iostream>
#include <cassert>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <set>
#include <map>
#include <string>
#include <unordered_map>
#include <functional>
#include <bitset>
#include <sstream>

using namespace std;

using int64 = long long;

int N, M, K;
vector<pair<int64, int64>> segments;

vector<int64> X;
vector<int64> vec;

map<int64, int> idx;

int64 dist(int64 x, pair<int64, int64> & s) {
    if (s.second < x) {
        return 2 * (x - s.second); 
    } else if (s.second >= x && s.first <= x) {
        return 0;
    } else {
        return 2 * (s.first - x);
    }
}

int64 solve1brute() {
    int64 res = (1LL<<60);
    for (auto & x : vec) {
        int64 cur = 0;
        for (auto & s : segments) {
            cur += dist(x, s);
        }
        res = min(res, cur);
    }
    return res;
}

// optimum right bridge for left bridges in [l, r] is
// in [optl, optr]

vector<int64> dp;

void rec(int l, int r, int optl, int optr) {
    if (l > r)
        return;
    
    int m = (l + r) >> 1;

    int opt = -1;
    int64 bst = (1LL<<60);

    for (int i = max(m + 1, optl); i <= optr; i++) {
        // Compute cost(m, opt)

        int64 cur = 0;
        for (auto & s: segments) {
            cur += min(dist(vec[m], s), dist(vec[i], s));
        }

        if (cur < bst) {
            bst = cur;
            opt = i;
        }
    }

    assert(opt != -1);

    dp[m] = bst;

    rec(l, m - 1, optl, opt);
    rec(m + 1, r, opt, optr);
}

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

    int64 res = 0;

    cin >> K >> N;

    set<int64> uniq;

    for (int i = 0; i < N; i++) {
        char z1, z2;
        int x1, x2;

        cin >> z1 >> x1 >> z2 >> x2;

        res += abs(x1 - x2);

        if (z1 != z2) {
            segments.emplace_back(min(x1, x2), max(x1, x2));
            uniq.insert(x1);
            uniq.insert(x2);
        }
    }

    uniq.insert(-(1<<30));
    uniq.insert((1LL<<31));

    if (!size(segments)) {
        cout << res << "\n";
        return 0;
    }

    vec = vector<int64>(begin(uniq), end(uniq));
    N = size(vec);

    for (int i = 0; i < N; i++) {
        idx[vec[i]] = i;
    }

    M = size(segments);
    res += M; // every crossed a bridge

    // need at least one bridge;

    dp.assign(N + 2, (1LL<<60));

    rec(0, N - 2, 1, N - 1);

    if (K == 1) {
        cout << res + dp[0] << "\n";
    } else {
        cout << res + (*min_element(begin(dp) + 1, begin(dp) + N - 2)) << "\n";
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 46 ms 592 KB Output is correct
5 Correct 48 ms 576 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 36 ms 716 KB Output is correct
8 Correct 33 ms 596 KB Output is correct
9 Correct 46 ms 588 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 38 ms 592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 49 ms 600 KB Output is correct
5 Correct 42 ms 476 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 35 ms 608 KB Output is correct
8 Correct 26 ms 596 KB Output is correct
9 Correct 37 ms 468 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 35 ms 520 KB Output is correct
12 Correct 19 ms 2752 KB Output is correct
13 Execution timed out 2007 ms 27320 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 320 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 320 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 340 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 320 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 324 KB Output is correct
14 Correct 3 ms 340 KB Output is correct
15 Correct 43 ms 596 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 7 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 37 ms 604 KB Output is correct
21 Correct 27 ms 588 KB Output is correct
22 Correct 40 ms 596 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 38 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 320 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 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 316 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 3 ms 332 KB Output is correct
15 Correct 43 ms 596 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 6 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 34 ms 596 KB Output is correct
21 Correct 27 ms 552 KB Output is correct
22 Correct 45 ms 596 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 38 ms 596 KB Output is correct
25 Correct 18 ms 3272 KB Output is correct
26 Correct 368 ms 3272 KB Output is correct
27 Execution timed out 2049 ms 26340 KB Time limit exceeded
28 Halted 0 ms 0 KB -