답안 #668710

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
668710 2022-12-04T16:05:10 Z ljuba Palembang Bridges (APIO15_bridge) C++17
22 / 100
123 ms 6616 KB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
 
typedef vector<int> vi;
typedef vector<ll> vll;
 
typedef vector<vi> vvi;
typedef vector<vll> vvll;
 
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
typedef vector<pii> vpii;
typedef vector<pll> vpll;
 
typedef vector<vpii> vvpii;
typedef vector<vpll> vvpll;
 
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define fi first
#define se second
 
template<class T> bool ckmin(T &a, const T &b) {return a > b ? a = b, 1 : 0;}
template<class T> bool ckmax(T &a, const T &b) {return a < b ? a = b, 1 : 0;}
 
namespace debug {
    void __print(int x) {cerr << x;}
    void __print(long long x) {cerr << x;}
    void __print(double x) {cerr << x;}
    void __print(long double x) {cerr << x;}
    void __print(char x) {cerr << '\'' << x << '\'';}
    void __print(const string &x) {cerr << '\"' << x << '\"';}
    void __print(const char *x) {cerr << '\"' << x << '\"';}
    void __print(bool x) {cerr << (x ? "true" : "false");}
 
    template<typename T, typename V>
    void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
    template<typename T>
    void __print(const T &x) {int f = 0; cerr << '{'; for(auto z : x) cerr << (f++ ? "," : ""), __print(z); cerr << "}";}
    void _print() {cerr << "]\n";}
    template <typename T, typename... V>
    void _print(T t, V... v) {__print(t); if(sizeof...(v)) cerr << ", "; _print(v...);}
 
#ifdef DEBUG
#define dbg(x...) cerr << "\e[91m" << "LINE(" << __LINE__ << ") -> " << "[" << #x << "] = ["; _print(x); cerr << "\033[0m";
#else
#define dbg(x...)
#endif
}
 
using namespace debug;
 
const char nl = '\n';
 
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

void solve() {
    int k, n;
    cin >> k >> n;

    ll ans = 0;

    vpll v;

    for(int i = 0; i < n; ++i) {
        char a, b;
        ll x, y;
        cin >> a >> x >> b >> y;

        if(a == b) {
            ans += abs(x - y);
        } else {
            v.pb({x, y});
            ++ans;
        }
    }

    sort(all(v), [](pll a, pll b) {
        return a.fi + a.se < b.fi + b.se;
    });

    dbg(v);

    priority_queue<ll> levi;
    priority_queue<ll, vll, greater<>> desni;

    ll lsum = 0, rsum = 0;

    auto insert = [&](ll x) {
        levi.push(x);
        lsum += x;

        while(sz(levi) > sz(desni)) {
            ll naj = levi.top();
            levi.pop();

            lsum -= naj;
            rsum += naj;
            desni.push(naj);
        }

        //sz(levi) <= sz(desni)

        while(sz(levi) < sz(desni)) {
            ll naj =  desni.top();
            desni.pop();

            rsum -= naj;
            lsum += naj;
            levi.push(naj);
        }
    };

    auto obrisi = [&]() {
        while(sz(levi)) levi.pop();
        while(sz(desni)) desni.pop();
        lsum = 0;
        rsum = 0;
    };

    n = sz(v);
    vll pref(n + 2), suf(n + 2);

    for(int i = 1; i <= n; ++i) {
        auto p = v[i - 1];
        insert(p.fi);
        insert(p.se);

        pref[i] = rsum - lsum;
    }

    obrisi();

    for(int i = n; i >= 1; --i) {
        auto p = v[i - 1];
        insert(p.fi);
        insert(p.se);

        suf[i] = rsum - lsum;
    }

    dbg(pref);

    ll dodaj = 1e18 + 12;

    if(k == 2) {
        for(int i = 1; i <= n; ++i) {
            ckmin(dodaj, pref[i] + suf[i + 1]);
        }
    } else {
        dodaj = pref[n];
    }


    ans += dodaj;

    cout << ans << nl;
}
 
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
 
    int testCases = 1;
    //cin >> testCases;
    while(testCases--)
        solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 2 ms 336 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 58 ms 6520 KB Output is correct
13 Correct 123 ms 6616 KB Output is correct
14 Correct 103 ms 6036 KB Output is correct
15 Correct 72 ms 3604 KB Output is correct
16 Correct 65 ms 6524 KB Output is correct
17 Correct 100 ms 6520 KB Output is correct
18 Correct 102 ms 6444 KB Output is correct
19 Correct 118 ms 6404 KB Output is correct
20 Correct 70 ms 6516 KB Output is correct
21 Correct 105 ms 6456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -