Submission #660928

#TimeUsernameProblemLanguageResultExecution timeMemory
660928danikoynovPalembang Bridges (APIO15_bridge)C++14
100 / 100
417 ms26916 KiB
#include <bits/stdc++.h>
#define endl '\n'

using namespace std;
typedef long long ll;

void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const ll maxn = 1e6 + 10, smalln = 2010;

struct work
{
    ll x, y;
} w[maxn];

ll k, n;
ll pref[maxn][2];

bool cmp(work w1, work w2)
{
    return w1.x + w1.y < w2.x + w2.y;
}

struct median
{
    multiset < ll > lf, rf;
    ll lf_sum, rf_sum;

    median()
    {
        lf_sum = 0;
        rf_sum = 0;
    }

    void add(ll x)
    {
        lf.insert(x);
        if (!lf.empty() && !rf.empty())
        {
            if (*lf.rbegin() > *rf.begin())
            {
                ll s1 = *lf.rbegin(), s2 = *rf.begin();
                lf_sum = lf_sum + s2 - s1;
                rf_sum = rf_sum + s1 - s2;
                lf.erase(lf.find(s1));
                rf.erase(rf.find(s2));
                lf.insert(s2);
                rf.insert(s1);
            }
        }
        lf_sum += x;

        if (lf.size() - 1 > rf.size())
        {
            lf_sum -= *lf.rbegin();
            rf_sum += *lf.rbegin();
            rf.insert(*lf.rbegin());
            lf.erase(lf.find(*lf.rbegin()));
            ///cout << lf_sum << " : " << rf_sum << " : " << x << endl;
        }
    }

    ll calc()
    {
        return rf_sum - lf_sum;
    }
};

void solve()
{
    cin >> k >> n;
    ll ans = 0;
    vector < ll > v, sx;
    for (ll i = 1; i <= n; i ++)
    {
        char c1, c2;
        cin >> c1 >> w[i].x >> c2 >> w[i].y;
        if (w[i].x > w[i].y)
            swap(w[i].x, w[i].y);
        if (c1 == c2)
        {
            ans = ans + w[i].y - w[i].x;
            i --;
            n --;
        }
        else
        {
            v.push_back(w[i].y);
            v.push_back(w[i].x);
            sx.push_back(w[i].x + w[i].y);
            ans ++;
        }
    }

    //for (ll i = 1; i <= n; i ++)
      //  cout << w[i].x << " - " << w[i].y << endl;
    if (k == 1)
    {

        sort(v.begin(), v.end());
        for (ll i = 0; i < v.size() / 2; i ++)
            ans = ans - (v[i]);
        for (ll i = v.size() / 2; i < v.size(); i ++)
            ans = ans + v[i];

        cout << ans << endl;

    }
    else
    {
        ll best = 1e18;
        if (v.empty())
            best = 0;
        sort(w + 1, w + n + 1, cmp);
        sort(v.begin(), v.end());

        median go_right, go_left;

        for (ll i = 1; i <= n; i ++)
        {
            go_right.add(w[i].x);
            go_right.add(w[i].y);
            pref[i][0] = go_right.calc();
        }

        for (ll i = n; i > 0; i --)
        {
            go_left.add(w[i].x);
            go_left.add(w[i].y);
            pref[i][1] = go_left.calc();
        }

        for (ll i = 0; i <= n; i ++)
        {
            best = min(best, pref[i][0] + pref[i + 1][1]);
        }

        cout << ans + best << endl;

    }


}

int main()
{
    solve();
    return 0;
}
/**
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/

Compilation message (stderr)

bridge.cpp: In function 'void solve()':
bridge.cpp:106:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  106 |         for (ll i = 0; i < v.size() / 2; i ++)
      |                        ~~^~~~~~~~~~~~~~
bridge.cpp:108:37: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  108 |         for (ll i = v.size() / 2; i < v.size(); i ++)
      |                                   ~~^~~~~~~~~~
#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...