답안 #229349

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
229349 2020-05-04T09:49:34 Z AM1648 Palembang Bridges (APIO15_bridge) C++17
100 / 100
86 ms 10788 KB
/* be name khoda */

#define long_enable
#include <iostream>
#include <algorithm>
#include <cstring>
#include <numeric>
#include <vector>
#include <fstream>
#include <queue>
#include <map>
using namespace std;

#ifdef long_enable
typedef long long int ll;
#else
typedef int ll;
#endif

typedef pair<ll, ll> pii;
typedef pair<pii, ll> ppi;
typedef pair<ll, pii> pip;
typedef vector<ll> vi;
typedef vector<pii> vpii;

#define forifrom(i, s, n) for (ll i = s; i < n; ++i)
#define forirto(i, n, e) for (ll i = (n) - 1; i >= e; --i)
#define fori(i, n) forifrom (i, 0, n)
#define forir(i, n) forirto (i, n, 0)

#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define smin(a, b) a = min(a, (b))
#define smax(a, b) a = max(a, (b))

#define debug(x) cout << #x << " -> " << (x) << endl
#define debug2(x, y) cout << #x << ' ' << #y << " -> " << (x) << ' ' << (y) << endl
#define debug3(x, y, z) cout << #x << ' ' << #y << ' ' << #z << " -> " << (x) << ' ' << (y) << ' ' << (z) << endl
#define debug4(x, y, z, t) cout << #x << ' ' << #y << ' ' << #z << ' ' << #t << " -> " << (x) << ' ' << (y) << ' ' << (z) << ' ' << (t) << endl
#define debugp(x) cout << #x << " -> " << "(" << (x).F << ", " << (x).S << ")" << endl
#define debuga(x, n) cout << #x << " -> "; fori (i1_da, n) { cout << (x)[i1_da] << ' '; } cout << endl
#define debugap(x, n) cout << #x << " ->\n"; fori (i1_dap, n) { cout << "(" << (x)[i1_dap].F << ", " << (x)[i1_dap].S << ")\n"; } cout << endl
#define debugaa(x, n, m) cout << #x << " ->\n"; fori (i1_daa, n) { fori (i2_daa, m) { cout << (x)[i1_daa][i2_daa] << ' '; } cout << '\n'; } cout << endl
#define debugav(x, n) cout << #x << " ->\n"; fori (i1_dav, n) { fori (i2_dav, (x)[i1_dav].size()) { cout << (x)[i1_dav][i2_dav] << ' '; } cout << '\n'; } cout << endl
#define debugia(x, n) cout << #x << " ->\n"; fori (i1_dia, n) { cout << i1_dia << " : " << (x)[i1_dia] << '\n'; } cout << endl

const ll MOD = 1000000007;
const ll INF = 2000000000;
const long long BIG = 1446803456761533460LL;

#define Add(a, b) a = ((a) + (b)) % MOD
#define Mul(a, b) a = (1LL * (a) * (b)) % MOD

// -----------------------------------------------------------------------

const ll maxn = 100010;
const ll maxnlg = 20;

ll K, N, n;
pip pts[maxn];
ll lt[maxn], rt[maxn];

struct MedianCost {
    priority_queue<ll> maxheap;
    priority_queue<ll, vi, greater<ll>> minheap;
    ll sum = 0;
    void add(ll x) {
        if (maxheap.empty() || x < maxheap.top()) {
            if (!maxheap.empty()) sum += maxheap.top() - x;
            maxheap.push(x);
            if ((ll) maxheap.size() - (ll) minheap.size() > 1) {
                ll y = maxheap.top(); maxheap.pop();
                minheap.push(y);
            }
        } else {
            sum += x - maxheap.top();
            minheap.push(x);
            if ((ll) minheap.size() - (ll) maxheap.size() >= 1) {
                ll y = minheap.top(); minheap.pop();
                sum -= y - maxheap.top();
                maxheap.push(y);
            }
        }
    }
    ll cost() {
        return sum;
    }
} ltcost, rtcost;

int main() {
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);

    cin >> K >> N;
    ll same = 0;
    fori (i, N) {
        char a, b; ll x, y; cin >> a >> x >> b >> y;
        if (a == b) {
            same += abs(x - y);
        } else {
            pts[n++] = {x + y, {x, y}};
        }
    }
    sort(pts, pts + n);
    fori (i, n) {
        ltcost.add(pts[i].S.F);
        ltcost.add(pts[i].S.S);
        lt[i] = ltcost.cost();
    }
    forir (i, n) {
        rtcost.add(pts[i].S.F);
        rtcost.add(pts[i].S.S);
        rt[i] = rtcost.cost();
    }
    ll ans = lt[n - 1];
    if (K == 2) {
        fori (i, n - 1) {
            smin(ans, lt[i] + rt[i + 1]);
        }
    }
    cout << ans + same + n << '\n';

}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 512 KB Output is correct
5 Correct 5 ms 512 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 5 ms 512 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 512 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 512 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 512 KB Output is correct
6 Correct 6 ms 512 KB Output is correct
7 Correct 5 ms 512 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 6 ms 512 KB Output is correct
10 Correct 5 ms 512 KB Output is correct
11 Correct 5 ms 512 KB Output is correct
12 Correct 40 ms 9292 KB Output is correct
13 Correct 85 ms 10288 KB Output is correct
14 Correct 70 ms 8804 KB Output is correct
15 Correct 57 ms 6476 KB Output is correct
16 Correct 56 ms 9704 KB Output is correct
17 Correct 65 ms 10328 KB Output is correct
18 Correct 72 ms 9952 KB Output is correct
19 Correct 70 ms 10216 KB Output is correct
20 Correct 54 ms 9828 KB Output is correct
21 Correct 69 ms 10084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 512 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 6 ms 512 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 512 KB Output is correct
20 Correct 5 ms 512 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 5 ms 512 KB Output is correct
23 Correct 5 ms 512 KB Output is correct
24 Correct 5 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 512 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 512 KB Output is correct
20 Correct 5 ms 512 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 5 ms 512 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 512 KB Output is correct
25 Correct 42 ms 9316 KB Output is correct
26 Correct 62 ms 8932 KB Output is correct
27 Correct 79 ms 10080 KB Output is correct
28 Correct 85 ms 10788 KB Output is correct
29 Correct 86 ms 10212 KB Output is correct
30 Correct 46 ms 5772 KB Output is correct
31 Correct 54 ms 9572 KB Output is correct
32 Correct 64 ms 10340 KB Output is correct
33 Correct 72 ms 9932 KB Output is correct
34 Correct 70 ms 10340 KB Output is correct
35 Correct 54 ms 9828 KB Output is correct
36 Correct 68 ms 10084 KB Output is correct
37 Correct 38 ms 8676 KB Output is correct