Submission #163885

# Submission time Handle Problem Language Result Execution time Memory
163885 2019-11-16T01:43:13 Z combi1k1 Palembang Bridges (APIO15_bridge) C++14
100 / 100
325 ms 14688 KB
#include<bits/stdc++.h>

using namespace std;

#define X   first
#define Y   second
#define ll  long long

typedef pair<int,int>   ii;

int main()  {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

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

    ll  prev_sum = 0;

    vector<ii>  vec;

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

        cin >> a >> x;
        cin >> b >> y;

        if (a == b) prev_sum += abs(x - y);
        if (a != b) {
            prev_sum++;
            if (x > y)  swap(x,y);
            vec.push_back(ii(x,y));
        }
    }

    sort(vec.begin(),vec.end(),[&](ii  x,ii  y) {
        return  x.X + x.Y < y.X + y.Y;
    });

    vector<ll>  f(vec.size() + 1,0);
    vector<ll>  g(vec.size() + 1,0);

    auto cal = [&](vector<ll> &v)   {
        multiset<int>   Min;
        multiset<int>   Max;

        ll  Sum_Max = 0;
        ll  Sum_Min = 0;
        ll  Med = 0;

        for(int i = 0 ; i < vec.size() ; ++i)   {
            if (vec[i].X <= Med && Med <= vec[i].Y) {
                Min.insert(vec[i].X);   Sum_Min += vec[i].X;
                Max.insert(vec[i].Y);   Sum_Max += vec[i].Y;
            }
            if (vec[i].X > Med) {
                Max.insert(vec[i].X);   Sum_Max += vec[i].X;
                Max.insert(vec[i].Y);   Sum_Max += vec[i].Y;
                int x = *(Max.begin());
                Max.erase(Max.begin()); Sum_Max -= x;
                Min.insert(x);          Sum_Min += x;
            }
            if (vec[i].Y < Med) {
                Min.insert(vec[i].X);   Sum_Min += vec[i].X;
                Min.insert(vec[i].Y);   Sum_Min += vec[i].Y;
                int x = *(--Min.end());
                Min.erase(--Min.end()); Sum_Min -= x;
                Max.insert(x);          Sum_Max += x;
            }

            v[i + 1] = Sum_Max - Sum_Min;

            Med = *(Max.begin());
        }
    };

    cal(f); reverse(vec.begin(),vec.end());
    cal(g); reverse(g.begin(),g.end());

    ll  ans = 1e18;

    for(int i = 0 ; i < (k == 1 ? 1 : f.size()) ; ++i)
        ans = min(ans,f[i] + g[i]);

    cout << ans + prev_sum << endl;
}
/*
1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/

Compilation message

bridge.cpp: In lambda function:
bridge.cpp:52:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 0 ; i < vec.size() ; ++i)   {
                         ~~^~~~~~~~~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:83:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0 ; i < (k == 1 ? 1 : f.size()) ; ++i)
                     ~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 504 KB Output is correct
4 Correct 3 ms 504 KB Output is correct
5 Correct 3 ms 504 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 3 ms 504 KB Output is correct
8 Correct 3 ms 504 KB Output is correct
9 Correct 3 ms 420 KB Output is correct
10 Correct 3 ms 504 KB Output is correct
11 Correct 3 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 484 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 504 KB Output is correct
4 Correct 3 ms 504 KB Output is correct
5 Correct 3 ms 476 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 3 ms 504 KB Output is correct
8 Correct 3 ms 504 KB Output is correct
9 Correct 3 ms 504 KB Output is correct
10 Correct 3 ms 504 KB Output is correct
11 Correct 3 ms 504 KB Output is correct
12 Correct 155 ms 12956 KB Output is correct
13 Correct 311 ms 14448 KB Output is correct
14 Correct 262 ms 12144 KB Output is correct
15 Correct 154 ms 8692 KB Output is correct
16 Correct 210 ms 13932 KB Output is correct
17 Correct 191 ms 14448 KB Output is correct
18 Correct 116 ms 14012 KB Output is correct
19 Correct 193 ms 14492 KB Output is correct
20 Correct 182 ms 14252 KB Output is correct
21 Correct 198 ms 14196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 344 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 3 ms 504 KB Output is correct
14 Correct 3 ms 504 KB Output is correct
15 Correct 3 ms 504 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 3 ms 376 KB Output is correct
18 Correct 3 ms 380 KB Output is correct
19 Correct 3 ms 504 KB Output is correct
20 Correct 3 ms 504 KB Output is correct
21 Correct 3 ms 504 KB Output is correct
22 Correct 3 ms 504 KB Output is correct
23 Correct 3 ms 504 KB Output is correct
24 Correct 3 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 380 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 3 ms 504 KB Output is correct
14 Correct 3 ms 504 KB Output is correct
15 Correct 4 ms 504 KB Output is correct
16 Correct 3 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 3 ms 504 KB Output is correct
20 Correct 3 ms 504 KB Output is correct
21 Correct 3 ms 504 KB Output is correct
22 Correct 3 ms 504 KB Output is correct
23 Correct 3 ms 504 KB Output is correct
24 Correct 3 ms 504 KB Output is correct
25 Correct 155 ms 12916 KB Output is correct
26 Correct 254 ms 13212 KB Output is correct
27 Correct 314 ms 13968 KB Output is correct
28 Correct 325 ms 14448 KB Output is correct
29 Correct 322 ms 14540 KB Output is correct
30 Correct 130 ms 7796 KB Output is correct
31 Correct 151 ms 13804 KB Output is correct
32 Correct 193 ms 14688 KB Output is correct
33 Correct 114 ms 14064 KB Output is correct
34 Correct 194 ms 14384 KB Output is correct
35 Correct 201 ms 14020 KB Output is correct
36 Correct 198 ms 14316 KB Output is correct
37 Correct 149 ms 13044 KB Output is correct