답안 #668725

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
668725 2022-12-04T17:26:25 Z ljuba Palembang Bridges (APIO15_bridge) C++17
100 / 100
131 ms 8784 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);

        dbg(p, rsum, lsum);

        suf[i] = rsum - lsum;
    }

    ll dodaj = pref[n];

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

    dbg(dodaj);

    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 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 1 ms 340 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 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 392 KB Output is correct
8 Correct 1 ms 340 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
12 Correct 56 ms 6476 KB Output is correct
13 Correct 121 ms 6372 KB Output is correct
14 Correct 110 ms 6048 KB Output is correct
15 Correct 68 ms 3544 KB Output is correct
16 Correct 75 ms 6364 KB Output is correct
17 Correct 99 ms 6456 KB Output is correct
18 Correct 100 ms 6316 KB Output is correct
19 Correct 114 ms 6388 KB Output is correct
20 Correct 69 ms 6308 KB Output is correct
21 Correct 102 ms 6376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 320 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 324 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 328 KB Output is correct
3 Correct 0 ms 320 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 324 KB Output is correct
13 Correct 1 ms 328 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 328 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 2 ms 336 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 324 KB Output is correct
8 Correct 1 ms 324 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 1 ms 324 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 55 ms 7176 KB Output is correct
26 Correct 107 ms 7232 KB Output is correct
27 Correct 123 ms 8140 KB Output is correct
28 Correct 129 ms 8720 KB Output is correct
29 Correct 131 ms 8612 KB Output is correct
30 Correct 83 ms 4488 KB Output is correct
31 Correct 68 ms 8020 KB Output is correct
32 Correct 102 ms 8640 KB Output is correct
33 Correct 111 ms 8316 KB Output is correct
34 Correct 129 ms 8784 KB Output is correct
35 Correct 67 ms 8276 KB Output is correct
36 Correct 108 ms 8492 KB Output is correct
37 Correct 51 ms 7132 KB Output is correct