Submission #229986

# Submission time Handle Problem Language Result Execution time Memory
229986 2020-05-07T13:29:57 Z lyc Palembang Bridges (APIO15_bridge) C++14
100 / 100
416 ms 13936 KB
#include <bits/stdc++.h>
using namespace std;

#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for (int i=(a);i>=(b);--i)
using ii = pair<int,int>;

const int MX_N = 1e5+5;

int K, N;
char P[MX_N], Q[MX_N];
int S[MX_N], T[MX_N];

struct Mean {
    multiset<int> st;
    multiset<int>::iterator mid;
    long long c;

    Mean(): mid(st.end()), c(0) {}

    void add(int x) {
        auto it = st.lower_bound(x);
        if (mid == st.end()) {
            st.insert(it,x);
            mid = st.begin();
        } else {
            int s = SZ(st);
            st.insert(it,x);
            if (s&1) {
                if (x <= *mid) {
                    c -= x;
                    c += *mid;
                    --mid;
                } else {
                    c += x;
                    c -= *mid;
                }
            } else {
                if (x <= *mid) {
                    c -= x;
                    c += *mid;
                } else {
                    c += x;
                    c -= *next(mid);
                    ++mid;
                }
            }
        }
    };

    void del(int x) {
        auto it = st.lower_bound(x);
        int s = SZ(st);
        if (it == mid) {
            if (s == 1) mid = st.end();
            else if (s&1) --mid;
            else {
                c += *mid;
                c -= *next(mid);
                ++mid;
            }
            st.erase(it);
        } else {
            st.erase(it);
            if (s&1) {
                if (x <= *mid) {
                    c += x;
                    c -= *mid;
                } else {
                    c -= x;
                    c += *mid;
                    --mid;
                }
            } else {
                if (x <= *mid) {
                    c += x;
                    c -= *next(mid);
                    ++mid;
                } else {
                    c -= x;
                    c += *mid;
                }
            }
        }
    }

    long long get() {
        return c;
    }

    void dbg() {
        cout << "mid: ";
        if (mid == st.end()) cout << "NULL";
        else cout << *mid << " (" << distance(st.begin(),mid) << ")";
        cout << endl;

        cout << "st: ";
        for (auto& x : st) cout << x << ' ';
        cout << endl;

        cout << "c: " << c;
        cout << endl;

        cout << endl;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> K >> N;
    long long ans = 0;
    FOR(i,1,N){
        cin >> P[i] >> S[i] >> Q[i] >> T[i];
        if (P[i] == Q[i]) ans += abs(S[i]-T[i]);
        else ans += 1;
    }

    //Mean m;

    //m.add(0);
    //m.add(1);
    //m.add(1);
    //m.add(1);
    //m.add(2);
    //m.dbg();
    //m.del(1);
    //m.dbg();
    //m.del(1);
    //m.dbg();

    //return 0;

    if (K == 1) {
        Mean m;
        FOR(i,1,N) if (P[i] != Q[i]) { 
            m.add(S[i]);
            m.add(T[i]);
            //m.dbg();
        }
        ans += m.get();
        cout << ans << '\n';
    } else {
        vector<ii> inv;
        FOR(i,1,N) if (P[i] != Q[i]) {
            inv.emplace_back(S[i],T[i]);
        }
        sort(ALL(inv),[](ii a, ii b){
                if (a.first+a.second == b.first+b.second) {
                if (a.first == b.first) return a.second < b.second;
                return a.first < b.first;
                }
                return (a.first+a.second < b.first+b.second);
                });

        Mean l, r;
        for (auto& x : inv) {
            r.add(x.first);
            r.add(x.second);
        }

        long long mini = r.get();
        FOR(i,0,SZ(inv)-1){
            auto x = inv[i];
            //TRACE(i _ x.first _ x.second);
            //cout << "L" << endl;
            l.add(x.first); l.add(x.second);
            //cout << "R" << endl;
            r.del(x.first); r.del(x.second);
            long long cur = l.get() + r.get();
            mini = min(mini,cur);
        }
        ans += mini;
        cout << ans << '\n';
    }
}

# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 6 ms 512 KB Output is correct
4 Correct 5 ms 512 KB Output is correct
5 Correct 5 ms 512 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 5 ms 512 KB Output is correct
8 Correct 5 ms 512 KB Output is correct
9 Correct 5 ms 512 KB Output is correct
10 Correct 5 ms 512 KB Output is correct
11 Correct 5 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 512 KB Output is correct
4 Correct 5 ms 512 KB Output is correct
5 Correct 5 ms 512 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 5 ms 512 KB Output is correct
8 Correct 5 ms 512 KB Output is correct
9 Correct 5 ms 512 KB Output is correct
10 Correct 6 ms 512 KB Output is correct
11 Correct 5 ms 512 KB Output is correct
12 Correct 62 ms 10872 KB Output is correct
13 Correct 160 ms 10872 KB Output is correct
14 Correct 132 ms 9848 KB Output is correct
15 Correct 67 ms 6584 KB Output is correct
16 Correct 80 ms 10872 KB Output is correct
17 Correct 84 ms 11000 KB Output is correct
18 Correct 86 ms 10872 KB Output is correct
19 Correct 117 ms 10872 KB Output is correct
20 Correct 74 ms 10872 KB Output is correct
21 Correct 85 ms 10872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 432 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 432 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 512 KB Output is correct
14 Correct 6 ms 512 KB Output is correct
15 Correct 6 ms 512 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 512 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 512 KB Output is correct
20 Correct 6 ms 512 KB Output is correct
21 Correct 6 ms 512 KB Output is correct
22 Correct 6 ms 512 KB Output is correct
23 Correct 6 ms 512 KB Output is correct
24 Correct 6 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 560 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 6 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 512 KB Output is correct
14 Correct 6 ms 512 KB Output is correct
15 Correct 6 ms 512 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 6 ms 384 KB Output is correct
18 Correct 6 ms 384 KB Output is correct
19 Correct 6 ms 512 KB Output is correct
20 Correct 6 ms 512 KB Output is correct
21 Correct 6 ms 560 KB Output is correct
22 Correct 6 ms 512 KB Output is correct
23 Correct 6 ms 512 KB Output is correct
24 Correct 6 ms 512 KB Output is correct
25 Correct 138 ms 12272 KB Output is correct
26 Correct 162 ms 12400 KB Output is correct
27 Correct 396 ms 13296 KB Output is correct
28 Correct 376 ms 13808 KB Output is correct
29 Correct 416 ms 13808 KB Output is correct
30 Correct 139 ms 7540 KB Output is correct
31 Correct 169 ms 13168 KB Output is correct
32 Correct 150 ms 13936 KB Output is correct
33 Correct 139 ms 13424 KB Output is correct
34 Correct 133 ms 13804 KB Output is correct
35 Correct 138 ms 13424 KB Output is correct
36 Correct 143 ms 13632 KB Output is correct
37 Correct 124 ms 12268 KB Output is correct