답안 #1054496

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1054496 2024-08-12T10:17:56 Z Mokocraft Palembang Bridges (APIO15_bridge) C++14
100 / 100
98 ms 3652 KB
#include <iostream>
#include <stdio.h>
#include <array>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>

bool sortByMiddle(std::pair<int, int>& a, std::pair<int, int>& b)
{
    return ((a.first + a.second) < (b.first + b.second));
}

int main()
{
    //freopen("duotaA.txt", "r", stdin);

    int K, N;
    std::cin >> K >> N;

    long long extra = 0;
    std::vector<std::pair<int, int>> ranges;
    for(int i = 0; i < N; i++)
    {
        int ai, bi;
        char ac, bc;
        std::cin >> ac >> ai >> bc >> bi;

        if(ai > bi)
            std::swap(ai, bi);

        if(ac == bc)
            extra += bi - ai;
        else
            ranges.push_back(std::make_pair(ai, bi));
    }


    N = ranges.size();

    if(N == 0)
    {
        std::cout << extra;
        return 0;
    }

    if(K == 1)
    {
        long long dist = 0;
        int nums[N*2];
        for(int i = 0; i < N; i++)
        {
            nums[i*2] = ranges[i].first;
            nums[i*2+1] = ranges[i].second;
        }
 
        std::sort(nums, nums + N*2);
 
        int point = nums[N];
 
        for(int i = 0; i < N; i++)
        {
            if(point >= ranges[i].first && point <= ranges[i].second)
                dist += 1 + ranges[i].second - ranges[i].first;
            else dist += 1 + ranges[i].second - ranges[i].first + 2 * std::min(std::abs(ranges[i].second - point), std::abs(ranges[i].first - point));
        }
 
        std::cout << dist + extra;
    }
    else
    {
        std::sort(ranges.begin(), ranges.end(), sortByMiddle);

        long long sumsFromRight[N+1];
        sumsFromRight[0] = 0;
        std::priority_queue<int> qL;
        long long sumQl = 0;
        std::priority_queue<int, std::vector<int>, std::greater<int>> qR;
        long long sumQr = 0;

        for(int i = N; i > 0; i--)
        {
            if(i == N)
            {
                qL.emplace(ranges[i-1].first);
                sumQl = qL.top();
                qR.emplace(ranges[i-1].second);
                sumQr = qR.top();
            }
            else
            {
                if(ranges[i-1].second <= qL.top())
                {
                    sumQl -= qL.top();
                    qR.emplace(qL.top());
                    sumQr += qL.top();
                    qL.pop();
                    sumQl += ranges[i-1].first + ranges[i-1].second;
                    qL.emplace(ranges[i-1].first);
                    qL.emplace(ranges[i-1].second);
                }
                else if(ranges[i-1].first >= qR.top())
                {
                    sumQr -= qR.top();
                    qL.emplace(qR.top());
                    sumQl += qR.top();
                    qR.pop();
                    sumQr += ranges[i-1].first + ranges[i-1].second;
                    qR.emplace(ranges[i-1].first);
                    qR.emplace(ranges[i-1].second);
                }
                else
                {
                    sumQl += ranges[i-1].first;
                    qL.emplace(ranges[i-1].first);
                    sumQr += ranges[i-1].second;
                    qR.emplace(ranges[i-1].second);
                }
            }
            sumsFromRight[N - i + 1] = sumQr - sumQl;
        }

        qL = std::priority_queue<int>();
        qR = std::priority_queue<int, std::vector<int>, std::greater<int>>();
        sumQl = 0;
        sumQr = 0;

        long long dist = sumsFromRight[N];

        for(int i = 0; i < N; i++)
        {
            if(i == 0)
            {
                qL.emplace(ranges[i].first);
                sumQl = qL.top();
                qR.emplace(ranges[i].second);
                sumQr = qR.top();
            }
            else
            {
                if(ranges[i].second <= qL.top())
                {
                    sumQl -= qL.top();
                    qR.emplace(qL.top());
                    sumQr += qL.top();
                    qL.pop();
                    sumQl += ranges[i].first + ranges[i].second;
                    qL.emplace(ranges[i].first);
                    qL.emplace(ranges[i].second);
                }
                else if(ranges[i].first >= qR.top())
                {
                    sumQr -= qR.top();
                    qL.emplace(qR.top());
                    sumQl += qR.top();
                    qR.pop();
                    sumQr += ranges[i].first + ranges[i].second;
                    qR.emplace(ranges[i].first);
                    qR.emplace(ranges[i].second);
                }
                else
                {
                    sumQl += ranges[i].first;
                    qL.emplace(ranges[i].first);
                    sumQr += ranges[i].second;
                    qR.emplace(ranges[i].second);
                }
            }
            dist = std::min(dist, sumQr - sumQl + sumsFromRight[N-i-1]);
        }

        std::cout << dist + extra + N;
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 472 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 24 ms 2500 KB Output is correct
13 Correct 59 ms 3172 KB Output is correct
14 Correct 61 ms 2500 KB Output is correct
15 Correct 34 ms 2264 KB Output is correct
16 Correct 71 ms 3052 KB Output is correct
17 Correct 79 ms 3268 KB Output is correct
18 Correct 47 ms 2964 KB Output is correct
19 Correct 69 ms 3348 KB Output is correct
20 Correct 49 ms 3200 KB Output is correct
21 Correct 58 ms 3008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 424 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 440 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 31 ms 3180 KB Output is correct
26 Correct 43 ms 3652 KB Output is correct
27 Correct 60 ms 3184 KB Output is correct
28 Correct 98 ms 3520 KB Output is correct
29 Correct 71 ms 3596 KB Output is correct
30 Correct 60 ms 1932 KB Output is correct
31 Correct 40 ms 3172 KB Output is correct
32 Correct 97 ms 3176 KB Output is correct
33 Correct 46 ms 3172 KB Output is correct
34 Correct 70 ms 3164 KB Output is correct
35 Correct 68 ms 3504 KB Output is correct
36 Correct 64 ms 3172 KB Output is correct
37 Correct 29 ms 3172 KB Output is correct