답안 #474787

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
474787 2021-09-19T18:54:31 Z MohammadAghil Palembang Bridges (APIO15_bridge) C++14
100 / 100
415 ms 12800 KB
#include <bits/stdc++.h> 
using namespace std;
typedef long long ll;
typedef pair<int, int> pp;
#define rep(i,l,r) for(int i = (l); i < r; i++)
#define per(i,r,l) for(int i = (r); i >= l; i--)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define pb push_back
#define ff first
#define ss second 
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const ll mod = 1e9 + 7, maxn = 3e5 + 5, inf = 1e9 + 5;

struct Box{
    multiset<int> low, up;
    ll sup, slow;
    Box(){
        sup = slow = 0;
    }
    void upd(){
        if(sz(low) > sz(up) + 1){
            int t = *prev(low.end());
            slow -= t, sup += t;
            up.insert(t);
            low.erase(prev(low.end()));
        }
        if(sz(up) > sz(low)){
            int t = *up.begin();
            slow += t, sup -= t;
            low.insert(t);
            up.erase(up.begin());
        }
    }
    void insert(int x){
        if(sz(up) && x >= *up.begin()){
            up.insert(x);
            sup += x;
        }else{
            low.insert(x);
            slow += x;
        }
        upd();
    }
    void erase(int x){
        if(sz(up) && x >= *up.begin()){
            up.erase(up.find(x));
            sup -= x;
        }else{
            low.erase(low.find(x));
            slow -= x;
        }
        upd();
    }
    ll get(){
        if(!sz(low)) return 0;
        ll md = *prev(low.end());
        return md*(sz(low) - sz(up)) - slow + sup;
    }
};

int main(){
    cin.tie(0) -> sync_with_stdio(0);
    int k , n; cin >> k >> n;
    ll ans = 0;
    vector<pp> v;
    rep(i,0,n){
        char h, w; 
        int s, e; cin >> h >> s >> w >> e;
        if(h == w){
            ans += abs(s - e);
        }else{
            ans++;
            v.pb({s, e});
        }
    } 
    sort(all(v), [&](pp a, pp b){
        return a.ff + a.ss < b.ff + b.ss;
    });
    Box l, r;
    for(pp c: v){
        r.insert(c.ff);
        r.insert(c.ss);
    }
    if(k == 1){
        ans += r.get();
    }else{
        ll k = r.get();
        for(pp c: v){
            l.insert(c.ff);
            l.insert(c.ss);
            r.erase(c.ff);
            r.erase(c.ss);
            k = min(k, l.get() + r.get());
        }
        ans += k;
    }
    cout << ans;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 432 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 1 ms 328 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 328 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 328 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 94 ms 11144 KB Output is correct
13 Correct 168 ms 12788 KB Output is correct
14 Correct 134 ms 10592 KB Output is correct
15 Correct 78 ms 7560 KB Output is correct
16 Correct 97 ms 12072 KB Output is correct
17 Correct 97 ms 12748 KB Output is correct
18 Correct 80 ms 12392 KB Output is correct
19 Correct 110 ms 12776 KB Output is correct
20 Correct 96 ms 12316 KB Output is correct
21 Correct 102 ms 12476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 312 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 248 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 2 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 2 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 2 ms 324 KB Output is correct
20 Correct 2 ms 332 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 2 ms 332 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 2 ms 324 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 2 ms 332 KB Output is correct
20 Correct 2 ms 332 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 2 ms 332 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
25 Correct 231 ms 11280 KB Output is correct
26 Correct 396 ms 11344 KB Output is correct
27 Correct 396 ms 12092 KB Output is correct
28 Correct 398 ms 12692 KB Output is correct
29 Correct 415 ms 12796 KB Output is correct
30 Correct 159 ms 6888 KB Output is correct
31 Correct 208 ms 12072 KB Output is correct
32 Correct 203 ms 12736 KB Output is correct
33 Correct 160 ms 12376 KB Output is correct
34 Correct 224 ms 12800 KB Output is correct
35 Correct 225 ms 12368 KB Output is correct
36 Correct 216 ms 12464 KB Output is correct
37 Correct 185 ms 11188 KB Output is correct