Submission #970974

# Submission time Handle Problem Language Result Execution time Memory
970974 2024-04-27T16:19:17 Z njoop Palembang Bridges (APIO15_bridge) C++14
22 / 100
104 ms 12904 KB
#include <bits/stdc++.h>
#define ll long long
using namespace std;

int n, k, in1, in2, mxl, mnr;
char c1, c2;
vector<pair<int, int>> v;
multiset<int> s1, s2;
ll pref[200010], suff[200010], su1, su2, res, ans;

bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
    return a.first+a.second < b.first+b.second;
}

void insertVal(int in) {
    if(s1.size() <= s2.size()) {
        s1.insert(in);
        su1 += in;
    } else {
        s2.insert(in);
        su2 += in;
    }
    if(s1.size() && s2.size() && *s1.rbegin() > *s2.begin()) {
        mxl = *s1.rbegin();
        mnr = *s2.begin();
        su1 += mnr - mxl;
        su2 += mxl - mnr;
        s1.insert(mnr);
        s1.erase(s1.find(mxl));
        s2.insert(mxl);
        s2.erase(s2.find(mnr));
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin >> k >> n;
    for(int i=0; i<n; i++) {
        cin >> c1 >> in1 >> c2 >> in2;
        if(c1 == c2) {
            res += abs(in1 - in2);
            continue;
        }
        v.push_back({in1, in2});
    }
    sort(v.begin(), v.end(), cmp);
    res += v.size();
    for(int i=0; i<v.size(); i++) {
        insertVal(v[i].first);
        insertVal(v[i].second);
        pref[i] = (s1.size()-s2.size())*(*s1.rbegin()) - su1 + su2;
    }
    ans = pref[v.size()-1];
    if(k == 1) {
        cout << ans+res;
        return 0;
    }
    for(int i=v.size()-1; i>=0; i--) {
        insertVal(v[i].first);
        insertVal(v[i].second);
        suff[i] = (s1.size()-s2.size())*(*s1.rbegin()) - su1 + su2;
    }
    for(int i=0; i<v.size()-2; i++) {
        ans = min(ans, pref[i]+suff[i+1]);
    }
    cout << ans+res;
    return 0;
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:48:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |     for(int i=0; i<v.size(); i++) {
      |                  ~^~~~~~~~~
bridge.cpp:63:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |     for(int i=0; i<v.size()-2; i++) {
      |                  ~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 2468 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 380 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 1 ms 2392 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 1 ms 2392 KB Output is correct
12 Correct 63 ms 12752 KB Output is correct
13 Correct 98 ms 12748 KB Output is correct
14 Correct 87 ms 11636 KB Output is correct
15 Correct 57 ms 8400 KB Output is correct
16 Correct 61 ms 12748 KB Output is correct
17 Correct 84 ms 12740 KB Output is correct
18 Correct 60 ms 12904 KB Output is correct
19 Correct 92 ms 12624 KB Output is correct
20 Correct 75 ms 12748 KB Output is correct
21 Correct 104 ms 12836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 600 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 604 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 604 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -