답안 #501977

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
501977 2022-01-05T04:03:21 Z blue Palembang Bridges (APIO15_bridge) C++17
100 / 100
248 ms 12976 KB
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
using namespace std;

using ll = long long;
using vll = vector<ll>;
using pll = pair<ll, ll>;
#define sz(x) int(x.size())
const ll INF = 1'000'000'000'000'000'000LL;





multiset<ll> lo, hi;
ll lo_sum, hi_sum;

void insert_pair(ll u, ll v)
{
    if(max(u, *lo.rbegin()) <= min(v, *hi.begin()))
    {
        lo.insert(u);
        lo_sum += u;

        hi.insert(v);
        hi_sum += v;
    }
    else if(max(*lo.rbegin(), v) <= *hi.begin())
    {
        lo.insert(u);
        lo.insert(v);
        lo_sum += u+v;

        lo_sum -= *lo.rbegin();
        hi_sum += *lo.rbegin();

        hi.insert(*lo.rbegin());
        lo.erase(lo.find(*lo.rbegin()));
    }
    else
    {
        hi.insert(u);
        hi.insert(v);
        hi_sum += u+v;

        hi_sum -= *hi.begin();
        lo_sum += *hi.begin();

        lo.insert(*hi.begin());
        hi.erase(hi.begin());
    }
}



void solve_2()
{
    int N;
    cin >> N;

    ll basicCost = 0;

    // vll points;

    vector<pll> points;

    for(int i = 1; i <= N; i++)
    {
        char Z1, Z2;
        ll P1, P2;
        cin >> Z1 >> P1 >> Z2 >> P2;

        if(Z1 == Z2)
        {
            basicCost += abs(P1 - P2);
        }
        else
        {
            basicCost += 1;
            // points.push_back(P1);
            // points.push_back(P2);
            points.push_back({min(P1, P2), max(P1, P2)});
        }
    }

    sort(points.begin(), points.end(), [] (pll a, pll b)
    {
        return a.first + a.second < b.first + b.second;
    });

    int S = sz(points);

    if(S == 0)
    {
        cout << basicCost << '\n';
        return;
    }



    vll left_cost(1+S);
    left_cost[0] = 0;

    lo.insert(points[0].first);
    lo_sum = points[0].first;

    hi.insert(points[0].second);
    hi_sum = points[0].second;

    left_cost[1] = hi_sum - lo_sum;

    for(int i = 2; i <= S; i++)
    {
        // ll u = points[i-1].first, v = points[i-1].second;
        insert_pair(points[i-1].first, points[i-1].second);

        left_cost[i] = hi_sum - lo_sum;
    }






    lo.clear();
    hi.clear();
    lo_sum = 0;
    hi_sum = 0;

    vll right_cost(1+S);
    right_cost[S] = 0;

    lo.insert(points[S-1].first);
    lo_sum += points[S-1].first;

    hi.insert(points[S-1].second);
    hi_sum += points[S-1].second;

    right_cost[S-1] = hi_sum - lo_sum;

    for(int i = S-2; i >= 0; i--)
    {
        insert_pair(points[i].first, points[i].second);
        right_cost[i] = hi_sum - lo_sum;
    }

    ll ans = INF;
    for(int i = 0; i <= S; i++) ans = min(ans, left_cost[i] + right_cost[i]);

    ans += basicCost;

    cout << ans << '\n';
}

void solve_1()
{
    int N;
    cin >> N;

    ll basicCost = 0;

    vll points;

    for(int i = 1; i <= N; i++)
    {
        char Z1, Z2;
        ll P1, P2;
        cin >> Z1 >> P1 >> Z2 >> P2;

        if(Z1 == Z2)
        {
            basicCost += abs(P1 - P2);
        }
        else
        {
            basicCost += 1;
            points.push_back(P1);
            points.push_back(P2);
        }
    }

    sort(points.begin(), points.end());

    ll ans = 0;
    for(int i = 0; i < sz(points)/2; i++)
        ans -= points[i];
    for(int i = sz(points)/2; i < sz(points); i++)
        ans += points[i];

    cout << ans + basicCost << '\n';
}

int main()
{
    int K;
    cin >> K;

    if(K == 1) solve_1();
    else solve_2();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 324 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 2 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 47 ms 2376 KB Output is correct
13 Correct 99 ms 2428 KB Output is correct
14 Correct 62 ms 2464 KB Output is correct
15 Correct 57 ms 1568 KB Output is correct
16 Correct 69 ms 2360 KB Output is correct
17 Correct 87 ms 2456 KB Output is correct
18 Correct 85 ms 2448 KB Output is correct
19 Correct 99 ms 2468 KB Output is correct
20 Correct 75 ms 2420 KB Output is correct
21 Correct 87 ms 2352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 2 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 2 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 2 ms 332 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 2 ms 420 KB Output is correct
20 Correct 2 ms 332 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 2 ms 332 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
25 Correct 133 ms 12704 KB Output is correct
26 Correct 191 ms 12724 KB Output is correct
27 Correct 222 ms 12740 KB Output is correct
28 Correct 248 ms 12976 KB Output is correct
29 Correct 243 ms 12760 KB Output is correct
30 Correct 110 ms 6816 KB Output is correct
31 Correct 160 ms 12796 KB Output is correct
32 Correct 197 ms 12724 KB Output is correct
33 Correct 141 ms 12720 KB Output is correct
34 Correct 204 ms 12804 KB Output is correct
35 Correct 182 ms 12716 KB Output is correct
36 Correct 194 ms 12912 KB Output is correct
37 Correct 123 ms 12768 KB Output is correct