Submission #107160

# Submission time Handle Problem Language Result Execution time Memory
107160 2019-04-22T08:57:00 Z oolimry Palembang Bridges (APIO15_bridge) C++14
100 / 100
115 ms 8860 KB
#include <bits/stdc++.h>

using namespace std;

typedef pair<long long, long long> ii;
bool comp(ii a, ii b){
    return a.first + a.second < b.first + b.second;
}
int main()
{
    //freopen("i.txt","r",stdin);
    int k, n;

    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> k >> n;


    vector<ii> pairs;
    long long forcedDist = 0ll;

    for(int i = 0;i < n;i++){
        char a, c;
        long long b, d;
        cin >> a >> b >> c >> d;

        if(a == c){
            forcedDist += abs(d-b);
        }
        else{
            forcedDist += abs(d-b);
            forcedDist++;
            if(b > d) swap(b,d);
            pairs.push_back(ii(b,d));
        }
    }

    n = pairs.size();
    if(n == 0){
        cout << forcedDist;
        return 0;
    }
    sort(pairs.begin(),pairs.end(),comp);

    for(int i = 0;i < n;i++){
        //cout << pairs[i].first << " " << pairs[i].second << "\n";
    }

    long long prefix[n];
    long long suffix[n];

    priority_queue<long long> low;
    priority_queue<long long, vector<long long>, greater<long long> > high;
    long long minVal = 0;
    for(int i = 0;i < n;i++){
        long long p = pairs[i].first;
        long long q = pairs[i].second;
        if(low.empty()){
            low.push(p);
            high.push(q);
        }
        else{
            if(low.top() >= q){
                low.push(p);
                low.push(q);
                minVal += abs(low.top() - q);

                high.push(low.top());
                low.pop();
            }
            else if(high.top() <= p){
                high.push(p);
                high.push(q);
                minVal += abs(high.top() - p);
                low.push(high.top());
                high.pop();
            }
            else{
                low.push(p);
                high.push(q);
            }
        }
        prefix[i] = minVal;
    }

    if(k == 1){
        cout << prefix[n-1] * 2ll + forcedDist;
        return 0;
    }




    reverse(pairs.begin(),pairs.end());

    while(!low.empty())low.pop();
    while(!high.empty())high.pop();
    minVal = 0;
    for(int i = 0;i < n;i++){
        long long p = pairs[i].first;
        long long q = pairs[i].second;
        if(low.empty()){
            low.push(p);
            high.push(q);
        }
        else{
            if(low.top() >= q){
                low.push(p);
                low.push(q);
                minVal += abs(low.top() - q);

                high.push(low.top());
                low.pop();
            }
            else if(high.top() <= p){
                high.push(p);
                high.push(q);
                minVal += abs(high.top() - p);
                low.push(high.top());
                high.pop();
            }
            else{
                low.push(p);
                high.push(q);
            }
        }
        suffix[i] = minVal;
    }
    reverse(suffix,suffix+n);
    //cout << "\n";
    long long mv = suffix[0];
    for(int i = 1;i < n;i++){
        //cout << suffix[i] << " ";
        mv = min(prefix[i-1] + suffix[i], mv);
    }

    cout << mv * 2ll + forcedDist;





    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 2 ms 256 KB Output is correct
3 Correct 2 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 3 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 4 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 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 3 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 4 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
12 Correct 35 ms 6124 KB Output is correct
13 Correct 67 ms 7032 KB Output is correct
14 Correct 61 ms 6132 KB Output is correct
15 Correct 46 ms 4796 KB Output is correct
16 Correct 54 ms 6884 KB Output is correct
17 Correct 71 ms 6840 KB Output is correct
18 Correct 40 ms 6892 KB Output is correct
19 Correct 82 ms 6832 KB Output is correct
20 Correct 49 ms 6888 KB Output is correct
21 Correct 82 ms 6868 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 3 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 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 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
# 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 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 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 2 ms 356 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 3 ms 384 KB Output is correct
16 Correct 3 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 4 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 3 ms 400 KB Output is correct
21 Correct 3 ms 384 KB Output is correct
22 Correct 3 ms 384 KB Output is correct
23 Correct 3 ms 384 KB Output is correct
24 Correct 3 ms 384 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 2 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 2 ms 384 KB Output is correct
8 Correct 2 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 3 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 3 ms 384 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 2 ms 384 KB Output is correct
20 Correct 4 ms 384 KB Output is correct
21 Correct 3 ms 384 KB Output is correct
22 Correct 3 ms 552 KB Output is correct
23 Correct 3 ms 384 KB Output is correct
24 Correct 4 ms 384 KB Output is correct
25 Correct 55 ms 6932 KB Output is correct
26 Correct 73 ms 7572 KB Output is correct
27 Correct 99 ms 8656 KB Output is correct
28 Correct 115 ms 8860 KB Output is correct
29 Correct 106 ms 8296 KB Output is correct
30 Correct 53 ms 4944 KB Output is correct
31 Correct 51 ms 8044 KB Output is correct
32 Correct 82 ms 8680 KB Output is correct
33 Correct 53 ms 8428 KB Output is correct
34 Correct 91 ms 8680 KB Output is correct
35 Correct 67 ms 8296 KB Output is correct
36 Correct 92 ms 8484 KB Output is correct
37 Correct 46 ms 7244 KB Output is correct