답안 #971390

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
971390 2024-04-28T12:54:50 Z HuyAT Palembang Bridges (APIO15_bridge) C++14
100 / 100
148 ms 13688 KB
#include<bits/stdc++.h>
#define newl '\n'

const int MaxN = 1e5 + 10;
const int MaxV = 1e7 + 10;
const long long Inf = 1e18;
const long long Mod = 1e9 + 7;

struct MedianCost{
    std::multiset<int> s,s1;
    int cnt;
    long long t,t1;
    MedianCost() : cnt(0),t(0),t1(0){

    }
    void balance(){
        while((int)s.size() > (cnt + 1) / 2){
            auto it = prev(s.end());
            t -= *it;
            t1 += *it;
            s1.insert(*it);
            s.erase(it);
        }
        while((int)s.size() < (cnt + 1) / 2){
            auto it = s1.begin();
            t += *it;
            t1 -= *it;
            s.insert(*it);
            s1.erase(it);
        }
    }
    void add(int x){
        if(cnt <= 2){
            s.insert(x);
            t += x;
        }else{
            if(x <= *prev(s.end())){
                s.insert(x);
                t += x;
            }else{
                s1.insert(x);
                t1 += x;
            }
        }
        ++cnt;
        balance();
    }
    long long cost(){
        long long mid = *prev(s.end());
        return mid * (long long)s.size() - t + t1 - mid * (long long)s1.size();
    }
};
std::vector<std::pair<int,int>> a{{0,0}};
int k,n;
long long total = 0;

void readData(){
    std::cin >> k >> n;
    for(int i = 1;i <= n;++i){
        char p,q;
        int s,t;
        std::cin >> p >> s >> q >> t;
        if(s > t){
            std::swap(s,t);
        }
        if(p == q){
            total += llabs(s - t);
        }else{
            a.emplace_back(s,t);
            
        }
    }
    std::sort(a.begin(),a.end(),[](const std::pair<int,int> &h,const std::pair<int,int> &h1){
        return (h.first + h.second < h1.first + h1.second);
    });
    n = (int)a.size() - 1;
}
long long first(){
    MedianCost s;
    for(int i = 1;i <= n;++i){
        s.add(a[i].first);
        s.add(a[i].second);
    }
    return s.cost() + total + n;
}
long long solve(){
    if(n == 0){
        return total;
    }
    std::vector<long long> pref(n + 1,0),suff(n + 1,0);
    MedianCost s;
    long long ans = Inf;
    for(int i = 1;i <= n;++i){
        s.add(a[i].first);
        s.add(a[i].second);
        pref[i] = s.cost();
    }
    s = MedianCost();
    for(int i = n;i >= 1;--i){
        s.add(a[i].first);
        s.add(a[i].second);
        suff[i] = s.cost();
    }
    for(int i = 1;i <= n;++i){
        if(i < n && k > 1){
            ans = std::min(ans,pref[i] + suff[i + 1]);
        }
        if(i == n){
            ans = std::min(ans,pref[i]);
        }
    }
    return ans + total + n;
}
int main(){
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);std::cout.tie(nullptr);
    readData();
    std::cout << solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 464 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 600 KB Output is correct
11 Correct 1 ms 348 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 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 604 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 348 KB Output is correct
12 Correct 91 ms 12964 KB Output is correct
13 Correct 147 ms 13688 KB Output is correct
14 Correct 117 ms 11708 KB Output is correct
15 Correct 73 ms 8208 KB Output is correct
16 Correct 77 ms 13004 KB Output is correct
17 Correct 98 ms 13468 KB Output is correct
18 Correct 70 ms 13476 KB Output is correct
19 Correct 102 ms 13256 KB Output is correct
20 Correct 97 ms 13260 KB Output is correct
21 Correct 118 ms 13480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 460 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 1 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 604 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 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 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 604 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 344 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 0 ms 348 KB Output is correct
4 Correct 1 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 452 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 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 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 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 2 ms 348 KB Output is correct
25 Correct 90 ms 12868 KB Output is correct
26 Correct 119 ms 12976 KB Output is correct
27 Correct 135 ms 13132 KB Output is correct
28 Correct 144 ms 13260 KB Output is correct
29 Correct 148 ms 13476 KB Output is correct
30 Correct 64 ms 7236 KB Output is correct
31 Correct 78 ms 13212 KB Output is correct
32 Correct 98 ms 13320 KB Output is correct
33 Correct 59 ms 13220 KB Output is correct
34 Correct 111 ms 13396 KB Output is correct
35 Correct 106 ms 13224 KB Output is correct
36 Correct 112 ms 13408 KB Output is correct
37 Correct 109 ms 12708 KB Output is correct