답안 #759040

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
759040 2023-06-15T17:16:20 Z udhavvarma03 Palembang Bridges (APIO15_bridge) C++17
100 / 100
307 ms 13600 KB
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimize("Ofast,unroll-loops")
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define NUM1 1000000007LL
#define all(a) a.begin(), a.end()
#define beg(a) a.begin(), a.begin()
#define sq(a) ((a)*(a))
#define NUM2 998244353LL
#define MOD NUM2
#define LMOD 1000000006LL
#define fi first
#define se second
typedef long double ld;
const ll MAX = 3000100;
const ll INF = 1e9;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
class MedianFind{
private:
    multiset<ll> low, hi;
    ll lowsum = 0, hisum = 0;
    ll cnt = 0;
public:
    void balance(){
        while(low.size() < hi.size()){
            ll v = *hi.begin();
            hi.erase(hi.begin());
            hisum -= v;
            low.insert(v);
            lowsum += v;
        }
        while(low.size() > hi.size() + 1){
            ll v = *low.rbegin();
            low.erase(low.find(v));
            lowsum -= v;
            hi.insert(v);
            hisum += v;
        }
    }
    void insert(ll val){
        cnt++;
        if(low.empty()){
            low.insert(val);
            lowsum += val;
            return;
        }
        else{
            if(val > *low.rbegin()){
                hi.insert(val);
                hisum += val;
            }
            else{
                low.insert(val);
                lowsum += val;
            }
            balance();
            return;
        }
    }
    void remove(ll val){
        --cnt;
        if(low.find(val) != low.end()){
            low.erase(low.find(val));
            lowsum -= val;
        }
        else{
            hi.erase(hi.find(val));
            hisum -= val;
        }
        balance();
    }
    ll findMedian(){
        if(low.empty()) return -1;
        return *low.rbegin();
    }
    ll findval(){
        if(cnt == 0) return 0ll;
        ll med = findMedian();
        return (low.size()*med - lowsum + hisum - (med*hi.size()));
    }
};
 
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll k, n;
    cin >> k >> n;
    ll inval = 0;
    vector<pair<ll, ll>> ed;
    for(ll i = 0; i < n; i++){
        ll s, t;
        char p, q;
        cin >> p >> s >> q >> t;
        if(p == q) inval += abs(s - t);
        else{
            ed.push_back({s, t});
        }
    }
    if(k == 1){
        vector<ll> v;
        for(auto& x: ed){
            v.push_back(x.fi);
            v.push_back(x.se);
        }
        sort(all(v));
        // for(auto& x: v) cerr << x << ' ';
        // cerr << '\n';
        ll k = v.size();
        ll med = v[k/2];
        for(auto& x: v) inval += abs(med - x);
        inval += ed.size();
        cout << inval << '\n';
        exit(0);
    }
    auto cmp = [](pair<ll, ll> a, pair<ll, ll> b){
        return a.fi + a.se < b.fi + b.se;
    };
    sort(all(ed), cmp);
    MedianFind med1, med2;
    n = ed.size();
    for(ll i = 0; i < n; i++){
        med2.insert(ed[i].fi);
        med2.insert(ed[i].se);
    }
    ll minval = med2.findval();
    for(ll i = 0; i < n; i++){
        med1.insert(ed[i].fi);
        med1.insert(ed[i].se);
        med2.remove(ed[i].fi);
        med2.remove(ed[i].se);
        minval = min(minval, med1.findval() + med2.findval());
    }
    cout << minval + inval + ed.size();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 360 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 19 ms 5648 KB Output is correct
13 Correct 46 ms 7224 KB Output is correct
14 Correct 29 ms 5936 KB Output is correct
15 Correct 24 ms 4168 KB Output is correct
16 Correct 23 ms 6596 KB Output is correct
17 Correct 30 ms 7176 KB Output is correct
18 Correct 35 ms 6872 KB Output is correct
19 Correct 42 ms 7212 KB Output is correct
20 Correct 23 ms 6788 KB Output is correct
21 Correct 31 ms 7004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 324 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 324 KB Output is correct
13 Correct 1 ms 368 KB Output is correct
14 Correct 2 ms 332 KB Output is correct
15 Correct 2 ms 336 KB Output is correct
16 Correct 1 ms 324 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 376 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 2 ms 332 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 320 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 332 KB Output is correct
15 Correct 2 ms 464 KB Output is correct
16 Correct 1 ms 328 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 145 ms 11992 KB Output is correct
26 Correct 241 ms 12156 KB Output is correct
27 Correct 235 ms 12892 KB Output is correct
28 Correct 307 ms 13600 KB Output is correct
29 Correct 227 ms 13524 KB Output is correct
30 Correct 104 ms 7280 KB Output is correct
31 Correct 132 ms 12840 KB Output is correct
32 Correct 124 ms 13500 KB Output is correct
33 Correct 102 ms 13172 KB Output is correct
34 Correct 152 ms 13532 KB Output is correct
35 Correct 131 ms 13144 KB Output is correct
36 Correct 134 ms 13232 KB Output is correct
37 Correct 130 ms 12028 KB Output is correct