답안 #756859

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
756859 2023-06-12T10:34:43 Z PanosPask Palembang Bridges (APIO15_bridge) C++14
100 / 100
488 ms 12880 KB
#include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair

using namespace std;

typedef pair<int, int> pi;
typedef long long ll;

struct MedianCost {

    int median = 0;
    multiset<int, greater<int>> smaller;
    multiset<int> larger;
    ll cost = 0;

    void balance(void) {
        if (smaller.size() > larger.size() + 1) {
            larger.insert(median);
            smaller.erase(smaller.begin());

            int new_median = *smaller.begin();

            int change = abs(median - new_median);

            cost += (ll)larger.size() * change;
            cost -= (ll)smaller.size() * change;

            median = new_median;
        }
        else if (larger.size() > smaller.size()) {
            int new_median = *larger.begin();

            int change = abs(median - new_median);

            cost -= (ll)larger.size() * change;
            cost += (ll)smaller.size() * change;

            larger.erase(larger.begin());
            smaller.insert(new_median);
            median = new_median;
        }
    }

    void insert(int a) {
        cost += abs(median - a);

        if (!smaller.empty() && *smaller.begin() >= a)
            smaller.insert(a);
        else
            larger.insert(a);

        balance();
    }

    void erase(int a) {
        cost -= abs(median - a);
        if (larger.find(a) != larger.end())
            larger.erase(larger.find(a));
        else {
            smaller.erase(smaller.find(a));

            int new_median = *smaller.begin();
            int change = abs(median - new_median);

            cost += change * larger.size();
            cost -= change * smaller.size();

            median = new_median;
        }

        balance();
    }
};

int n, k;
vector<pi> v;
ll init_boost;
ll ans = 0;
MedianCost a, b;

bool sumsort(const pi& a, const pi& b)
{
    return a.f + a.s < b.f + b.s;
}

int main(void)
{
    scanf("%d %d", &k, &n);

    for (int i = 0; i < n; i++) {
        char p, q;
        int h, off;

        scanf(" %c %d %c %d", &p, &h, &q, &off);
        if (p == q)
            init_boost += abs(off - h);
        else
            v.push_back(mp(h, off));
    }

    n = v.size();
    sort(v.begin(), v.end(), sumsort);

    for (int i = 0; i < n; i++) {
        a.insert(v[i].f);
        a.insert(v[i].s);
    }

    ans = a.cost;
    if (k == 1) {
        ans += init_boost + n;
        printf("%lld\n", ans);

        return 0;
    }

    for (int i = n - 1; i >= 0; i--) {
        a.erase(v[i].f);
        a.erase(v[i].s);
        b.insert(v[i].f);
        b.insert(v[i].s);

        ans = min(ans, a.cost + b.cost);
    }

    ans += init_boost + n;
    printf("%lld\n", ans);

    return 0;
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:90:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |     scanf("%d %d", &k, &n);
      |     ~~~~~^~~~~~~~~~~~~~~~~
bridge.cpp:96:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |         scanf(" %c %d %c %d", &p, &h, &q, &off);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 1 ms 312 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 312 KB Output is correct
8 Correct 1 ms 312 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 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 2 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 2 ms 312 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 312 KB Output is correct
12 Correct 84 ms 11168 KB Output is correct
13 Correct 133 ms 12748 KB Output is correct
14 Correct 107 ms 10556 KB Output is correct
15 Correct 108 ms 7644 KB Output is correct
16 Correct 83 ms 12080 KB Output is correct
17 Correct 92 ms 12684 KB Output is correct
18 Correct 82 ms 12380 KB Output is correct
19 Correct 112 ms 12804 KB Output is correct
20 Correct 113 ms 12316 KB Output is correct
21 Correct 93 ms 12512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 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 300 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 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 300 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 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 300 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 1 ms 300 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 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 3 ms 340 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 2 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 296 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 1 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 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 300 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
13 Correct 2 ms 340 KB Output is correct
14 Correct 3 ms 304 KB Output is correct
15 Correct 3 ms 340 KB Output is correct
16 Correct 1 ms 296 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 190 ms 11244 KB Output is correct
26 Correct 367 ms 11360 KB Output is correct
27 Correct 421 ms 12092 KB Output is correct
28 Correct 488 ms 12784 KB Output is correct
29 Correct 451 ms 12800 KB Output is correct
30 Correct 165 ms 7008 KB Output is correct
31 Correct 156 ms 12096 KB Output is correct
32 Correct 187 ms 12728 KB Output is correct
33 Correct 144 ms 12304 KB Output is correct
34 Correct 224 ms 12880 KB Output is correct
35 Correct 200 ms 12348 KB Output is correct
36 Correct 187 ms 12556 KB Output is correct
37 Correct 198 ms 11216 KB Output is correct