Submission #1158330

#TimeUsernameProblemLanguageResultExecution timeMemory
1158330SangPalembang Bridges (APIO15_bridge)C++20
100 / 100
139 ms12164 KiB
#include<bits/stdc++.h>
using namespace std;

#define int long long
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(), (a).end()
#define task "kbsiudthw"
#define sz(a) (int)(a).size()

typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<int, ii> pii;

const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 2277;

int k, n;

multiset<int> L, R;
int sL, sR;

void Insert(int x){
    if (L.empty() || *L.rbegin() >= x) L.insert(x), sL += x;
    else R.insert(x), sR += x;
    if (sz(L) - sz(R) > 1){
        R.insert(*L.rbegin());
        sR += *L.rbegin();
        sL -= *L.rbegin();
        L.erase(--L.end());
    }
    while (sz(R) - sz(L) > 0){
        L.insert(*R.begin());
        sL += *R.begin();
        sR -= *R.begin();
        R.erase(R.begin());
    }
}

int getMedian(){
    return *L.rbegin();
}

signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    if (fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    cin >> k >> n;
    vector<ii> a;
    int ans = 0;
    FOR (i, 1, n){
        char p, q;
        int s, t; cin >> p >> s >> q >>t;
        if (q == p) ans += abs(s - t);
        else a.pb({s, t});
    }
    sort(ALL(a), [&](ii &A, ii &B){
        return A.fi + A.se < B.fi + B.se;
    });
    n = sz(a);
    vi pref(n+2, 0);
    FOR (i, 1, n){
        Insert(a[i-1].fi);
        Insert(a[i-1].se);
        int m = getMedian();
        pref[i] = sz(L)*m - sL + sR - sz(R)*m;
    }
    ans += n;
    if (k == 1) return cout << ans + pref[n], 0;
    sL = sR = 0;
    L.clear(); R.clear();
    int ans2 = pref[n];
    FORD(i, n, 1){
        Insert(a[i-1].fi);
        Insert(a[i-1].se);
        int m = getMedian();
        ans2 = min(ans2, sz(L) * m - sL + sR - sz(R) * m + pref[i-1]);
    }
    cout << ans + ans2 << '\n';

    return 0;
}

Compilation message (stderr)

bridge.cpp: In function 'int main()':
bridge.cpp:52:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   52 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:53:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   53 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...