답안 #1089764

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1089764 2024-09-17T06:11:52 Z SnowRaven52 Palembang Bridges (APIO15_bridge) C++17
0 / 100
1 ms 348 KB
#include <bits/stdc++.h>

using namespace std;

bool cmp(pair<int, int> a, pair<int, int> b){
    return a.first + a.second < b.first + b.second;
}

priority_queue<int> lpq;
priority_queue<int, vector<int>, greater<int>> rpq;
int lsum, rsum;

void insert(int x){
    int median = (lpq.size() ? lpq.top() : 1000000001);
    if(x <= median){
        lpq.push(x);
        lsum += x;;
    } else {
		rpq.push(x);
		rsum += x;
	}
    if(rpq.size() + 1 < lpq.size()){
        int nxt = lpq.top();
        lpq.pop();
        rpq.push(nxt);
        lsum -= nxt, rsum += nxt;
    } else if(lpq.size() < rpq.size()){
        int nxt = rpq.top();
        rpq.pop();
        lpq.push(nxt);
        rsum -= nxt, lsum += nxt;
    }
}

int pref[100001];

int main() {
	// freopen("main.in", "r", stdin);
	// freopen(".out", "w", stdout);
    int k, n; cin >> k >> n;
    int sames = 0;
    vector<pair<int, int>> v = {{0, 0}};
    for(int i = 0; i < n; i++){
        char a, b; int x, y; cin >> a >> b >> x >> y;
        if(a == b) sames += abs(x - y);
        else v.push_back({x, y});
    }
    sort(v.begin(), v.end(), cmp);
    n = v.size() - 1;
    sames += n;
    lsum = 0;
    rsum = 0;
    for(int i = 1; i <= n; i++){
        insert(v[i].first);
        insert(v[i].second);
        pref[i] = rsum - lsum;
    }
    int ans = pref[n];
    if(k == 2){
        while(lpq.size()) lpq.pop();
        while(rpq.size()) rpq.pop();
        lsum = 0;
        rsum = 0;

        for(int i = n; i > 0; i--){
            insert(v[i].first);
            insert(v[i].second);
            ans = min(ans, rsum - lsum + pref[i - 1]);
        }
    }
    cout << sames + ans;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -