Submission #732492

# Submission time Handle Problem Language Result Execution time Memory
732492 2023-04-29T04:58:38 Z hafo Palembang Bridges (APIO15_bridge) C++14
100 / 100
269 ms 14388 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define all(x) x.begin(), x.end()
using namespace std;

template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}

const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 2e5 + 7;
const ll oo = 1e9 + 69;

int k, n;
ll fa[maxn], fb[maxn], ans = 0;
vector<int> a, b, point;

ll calc(int i) {
    int id = upper_bound(all(a), i) - a.begin();
    ll res = 1LL * i * id - fa[id] + fa[(int) a.size()] - fa[id] - 1LL * i * ((int) a.size() - id);
    id = upper_bound(all(b), i) - b.begin();
    res = res + 1LL * i * id - fb[id] + fb[(int) b.size()] - fb[id] - 1LL * i * ((int) b.size() - id);
    return res;
}

void sub1() {
    for(auto i:a) point.pb(i);
    for(auto i:b) point.pb(i);
    sort(all(point));
    sort(all(a));
    sort(all(b));
    fa[0] = fb[0] = 0;
    for(int i = 1; i <= (int) a.size(); i++) fa[i] = fa[i - 1] + a[i - 1];
    for(int i = 1; i <= (int) b.size(); i++) fb[i] = fb[i - 1] + b[i - 1];

    ll mn = oo;
    for(auto i:point) mini(mn, calc(i));
    ans += (int) a.size() + mn;
}

struct sliding_median {
    multiset<int> low, high;
    ll lsum, rsum;

    void init() {
        multiset<int> tmp;
        low = tmp;
        high = tmp;
        lsum = rsum = 0;
    }

    void fix() {
        if(low.size() < high.size()) {
            int val = *high.begin();
            lsum += val;
            rsum -= val;
            low.insert(val);
            high.erase(high.find(val));
        } else if((int) low.size() > (int) high.size() + 1) {
            int val = *low.rbegin();
            lsum -= val;
            rsum += val;
            low.erase(low.find(val));
            high.insert(val);
        }
    }

    void update(int x) {
        int med = (low.empty() ? oo:*low.rbegin());
        if(x < med){
            lsum += x;
            low.insert(x);
        }
        else {
            rsum += x;
            high.insert(x);
        }
        fix();
    }

    void Remove(int x) {
        if(low.find(x) != low.end()) {
            lsum -= x;
            low.erase(low.find(x));
        }
        else {
            rsum -= x;
            high.erase(high.find(x));
        }
        fix();
    }
} s;

bool cmp(pa a, pa b) {
    return a.fi + a.se < b.fi + b.se;
}

void sub2(){
    int m = (int) a.size();
    vector<pa> val;
    for(int i = 0; i < m; i++) {
        val.pb({a[i], b[i]});
    }
    sort(all(val), cmp);
    s.init();
    for(int i = 1; i <= m; i++) {
        s.update(val[i - 1].fi);
        s.update(val[i - 1].se);
        fa[i] = s.rsum - s.lsum;
    }

    s.init();
    ll mn = fa[m];
    if(k == 1) {
        ans += mn + m;
        return;
    }
    for(int i = m; i >= 1; i--) {
        s.update(val[i - 1].fi);
        s.update(val[i - 1].se);
        mini(mn, fa[i - 1] + s.rsum - s.lsum);
    }
    ans += (mn + m);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    //freopen(TASK".inp", "r", stdin);
    //freopen("a.out", "w", stdout);

    cin>>k>>n;
    ans = 0;
    for(int i = 0; i < n; i++){
        char ch1, ch2;
        int x, y;
        cin>>ch1>>x>>ch2>>y;
        if(ch1 == ch2) ans += abs(x - y);
        else {
            if(ch1 == 'A') a.pb(x);
            else b.pb(x);
            if(ch2 == 'A') a.pb(y);
            else b.pb(y);
        }
    }


    sub2();
    cout<<ans;
    return 0;
}
# Verdict Execution time Memory 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 472 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 2 ms 360 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
# Verdict Execution time Memory 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 468 KB Output is correct
5 Correct 1 ms 476 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 92 ms 12752 KB Output is correct
13 Correct 123 ms 14340 KB Output is correct
14 Correct 106 ms 11976 KB Output is correct
15 Correct 62 ms 8532 KB Output is correct
16 Correct 84 ms 13712 KB Output is correct
17 Correct 91 ms 14388 KB Output is correct
18 Correct 71 ms 13932 KB Output is correct
19 Correct 95 ms 14316 KB Output is correct
20 Correct 85 ms 13988 KB Output is correct
21 Correct 115 ms 14124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 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 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 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 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
21 Correct 2 ms 460 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 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 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 145 ms 11996 KB Output is correct
26 Correct 269 ms 12084 KB Output is correct
27 Correct 234 ms 12080 KB Output is correct
28 Correct 233 ms 12036 KB Output is correct
29 Correct 224 ms 12028 KB Output is correct
30 Correct 91 ms 6512 KB Output is correct
31 Correct 140 ms 11968 KB Output is correct
32 Correct 131 ms 12000 KB Output is correct
33 Correct 139 ms 12128 KB Output is correct
34 Correct 150 ms 12012 KB Output is correct
35 Correct 145 ms 12016 KB Output is correct
36 Correct 153 ms 12064 KB Output is correct
37 Correct 144 ms 12052 KB Output is correct