답안 #1095284

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1095284 2024-10-01T18:10:44 Z dpsaveslives Palembang Bridges (APIO15_bridge) C++17
100 / 100
79 ms 7152 KB
#include <bits/stdc++.h>
#define ll long long
#define f first
#define s second
using namespace std;
const int MAXN = 1e5+5;
bool cmp(pair<int,int> a, pair<int,int> b){
    return a.f+a.s < b.f+b.s;
}
ll X[2][MAXN]; pair<int,int> dif[MAXN];
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int K,N; cin >> K >> N;
    ll ans = 0;
    vector<int> vec; int n = 1;
    for(int i = 0;i<N;++i){
        char p1,p2; int S,T; cin >> p1 >> S >> p2 >> T;
        if(p1 != p2){
            vec.push_back(S); vec.push_back(T);
            dif[n].f = S; dif[n].s = T; ++n; ++ans;
        }
        else{
            ans += abs(S-T);
        }
    }
    --n;
    if(K == 1){
        if(!vec.size()){
            cout << ans << "\n";
            return 0;
        }
        sort(vec.begin(),vec.end());
        int med = vec[vec.size()/2];
        for(int i = 1;i<=n;++i){
            ans += abs(dif[i].f-med)+abs(dif[i].s-med);
        }
        cout << ans << "\n";
    }
    else{
        sort(dif+1,dif+n+1,cmp);
        priority_queue<int,vector<int>,less<int>> pq1; priority_queue<int,vector<int>,greater<int>> pq2; //pq1: decreasing, pq2: increasing
        ll lsum = 0, rsum = 0;
        for(int i = 1;i<=n;++i){
            pq1.push(dif[i].f); pq1.push(dif[i].s);
            lsum += dif[i].f+dif[i].s; rsum += pq1.top(); lsum -= pq1.top();
            pq2.push(pq1.top()); pq1.pop();
            if(pq1.top() > pq2.top()){
                int tmp1 = pq1.top(), tmp2 = pq2.top();
                pq1.pop(); pq2.pop();
                pq1.push(tmp2); pq2.push(tmp1);
                lsum += tmp2-tmp1; rsum -= tmp2-tmp1;
            }
            X[0][i] = rsum-lsum; //distance of the median to everything
        }
        while(!pq1.empty()){
            pq1.pop();
        }
        while(!pq2.empty()){
            pq2.pop();
        }
        lsum = rsum = 0;
        for(int i = n;i>=1;--i){
            pq1.push(dif[i].f); pq1.push(dif[i].s);
            lsum += dif[i].f+dif[i].s; rsum += pq1.top(); lsum -= pq1.top();
            pq2.push(pq1.top()); pq1.pop();
            if(pq1.top() > pq2.top()){
                int tmp1 = pq1.top(), tmp2 = pq2.top();
                pq1.pop(); pq2.pop();
                pq1.push(tmp2); pq2.push(tmp1);
                lsum += tmp2-tmp1; rsum -= tmp2-tmp1;
            }
            X[1][i] = rsum-lsum;
        }
        ll val = 1e18;
        for(int i = 1;i<=n+1;++i){
            val = min(val,X[0][i-1]+X[1][i]); //first bridge for the first i-1, second for the others
        }
		cout << val+ans << "\n";
    }
    /*2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7*/
    return 0;
}
# 결과 실행 시간 메모리 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 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 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 344 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 344 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 0 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 17 ms 2916 KB Output is correct
13 Correct 29 ms 4292 KB Output is correct
14 Correct 21 ms 3288 KB Output is correct
15 Correct 17 ms 2744 KB Output is correct
16 Correct 17 ms 3584 KB Output is correct
17 Correct 19 ms 4392 KB Output is correct
18 Correct 23 ms 4056 KB Output is correct
19 Correct 35 ms 4312 KB Output is correct
20 Correct 18 ms 4044 KB Output is correct
21 Correct 24 ms 4052 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 1 ms 464 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 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 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 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 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 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 344 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 0 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 344 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 428 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 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 1 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 524 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 604 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 352 KB Output is correct
21 Correct 1 ms 472 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 27 ms 5640 KB Output is correct
26 Correct 58 ms 5588 KB Output is correct
27 Correct 67 ms 6468 KB Output is correct
28 Correct 79 ms 7152 KB Output is correct
29 Correct 71 ms 6904 KB Output is correct
30 Correct 36 ms 3908 KB Output is correct
31 Correct 28 ms 6292 KB Output is correct
32 Correct 62 ms 7152 KB Output is correct
33 Correct 45 ms 6612 KB Output is correct
34 Correct 63 ms 7120 KB Output is correct
35 Correct 37 ms 6608 KB Output is correct
36 Correct 62 ms 6812 KB Output is correct
37 Correct 20 ms 5592 KB Output is correct