Submission #894536

# Submission time Handle Problem Language Result Execution time Memory
894536 2023-12-28T11:47:38 Z salmon Palembang Bridges (APIO15_bridge) C++14
100 / 100
487 ms 23892 KB
#include <bits/stdc++.h>
using namespace std;

int N;
int K;
char c,c1;
vector<pair<long long int,pair<int,int>>> v;
long long int h,h1;
long long int sum = 0;
long long int ans = 0;
long long int big = 1e18;
long long int lst[100100];

int main(){

    scanf(" %d",&K);
    scanf(" %d",&N);

    set<pair<int,int>> s;
    map<int,int> mep;

    for(int i = 0; i < N; i++){
        scanf(" %c",&c);
        scanf(" %lld",&h);
        scanf(" %c",&c1);
        scanf(" %lld",&h1);

        if(c == c1){
            ans += abs(h - h1);
        }
        else{
            v.push_back(make_pair(h + h1, make_pair(h,h1)));
        }
    }

    sort(v.begin(),v.end());

    auto it = s.begin();

    lst[0] = 0;

    for(int i = 0; i < v.size(); i++){
        if(i == 0){
            sum = abs(v[i].second.second - v[i].second.first);
            s.insert(make_pair(v[i].second.second, mep[v[i].second.second]));
            mep[v[i].second.second]++;
            s.insert(make_pair(v[i].second.first, mep[v[i].second.first]));
            mep[v[i].second.first]++;
            it = s.begin();
        }
        else{
            int flag = 0;

            pair<int,int> ii = make_pair(v[i].second.first, mep[v[i].second.first] );
            mep[v[i].second.first]++;
            pair<int,int> ii1 = make_pair(v[i].second.second, mep[v[i].second.second]);
            mep[v[i].second.second]++;

            flag += (*it) < ii;
            flag += (*it) < ii1;

            int lum = 0;

            if(flag == 0){
                s.insert(ii);
                s.insert(ii1);

                lum += it -> first;
                advance(it,-1);
                lum -= it -> first;

                sum += abs(it -> first - ii.first);
                sum += abs(it -> first - ii1.first);
                sum += abs(lum)* 2;
            }
            if(flag == 1){
                s.insert(ii);
                s.insert(ii1);

                sum += abs(it -> first - ii.first);
                sum += abs(it -> first - ii1.first);
            }
            if(flag == 2){
                s.insert(ii);
                s.insert(ii1);

                advance(it,1);

                sum += abs(it -> first - ii.first);
                sum += abs(it -> first - ii1.first);
            }
        }

        lst[i + 1] = sum;
    }

    s.clear();
    mep.clear();
    big = sum;

    for(int i = v.size() - 1; i >= 0; i--){
        if(i == v.size() - 1){
            sum = abs(v[i].second.second - v[i].second.first);
            s.insert(make_pair(v[i].second.second, mep[v[i].second.second]));
            mep[v[i].second.second]++;
            s.insert(make_pair(v[i].second.first, mep[v[i].second.first]));
            mep[v[i].second.first]++;
            it = s.begin();
        }
        else{
            int flag = 0;

            pair<int,int> ii = make_pair(v[i].second.first, mep[v[i].second.first] );
            mep[v[i].second.first]++;
            pair<int,int> ii1 = make_pair(v[i].second.second, mep[v[i].second.second]);
            mep[v[i].second.second]++;

            flag += (*it) < ii;
            flag += (*it) < ii1;

            int lum = 0;

            if(flag == 0){
                s.insert(ii);
                s.insert(ii1);

                lum += it -> first;
                advance(it,-1);
                lum -= it -> first;

                sum += abs(it -> first - ii.first);
                sum += abs(it -> first - ii1.first);
                sum += abs(lum) * 2;
            }
            if(flag == 1){
                s.insert(ii);
                s.insert(ii1);

                sum += abs(it -> first - ii.first);
                sum += abs(it -> first - ii1.first);
            }
            if(flag == 2){
                s.insert(ii);
                s.insert(ii1);

                advance(it,1);

                sum += abs(it -> first - ii.first);
                sum += abs(it -> first - ii1.first);
            }
        }

        big = min(big, lst[i] + sum);
    }

    if(K == 2) printf("%lld",big + v.size() + ans);
    else printf("%lld", lst[v.size()] + ans + v.size());

}
/*
2 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 function 'int main()':
bridge.cpp:42:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |     for(int i = 0; i < v.size(); i++){
      |                    ~~^~~~~~~~~~
bridge.cpp:102:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |         if(i == v.size() - 1){
      |            ~~^~~~~~~~~~~~~~~
bridge.cpp:16:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |     scanf(" %d",&K);
      |     ~~~~~^~~~~~~~~~
bridge.cpp:17:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |     scanf(" %d",&N);
      |     ~~~~~^~~~~~~~~~
bridge.cpp:23:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |         scanf(" %c",&c);
      |         ~~~~~^~~~~~~~~~
bridge.cpp:24:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |         scanf(" %lld",&h);
      |         ~~~~~^~~~~~~~~~~~
bridge.cpp:25:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |         scanf(" %c",&c1);
      |         ~~~~~^~~~~~~~~~~
bridge.cpp:26:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   26 |         scanf(" %lld",&h1);
      |         ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 2 ms 604 KB Output is correct
5 Correct 2 ms 600 KB Output is correct
6 Correct 1 ms 456 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 89 ms 12736 KB Output is correct
13 Correct 442 ms 22868 KB Output is correct
14 Correct 247 ms 12332 KB Output is correct
15 Correct 179 ms 13716 KB Output is correct
16 Correct 93 ms 13188 KB Output is correct
17 Correct 179 ms 23228 KB Output is correct
18 Correct 241 ms 22840 KB Output is correct
19 Correct 152 ms 22440 KB Output is correct
20 Correct 89 ms 13248 KB Output is correct
21 Correct 182 ms 22716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 444 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 444 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 500 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 2 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 2 ms 680 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 600 KB Output is correct
21 Correct 2 ms 604 KB Output is correct
22 Correct 1 ms 456 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 448 KB Output is correct
3 Correct 0 ms 444 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 2 ms 788 KB Output is correct
16 Correct 1 ms 448 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 2 ms 348 KB Output is correct
20 Correct 1 ms 560 KB Output is correct
21 Correct 2 ms 604 KB Output is correct
22 Correct 1 ms 600 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 88 ms 12900 KB Output is correct
26 Correct 107 ms 12888 KB Output is correct
27 Correct 433 ms 22216 KB Output is correct
28 Correct 487 ms 23740 KB Output is correct
29 Correct 442 ms 23740 KB Output is correct
30 Correct 193 ms 12812 KB Output is correct
31 Correct 89 ms 13756 KB Output is correct
32 Correct 163 ms 23892 KB Output is correct
33 Correct 221 ms 23480 KB Output is correct
34 Correct 157 ms 23464 KB Output is correct
35 Correct 87 ms 14024 KB Output is correct
36 Correct 185 ms 23652 KB Output is correct
37 Correct 82 ms 12832 KB Output is correct