Submission #282872

#TimeUsernameProblemLanguageResultExecution timeMemory
282872IgorIPalembang Bridges (APIO15_bridge)C++17
100 / 100
542 ms23928 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll ans0, ans;

vector<pair<int, int> > a;

bool comp(pair<int, int> x, pair<int, int> y)
{
    return x.first + x.second < y.first + y.second;
}

struct MedianGetter{
    set<pair<int, int> > s0, s1;
    int tot = 0;
    ll si0 = 0, si1 = 0;
    void add(int x)
    {
        pair<int, int> elem = {x, tot};
        tot++;
        if (s0.size() == 0)
        {
            s0.insert(elem);
            si0 += x;
            return;
        }
        auto it = s0.end();
        it--;
        if (x <= (*it).first)
        {
            s0.insert(elem);
            si0 += x;
        }
        else
        {
            s1.insert(elem);
            si1 += x;
        }
        while (s0.size() < (tot + 1) / 2)
        {
            pair<int, int> z = *(s1.begin());
            s1.erase(s1.begin());
            si1 -= z.first;
            si0 += z.first;
            s0.insert(z);
        }
        while (s0.size() > (tot + 1) / 2)
        {
            auto it = s0.end();
            it--;
            pair<int, int> z = *(it);
            s0.erase(it);
            si0 -= z.first;
            si1 += z.first;
            s1.insert(z);
        }
    }
    int get()
    {
        auto it = s0.end();
        it--;
        return (*it).first;
    }
};

signed main()
{
    int n, k;
    cin >> k >> n;
    for (int i = 0; i < n; i++)
    {
        string c, d;
        int x, y;
        cin >> c >> x >> d >> y;
        if (c == d) ans0 += abs(x - y);
        else a.push_back({x, y});
    }
    if (a.size() == 0)
    {
        cout << ans0;
        return 0;
    }
    sort(a.begin(), a.end(), comp);
    vector<ll> optpref(a.size()), optsuff(a.size());
    MedianGetter M1;
    for (int i = 0; i < a.size(); i++)
    {
        M1.add(a[i].first);
        M1.add(a[i].second);
        optpref[i] = M1.si1 - M1.si0;
    }
    MedianGetter M2;
    for (int i = (int)a.size() - 1; i >= 0; i--)
    {
        M2.add(a[i].first);
        M2.add(a[i].second);
        optsuff[i] = M2.si1 - M2.si0;
    }
    if (k == 1)
    {
        cout << ans0 + optpref[a.size() - 1] + a.size();
        return 0;
    }
    //for (int i = 0; i < n; i++) cout << optpref[i] << " "; cout << "\n";
    //for (int i = 0; i < n; i++) cout << optsuff[i] << " "; cout << "\n";
    ans = min(optpref[a.size() - 1], optsuff[0]);
    for (int i = 0; i + 1 < a.size(); i++) ans = min(ans, optpref[i] + optsuff[i + 1]);
    cout << ans0 + ans + a.size();
}

Compilation message (stderr)

bridge.cpp: In member function 'void MedianGetter::add(int)':
bridge.cpp:42:26: warning: comparison of integer expressions of different signedness: 'std::set<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   42 |         while (s0.size() < (tot + 1) / 2)
      |                ~~~~~~~~~~^~~~~~~~~~~~~~~
bridge.cpp:50:26: warning: comparison of integer expressions of different signedness: 'std::set<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   50 |         while (s0.size() > (tot + 1) / 2)
      |                ~~~~~~~~~~^~~~~~~~~~~~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:89:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |     for (int i = 0; i < a.size(); i++)
      |                     ~~^~~~~~~~~~
bridge.cpp:110:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |     for (int i = 0; i + 1 < a.size(); i++) ans = min(ans, optpref[i] + optsuff[i + 1]);
      |                     ~~~~~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...