Submission #106657

# Submission time Handle Problem Language Result Execution time Memory
106657 2019-04-19T12:00:44 Z popovicirobert Palembang Bridges (APIO15_bridge) C++14
100 / 100
580 ms 14740 KB
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44

using namespace std;

const ll INF = 1e18;

inline ll getmin(ll a, ll b) {
    return a < b ? a : b;
}

inline void solve(vector < pair <int, int> > &arr, vector <ll> &sol) {
    int sz = arr.size();
    sol.resize(sz);

    multiset <int> mst1, mst2;
    ll sum1 = 0, sum2 = 0;

    for(int i = 0; i < sz; i++) {
        mst2.insert(arr[i].first);
        sum2 += arr[i].first;
        mst2.insert(arr[i].second);
        sum2 += arr[i].second;

        while(mst1.size() < i + 1) {
            mst1.insert(*mst2.begin());
            sum1 += *mst2.begin();
            sum2 -= *mst2.begin();
            mst2.erase(mst2.begin());
        }

        while(*prev(mst1.end()) > *mst2.begin()) {
            int a = *prev(mst1.end()), b = *mst2.begin();
            mst1.erase(mst1.find(a));
            mst1.insert(b);
            mst2.erase(mst2.find(b));
            mst2.insert(a);
            sum1 += b - a;
            sum2 += a - b;
        }

        int cur = *prev(mst1.end());
        sol[i] = 1LL * mst1.size() * cur - sum1 + sum2 - 1LL * mst2.size() * cur;

    }

}

int main() {
    //ifstream cin("A.in");
    //ofstream cout("A.out");
    int i, n, k;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> k >> n;

    vector < pair <int, int> > arr;
    ll ans = 0;
    for(i = 1; i <= n; i++) {
        char a, b;
        int x, y;
        cin >> a >> x >> b >> y;
        if(x > y) {
            swap(x, y);
        }
        if(a != b) {
            arr.push_back({x, y});
        }
        else {
            ans += y - x;
        }
    }

    sort(arr.begin(), arr.end(), [&](const pair <int, int> &a, const pair <int, int> &b) {

            return a.first + a.second < b.first + b.second;

        });

    if(arr.size() == 0) {
        cout << ans;
        return 0;
    }

    if(k == 1) {
        vector <int> vals;
        ll tot = 0;
        for(auto it : arr) {
            vals.push_back(it.first);
            vals.push_back(it.second);
            tot += it.first + it.second;
        }
        sort(vals.begin(), vals.end());
        int sz = arr.size();
        ll mn = INF, sum = 0;
        for(i = 0; i < 2 * sz; i++) {
            mn = getmin(mn, 1LL * vals[i] * i - sum + (tot - sum) - 1LL * vals[i] * (2 * sz - i));
            sum += vals[i];
        }
        cout << ans + mn + sz;
        return 0;
    }

    vector <ll> sol_l, sol_r;
    solve(arr, sol_l);
    reverse(arr.begin(), arr.end());
    solve(arr, sol_r);

    int sz = arr.size();
    ll mn = getmin(sol_l[sz - 1], sol_r[sz - 1]);
    for(int i = 0; i < sz - 1; i++) {
        mn = getmin(mn, sol_l[i] + sol_r[sz - i - 2]);
    }

    cout << ans + mn + sz;

    //cin.close();
    //cout.close();
    return 0;
}

Compilation message

bridge.cpp: In function 'void solve(std::vector<std::pair<int, int> >&, std::vector<long long int>&)':
bridge.cpp:29:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         while(mst1.size() < i + 1) {
               ~~~~~~~~~~~~^~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 6 ms 384 KB Output is correct
6 Correct 6 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 26 ms 2680 KB Output is correct
13 Correct 54 ms 2668 KB Output is correct
14 Correct 47 ms 2672 KB Output is correct
15 Correct 38 ms 1652 KB Output is correct
16 Correct 33 ms 2804 KB Output is correct
17 Correct 51 ms 2760 KB Output is correct
18 Correct 39 ms 2672 KB Output is correct
19 Correct 63 ms 2672 KB Output is correct
20 Correct 44 ms 2672 KB Output is correct
21 Correct 44 ms 2672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 3 ms 304 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 3 ms 356 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 4 ms 512 KB Output is correct
15 Correct 5 ms 512 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
18 Correct 4 ms 384 KB Output is correct
19 Correct 4 ms 512 KB Output is correct
20 Correct 6 ms 512 KB Output is correct
21 Correct 4 ms 512 KB Output is correct
22 Correct 4 ms 512 KB Output is correct
23 Correct 6 ms 512 KB Output is correct
24 Correct 4 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 4 ms 512 KB Output is correct
15 Correct 5 ms 512 KB Output is correct
16 Correct 3 ms 384 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 3 ms 384 KB Output is correct
19 Correct 4 ms 512 KB Output is correct
20 Correct 5 ms 512 KB Output is correct
21 Correct 9 ms 512 KB Output is correct
22 Correct 3 ms 512 KB Output is correct
23 Correct 4 ms 512 KB Output is correct
24 Correct 2 ms 512 KB Output is correct
25 Correct 229 ms 13040 KB Output is correct
26 Correct 292 ms 13036 KB Output is correct
27 Correct 576 ms 14156 KB Output is correct
28 Correct 537 ms 14576 KB Output is correct
29 Correct 580 ms 14740 KB Output is correct
30 Correct 237 ms 7796 KB Output is correct
31 Correct 186 ms 13804 KB Output is correct
32 Correct 265 ms 14440 KB Output is correct
33 Correct 152 ms 14120 KB Output is correct
34 Correct 298 ms 14448 KB Output is correct
35 Correct 198 ms 14064 KB Output is correct
36 Correct 248 ms 14192 KB Output is correct
37 Correct 186 ms 13012 KB Output is correct