답안 #1111209

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1111209 2024-11-11T17:17:00 Z vladilius Palembang Bridges (APIO15_bridge) C++17
100 / 100
274 ms 21044 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
#define pb push_back
#define ff first
#define ss second
const ll inf = numeric_limits<ll> :: max();
 
struct DS{
    vector<pil> t;
    vector<int> imp;
    int n, cc;
    void init(vector<int> imps){
        sort(imps.begin(), imps.end());
        for (int i = 0; i < imps.size(); i++){
            if (!i || imps[i - 1] != imps[i]){
                imp.pb(imps[i]);
            }
        }
        n = (int) imp.size();
        t.resize(4 * n);
        cc = 0;
    }
    void upd(int v, int tl, int tr, int& p, int& x){
        if (tl == tr){
            t[v].ff++;
            t[v].ss += x;
            return;
        }
        int tm = (tl + tr) / 2, vv = 2 * v;
        if (p <= tm){
            upd(vv, tl, tm, p, x);
        }
        else {
            upd(vv + 1, tm + 1, tr, p, x);
        }
        t[v].ff = t[vv].ff + t[vv + 1].ff;
        t[v].ss = t[vv].ss + t[vv + 1].ss;
    }
    void upd(int p, int x){
        upd(1, 1, n, p, x);
    }
    pil get(int v, int tl, int tr, int& l, int& r){
        if (l > tr || r < tl) return {0, 0};
        if (l <= tl && tr <= r){
            return t[v];
        }
        int tm = (tl + tr) / 2, vv = 2 * v;
        pil p1 = get(vv, tl, tm, l, r), p2 = get(vv + 1, tm + 1, tr, l, r);
        return {p1.ff + p2.ff, p1.ss + p2.ss};
    }
    pair<int, ll> get(int l, int r){
        return get(1, 1, n, l, r);
    }
    priority_queue<int> s1;
    priority_queue<int, vector<int>, greater<int>> s2;
    vector<int> :: iterator it;
    void add(int x){
        cc++;
        s1.push(x);
        if (s1.size() > s2.size()){
            int y = s1.top();
            s1.pop();
            s2.push(y);
        }
        if (!s1.empty() && !s2.empty()){
            int a = s1.top(), b = s2.top();
            if (a > b){
                s1.pop();
                s2.pop();
                s1.push(b);
                s2.push(a);
            }
        }
        it = lower_bound(imp.begin(), imp.end(), x);
        int j = (int) (it - imp.begin()) + 1;
        upd(j, x);
    }
    ll get(){
        if (s2.empty()) return 0;
        int x = (cc % 2) ? s2.top() : s1.top();
        it = lower_bound(imp.begin(), imp.end(), x);
        int j = (int) (it - imp.begin()) + 1;
        pil p1 = get(1, j), p2 = get(j + 1, n);
        return (1LL * x * p1.ff - p1.ss) + (p2.ss - 1LL * x * p2.ff);
    }
    void clear(){
        for (int i = 0; i < 4 * n; i++){
            t[i] = {0, 0};
        }
        while (!s1.empty()) s1.pop();
        while (!s2.empty()) s2.pop();
    }
};
 
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int k, n; cin>>k>>n;
    vector<pii> all;
    ll out = 0;
    while (n--){
        char t1, t2; int x1, y1; cin>>t1>>x1>>t2>>y1;
        if (t1 == t2){
            out += abs(x1 - y1);
            continue;
        }
        if (x1 > y1) swap(x1, y1);
        all.pb({x1, y1});
    }
    if (all.empty()){
        cout<<out<<"\n";
        return 0;
    }
    
    n = (int) all.size();
    
    vector<int> imp;
    for (auto [x, y]: all){
        imp.pb(x); imp.pb(y);
    }
 
    ll mn = inf;
    if (k == 1){
        sort(imp.begin(), imp.end());
        int x = imp[(int) (imp.size() - 1) / 2];
        ll sum = 0;
        for (int i: imp){
            sum += abs(i - x);
        }
        mn = sum + n;
    }
    else {
        vector<pii> f;
        for (int i = 0; i < n; i++){
            f.pb({all[i].ff + all[i].ss, i});
        }
        sort(f.begin(), f.end());
        vector<ll> f1(n + 1);
        DS T; T.init(imp);
        for (int i = 1; i <= n; i++){
            auto [x, y] = all[f[i - 1].ss];
            T.add(x); T.add(y);
            f1[i] = T.get();
        }
        
        vector<ll> f2(n + 2);
        T.clear();
        for (int i = n; i > 0; i--){
            auto [x, y] = all[f[i - 1].ss];
            T.add(x); T.add(y);
            f2[i] = T.get();
        }
        
        for (int i = 0; i <= n; i++){
            mn = min(mn, f1[i] + f2[i + 1] + n);
        }
    }
    cout<<out + mn<<"\n";
}

Compilation message

bridge.cpp: In member function 'void DS::init(std::vector<int>)':
bridge.cpp:17:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 |         for (int i = 0; i < imps.size(); i++){
      |                         ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 504 KB Output is correct
8 Correct 2 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 504 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 15 ms 2552 KB Output is correct
13 Correct 31 ms 2764 KB Output is correct
14 Correct 22 ms 2508 KB Output is correct
15 Correct 18 ms 1488 KB Output is correct
16 Correct 17 ms 2932 KB Output is correct
17 Correct 21 ms 2764 KB Output is correct
18 Correct 25 ms 2668 KB Output is correct
19 Correct 29 ms 2764 KB Output is correct
20 Correct 20 ms 2764 KB Output is correct
21 Correct 26 ms 2764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 516 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 508 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 504 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 2 ms 336 KB Output is correct
15 Correct 3 ms 592 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 2 ms 336 KB Output is correct
19 Correct 2 ms 336 KB Output is correct
20 Correct 2 ms 592 KB Output is correct
21 Correct 2 ms 592 KB Output is correct
22 Correct 3 ms 592 KB Output is correct
23 Correct 1 ms 528 KB Output is correct
24 Correct 3 ms 592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 2 ms 336 KB Output is correct
15 Correct 2 ms 592 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 2 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 2 ms 592 KB Output is correct
21 Correct 2 ms 476 KB Output is correct
22 Correct 2 ms 592 KB Output is correct
23 Correct 1 ms 528 KB Output is correct
24 Correct 2 ms 760 KB Output is correct
25 Correct 50 ms 6332 KB Output is correct
26 Correct 112 ms 6580 KB Output is correct
27 Correct 273 ms 19340 KB Output is correct
28 Correct 274 ms 21028 KB Output is correct
29 Correct 273 ms 21044 KB Output is correct
30 Correct 139 ms 11576 KB Output is correct
31 Correct 60 ms 7100 KB Output is correct
32 Correct 172 ms 21024 KB Output is correct
33 Correct 168 ms 20712 KB Output is correct
34 Correct 178 ms 20544 KB Output is correct
35 Correct 64 ms 7376 KB Output is correct
36 Correct 169 ms 20796 KB Output is correct
37 Correct 53 ms 6340 KB Output is correct