Submission #1125144

#TimeUsernameProblemLanguageResultExecution timeMemory
1125144wibulordPalembang Bridges (APIO15_bridge)C++20
100 / 100
78 ms3004 KiB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb emplace_back
#define ii pair<int, int>
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define sz(s) (int)((s).size())
#define all(a) a.begin(), a.end()
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define F0R(i, b) for (int i = 0, _b = (b); i < _b; ++i)
#define FORd(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define F0Rd(i, b) for (int i = (b)-1; i >= 0; i--)
#define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
using namespace std;

template<typename T1,typename T2> bool ckmax(T1 &x,const T2 &y){if(x<y){x=y; return 1;} return 0;}
template<typename T1,typename T2> bool ckmin(T1 &x,const T2 &y){if(y<x){x=y; return 1;} return 0;}

const int MOD = (int)1e9 + 7;
const int mod = 998244353;
const int N = 2e5 + 10, M = 18;
const long long INF = (long long)4e18 + 11;

/*
     /\_/\
    (= ._.)
    / >?  \>$
*/

int n, m, k;
ii A[N];
long long sumL, sumR, cntL, cntR;
long long pref[N];
priority_queue<int> L;
priority_queue<int, vector<int>, greater<int>> R;

void add(int x){
    if((int)L.size() == 0 || x <= L.top()){
        L.push(x);
        sumL += x, cntL++;
        if(sz(L) > sz(R) + 1){
            sumR += L.top(), cntR++;
            sumL -= L.top(); cntL--;
            R.push(L.top());
            L.pop();
        }
    }
    else{
        R.push(x);
        sumR += x, cntR++;
        if(sz(L) < sz(R)){
            sumL += R.top(), cntL++;
            sumR -= R.top(); cntR--;
            L.push(R.top());
            R.pop();
        }
    }
}

void sol(void){
    cin >> k >> n;
    long long sum = 0;
    FOR(i, 1, n){
        char t1, t2;
        int s, t; cin >> t1 >> s >> t2 >> t;
        if(s < t) swap(s, t);
        if(t1 == t2) sum += s - t;
        else A[++m] = ii(s, t);
    }
    if(m == 0) return void(cout << sum << '\n');
    n = m;
    sort(A+1,A+n+1, [&](const ii &x, const ii &y){
        return x.fi + x.se < y.fi + y.se;
    });

//    FOR(i, 1, n) cout << A[i].fi << ' ' << A[i].se << '\n';

    for(int i = 1; i <= n; i++){
        auto &[x, y] = A[i];
        add(x); add(y);
        int med = L.top();
        pref[i] = sumR - med * cntR + med * cntL - sumL;
    }
    if(k == 1) cout << sum + pref[n] + n << '\n';
    if(k == 2){
        while(sz(L)) L.pop();
        while(sz(R)) R.pop();
        sumL = sumR = cntL = cntR = 0;
        long long res = INF;
        for(int i = n; i >= 1; i--){
            auto &[x, y] = A[i];
            add(x); add(y);
            int med = L.top();
            res = min(res, sum + pref[i-1] + sumR - med * cntR + med * cntL - sumL + n);
        }
        cout << res << '\n';
    }
}

signed main(void){
    #define TASK "nhap"
    if(fopen(TASK".inp", "r")){
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    int t = 1;
//    cin >> t;
    while(t--) sol();
    cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << " ms\n";
}

Compilation message (stderr)

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