답안 #162281

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
162281 2019-11-07T12:14:30 Z rama_pang Palembang Bridges (APIO15_bridge) C++14
100 / 100
588 ms 18508 KB
#include <bits/stdc++.h>
using namespace std;
using lint = long long;

lint solve(lint K, vector<pair<lint, lint>> A) {
    if (A.empty()) 
        return 0;

    lint N = A.size();
    vector<lint> points;
    sort(A.begin(), A.end(), [&](pair<lint, lint> l, pair<lint, lint> r) {
        return l.first + l.second < r.first + r.second;
    });

    auto running_median = [&](vector<pair<lint, lint>> A) {
        multiset<lint> sml;
        multiset<lint> big;
        lint sum_sml = 0, sum_big = 0;
        vector<lint> res(N + 1, 0);

        for (int i = 1; i <= N; i++) {
            sml.emplace(A[i - 1].first);
            sum_sml += A[i - 1].first;
            big.emplace(A[i - 1].second);
            sum_big += A[i - 1].second;

            while (*sml.rbegin() > *big.begin()) {
                sum_sml += *big.begin() - *sml.rbegin();
                sum_big += *sml.rbegin() - *big.begin();
                big.emplace(*sml.rbegin());
                sml.emplace(*big.begin());
                sml.erase(prev(sml.end()));
                big.erase(big.begin());
            }

            res[i] = sum_big - sum_sml;
        }

        return res;
    };

    vector<lint> prefix_median = running_median(A);
    reverse(A.begin(), A.end());
    vector<lint> suffix_median = running_median(A);
    reverse(A.begin(), A.end());

    lint res = INT64_MAX;

    for (int cur = 0; cur <= N; cur++) {
        res = min(res, prefix_median[cur] + suffix_median[N - cur]);
    }

    if (K == 1)
        res = prefix_median[N];

    return res;
}

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

    lint K, N, ans = 0;
    vector<pair<lint, lint>> A;

    cin >> K >> N;

    for (int i = 0; i < N; i++) {
        char Z1, Z2;
        lint B1, B2;

        cin >> Z1 >> B1 >> Z2 >> B2;
        if (B1 > B2) swap(B1, B2);
        if (Z1 == Z2)
            ans += B2 - B1;
        else
            A.emplace_back(B1, B2);
    }

    cout << ans + A.size() + solve(K, A) << "\n";

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 504 KB Output is correct
4 Correct 4 ms 504 KB Output is correct
5 Correct 3 ms 504 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 3 ms 504 KB Output is correct
8 Correct 3 ms 504 KB Output is correct
9 Correct 4 ms 504 KB Output is correct
10 Correct 3 ms 504 KB Output is correct
11 Correct 3 ms 504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 504 KB Output is correct
4 Correct 4 ms 504 KB Output is correct
5 Correct 4 ms 504 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 3 ms 504 KB Output is correct
8 Correct 3 ms 504 KB Output is correct
9 Correct 4 ms 504 KB Output is correct
10 Correct 3 ms 504 KB Output is correct
11 Correct 4 ms 504 KB Output is correct
12 Correct 186 ms 16712 KB Output is correct
13 Correct 377 ms 16844 KB Output is correct
14 Correct 340 ms 15184 KB Output is correct
15 Correct 167 ms 10420 KB Output is correct
16 Correct 165 ms 16776 KB Output is correct
17 Correct 264 ms 16772 KB Output is correct
18 Correct 134 ms 16776 KB Output is correct
19 Correct 276 ms 16672 KB Output is correct
20 Correct 228 ms 16844 KB Output is correct
21 Correct 277 ms 16876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 3 ms 420 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 3 ms 508 KB Output is correct
14 Correct 3 ms 504 KB Output is correct
15 Correct 4 ms 504 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 3 ms 504 KB Output is correct
18 Correct 3 ms 504 KB Output is correct
19 Correct 3 ms 504 KB Output is correct
20 Correct 4 ms 504 KB Output is correct
21 Correct 3 ms 504 KB Output is correct
22 Correct 4 ms 504 KB Output is correct
23 Correct 3 ms 504 KB Output is correct
24 Correct 4 ms 508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 4 ms 376 KB Output is correct
9 Correct 2 ms 508 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 3 ms 504 KB Output is correct
14 Correct 3 ms 504 KB Output is correct
15 Correct 3 ms 504 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 3 ms 504 KB Output is correct
18 Correct 3 ms 504 KB Output is correct
19 Correct 3 ms 504 KB Output is correct
20 Correct 3 ms 504 KB Output is correct
21 Correct 3 ms 504 KB Output is correct
22 Correct 3 ms 504 KB Output is correct
23 Correct 3 ms 504 KB Output is correct
24 Correct 3 ms 504 KB Output is correct
25 Correct 180 ms 16840 KB Output is correct
26 Correct 292 ms 17076 KB Output is correct
27 Correct 588 ms 18032 KB Output is correct
28 Correct 365 ms 18368 KB Output is correct
29 Correct 346 ms 18508 KB Output is correct
30 Correct 150 ms 9816 KB Output is correct
31 Correct 167 ms 17720 KB Output is correct
32 Correct 266 ms 18380 KB Output is correct
33 Correct 130 ms 17992 KB Output is correct
34 Correct 267 ms 18480 KB Output is correct
35 Correct 340 ms 18000 KB Output is correct
36 Correct 271 ms 18384 KB Output is correct
37 Correct 166 ms 17068 KB Output is correct