답안 #1103703

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1103703 2024-10-21T14:42:24 Z MongHwa Palembang Bridges (APIO15_bridge) C++17
100 / 100
265 ms 14368 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;

#define ll long long

struct A {
    ll sum, s, t;

    bool operator< (A& other) {
        return sum < other.sum;
    }
};

vector<A> arr;
multiset<int> lo1, hi1, lo2, hi2;

void Ins(multiset<int>& lo, multiset<int>& hi, ll& losum, ll& hisum, int val, int sz)
{
    if(!lo.empty() && *lo.rbegin() < val)
    {
        hisum += val;
        hi.insert(val);
    }
    else
    {
        losum += val;
        lo.insert(val);
    }
    
    int mid = sz/2 + (sz%2 > 0);
    if(!hi.empty() && hi.size() > sz/2)
    {
        losum += *hi.begin();
        hisum -= *hi.begin();
        lo.insert(*hi.begin());
        hi.erase(hi.begin());
    }
    else if(!lo.empty() && lo.size() > mid)
    {
        losum -= *lo.rbegin();
        hisum += *lo.rbegin();
        hi.insert(*lo.rbegin());
        lo.erase(prev(lo.end()));
    }
}

void Del(multiset<int>& lo, multiset<int>& hi, ll& losum, ll& hisum, int val, int sz)
{
    if(lo.find(val) != lo.end())
    {
        losum -= val;
        lo.erase(lo.find(val));
    }
    else
    {
        hisum -= val;
        hi.erase(hi.find(val));
    }
    
    int mid = sz/2 + (sz%2 > 0);
    if(!hi.empty() && hi.size() > sz/2)
    {
        losum += *hi.begin();
        hisum -= *hi.begin();
        lo.insert(*hi.begin());
        hi.erase(hi.begin());
    }
    else if(!lo.empty() && lo.size() > mid)
    {
        losum -= *lo.rbegin();
        hisum += *lo.rbegin();
        hi.insert(*lo.rbegin());
        lo.erase(prev(lo.end()));
    }
}

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

    int k, n;
    cin >> k >> n;

    ll ans = 0;
    ll losum1 = 0, hisum1 = 0, losum2 = 0, hisum2 = 0;
    for(int i = 0; i < n; i++)
    {
        char p, q; int s, t;
        cin >> p >> s >> q >> t;

        if(p == q)
            ans += abs(s-t);
        else
        {
            arr.push_back({s+t, s, t});
            Ins(lo2, hi2, losum2, hisum2, s, arr.size()*2);
            Ins(lo2, hi2, losum2, hisum2, t, arr.size()*2);
        }
    }

    if(arr.empty())
    {
        cout << ans << '\n';
        return 0;
    }

    if(arr.size() == 1 || k == 1)
    {
        ll mid = *lo2.rbegin();
        cout << ans + (mid*lo2.size() - losum2) + (hisum2 - mid*hi2.size()) + arr.size() << '\n';

        return 0;
    }

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

    ll val = 1e18;
    for(int i = 0; i < (int)arr.size()-1; i++)
    {
        Ins(lo1, hi1, losum1, hisum1, arr[i].s, (i+1)*2);
        Ins(lo1, hi1, losum1, hisum1, arr[i].t, (i+1)*2);
        
        Del(lo2, hi2, losum2, hisum2, arr[i].s, arr.size()*2 - (i+1)*2);
        Del(lo2, hi2, losum2, hisum2, arr[i].t, arr.size()*2 - (i+1)*2);

        ll mid = *lo1.rbegin();
        ll elem1 = (mid*lo1.size() - losum1) + (hisum1 - mid*hi1.size());
        mid = *lo2.rbegin();
        ll elem2 = (mid*lo2.size() - losum2) + (hisum2 - mid*hi2.size());

        val = min(val, elem1 + elem2);
    }

    cout << ans + val + arr.size() << '\n';
}

Compilation message

bridge.cpp: In function 'void Ins(std::multiset<int>&, std::multiset<int>&, long long int&, long long int&, int, int)':
bridge.cpp:34:33: warning: comparison of integer expressions of different signedness: 'std::multiset<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   34 |     if(!hi.empty() && hi.size() > sz/2)
      |                       ~~~~~~~~~~^~~~~~
bridge.cpp:41:38: warning: comparison of integer expressions of different signedness: 'std::multiset<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   41 |     else if(!lo.empty() && lo.size() > mid)
      |                            ~~~~~~~~~~^~~~~
bridge.cpp: In function 'void Del(std::multiset<int>&, std::multiset<int>&, long long int&, long long int&, int, int)':
bridge.cpp:64:33: warning: comparison of integer expressions of different signedness: 'std::multiset<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   64 |     if(!hi.empty() && hi.size() > sz/2)
      |                       ~~~~~~~~~~^~~~~~
bridge.cpp:71:38: warning: comparison of integer expressions of different signedness: 'std::multiset<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   71 |     else if(!lo.empty() && lo.size() > mid)
      |                            ~~~~~~~~~~^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 2 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 2 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 50 ms 12004 KB Output is correct
13 Correct 88 ms 12024 KB Output is correct
14 Correct 75 ms 11000 KB Output is correct
15 Correct 52 ms 7164 KB Output is correct
16 Correct 55 ms 12024 KB Output is correct
17 Correct 61 ms 12024 KB Output is correct
18 Correct 60 ms 12096 KB Output is correct
19 Correct 75 ms 12100 KB Output is correct
20 Correct 58 ms 12024 KB Output is correct
21 Correct 62 ms 12024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 508 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 504 KB Output is correct
12 Correct 1 ms 508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 592 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 504 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 2 ms 504 KB Output is correct
14 Correct 2 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 2 ms 508 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 2 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 508 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 508 KB Output is correct
13 Correct 2 ms 336 KB Output is correct
14 Correct 2 ms 336 KB Output is correct
15 Correct 2 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 2 ms 336 KB Output is correct
22 Correct 2 ms 336 KB Output is correct
23 Correct 2 ms 336 KB Output is correct
24 Correct 2 ms 336 KB Output is correct
25 Correct 119 ms 11968 KB Output is correct
26 Correct 230 ms 13052 KB Output is correct
27 Correct 252 ms 13820 KB Output is correct
28 Correct 258 ms 14336 KB Output is correct
29 Correct 265 ms 14332 KB Output is correct
30 Correct 112 ms 7772 KB Output is correct
31 Correct 126 ms 13672 KB Output is correct
32 Correct 138 ms 14368 KB Output is correct
33 Correct 116 ms 13992 KB Output is correct
34 Correct 143 ms 14332 KB Output is correct
35 Correct 112 ms 14076 KB Output is correct
36 Correct 132 ms 14076 KB Output is correct
37 Correct 165 ms 12796 KB Output is correct