Submission #435301

# Submission time Handle Problem Language Result Execution time Memory
435301 2021-06-23T07:20:01 Z iANikzad Palembang Bridges (APIO15_bridge) C++14
100 / 100
109 ms 7976 KB
#include<bits/stdc++.h>

using namespace std;

#define pb push_back
#define F first
#define S second
#define debug(x) cerr<<#x<<" :"<<x<<"\n"
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define FAST ios_base::sync_with_stdio(false), cin.tie(), cout.tie();
#define int long long

typedef long long ll;
typedef long double ld;

const int maxn = 1e5 + 7;
const int mod = 1e9 + 7;
const int INF = 1e9 + 1;
const int mlog = 20;
const int SQ = 400;

int ls, rs;
priority_queue<int> lpq;
priority_queue<int, vector<int>, greater<int>> rpq;

int pref[maxn];

bool cmp(pii a, pii b) { return a.first + a.second < b.first + b.second; }

void add(int x)
{
    int median = (lpq.size() ? lpq.top() : +INF);

    if (x <= median)
    {
        lpq.push(x);
        ls += x;
    } else  {
        rpq.push(x);
        rs += x;
    }


    if (rpq.size() + 1 < lpq.size())
    {
        int nxt = lpq.top();
        lpq.pop(); rpq.push(nxt);
        ls -= nxt; rs += nxt;

    } else if (lpq.size() < rpq.size())
    {
        int nxt = rpq.top();
        rpq.pop();
        lpq.push(nxt);
        rs -= nxt; ls += nxt;
    }
}

int32_t main()
{
    FAST;

    int n, k;
    cin >> k >> n;

    int ham = 0;

    vector<pii> vec = {{0, 0}};

    for(int i = 0; i < n; i++)
    {
        char a, b; int x, y;
        cin >> a >> x >> b >> y;

        if(a == b) ham += abs(x - y);
        else vec.pb({x, y});
    }

    sort(all(vec), cmp);

    n = vec.size() - 1;
    ham += n;

    ls = rs = 0;
    for(int i = 1; i <= n; i++)
    {
        add(vec[i].F); add(vec[i].S);
        pref[i] = rs - ls;
    }

    int ans = pref[n];
    if(k == 2)
    {
        while(lpq.size()) lpq.pop();
        while(rpq.size()) rpq.pop();

        ls = rs = 0;
        for(int i = n; i; i--)
        {
            add(vec[i].F); add(vec[i].S);
            ans = min(ans, rs - ls + pref[i - 1]);
        }
    }

    cout << ham + ans << "\n";

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 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 2 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 2 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 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 39 ms 6272 KB Output is correct
13 Correct 70 ms 7676 KB Output is correct
14 Correct 54 ms 6920 KB Output is correct
15 Correct 40 ms 4912 KB Output is correct
16 Correct 45 ms 6964 KB Output is correct
17 Correct 54 ms 7864 KB Output is correct
18 Correct 52 ms 7856 KB Output is correct
19 Correct 62 ms 7864 KB Output is correct
20 Correct 47 ms 7480 KB Output is correct
21 Correct 58 ms 7584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 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 320 KB Output is correct
6 Correct 1 ms 204 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 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 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 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 2 ms 204 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 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 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 1 ms 332 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory 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 324 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 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 2 ms 360 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 1 ms 360 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
25 Correct 53 ms 6328 KB Output is correct
26 Correct 83 ms 7072 KB Output is correct
27 Correct 108 ms 7352 KB Output is correct
28 Correct 109 ms 7976 KB Output is correct
29 Correct 106 ms 7424 KB Output is correct
30 Correct 54 ms 4160 KB Output is correct
31 Correct 59 ms 6844 KB Output is correct
32 Correct 82 ms 7864 KB Output is correct
33 Correct 82 ms 7100 KB Output is correct
34 Correct 90 ms 7848 KB Output is correct
35 Correct 60 ms 7392 KB Output is correct
36 Correct 85 ms 7608 KB Output is correct
37 Correct 44 ms 6268 KB Output is correct