Submission #175952

# Submission time Handle Problem Language Result Execution time Memory
175952 2020-01-07T12:38:34 Z dolphingarlic Palembang Bridges (APIO15_bridge) C++14
100 / 100
111 ms 5740 KB
#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;
 
bool cmp(pair<int, int> a, pair<int, int> b) {
	return a.first + a.second < b.first + b.second;
}

priority_queue<int> lpq;
priority_queue<int, vector<int>, greater<int>> rpq;
ll lsum, rsum;

void insert(int x) {
	int median = (lpq.size() ? lpq.top() : 1000000001);
	if (x <= median) {
		lpq.push(x);
		lsum += x;
	} else {
		rpq.push(x);
		rsum += x;
	}

	if (rpq.size() + 1 < lpq.size()) {
		int nxt = lpq.top();
		lpq.pop();
		rpq.push(nxt);
		lsum -= nxt;
		rsum += nxt;
	} else if (lpq.size() < rpq.size()) {
		int nxt = rpq.top();
		rpq.pop();
		lpq.push(nxt);
		rsum -= nxt;
		lsum += nxt;
	}
}

ll pref[100001];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int k, n;
    ll same_side = 0;
    vector<pair<int, int>> v = {{0, 0}};
    cin >> k >> n;
    FOR(i, 0, n) {
        char a, b;
        int x, y;
        cin >> a >> x >> b >> y;
        if (a == b) same_side += abs(x - y);
        else v.push_back({x, y});
    }
    sort(v.begin(), v.end(), cmp);
    n = v.size() - 1;
    same_side += n;
    
    lsum = rsum = 0;
    FOR(i, 1, n + 1) {
    	insert(v[i].first);
    	insert(v[i].second);
    	pref[i] = rsum - lsum;
    }

	ll ans = pref[n];
	
    if (k == 2) {
        while (lpq.size()) lpq.pop();
        while (rpq.size()) rpq.pop();
        lsum = rsum = 0;
        
        for (int i = n; i; i--) {
        	insert(v[i].first);
        	insert(v[i].second);
        	ans = min(ans, rsum - lsum + pref[i - 1]);
        }
    }
    cout << same_side + ans;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 504 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 3 ms 376 KB Output is correct
8 Correct 3 ms 376 KB Output is correct
9 Correct 3 ms 400 KB Output is correct
10 Correct 3 ms 432 KB Output is correct
11 Correct 6 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 380 KB Output is correct
3 Correct 2 ms 380 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 3 ms 380 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 380 KB Output is correct
12 Correct 38 ms 3976 KB Output is correct
13 Correct 70 ms 5612 KB Output is correct
14 Correct 55 ms 4640 KB Output is correct
15 Correct 42 ms 3572 KB Output is correct
16 Correct 45 ms 4884 KB Output is correct
17 Correct 58 ms 5488 KB Output is correct
18 Correct 58 ms 5512 KB Output is correct
19 Correct 64 ms 5484 KB Output is correct
20 Correct 49 ms 5388 KB Output is correct
21 Correct 61 ms 5228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 380 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 380 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 380 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 3 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 380 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 3 ms 376 KB Output is correct
15 Correct 3 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 3 ms 404 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 3 ms 376 KB Output is correct
21 Correct 3 ms 376 KB Output is correct
22 Correct 3 ms 376 KB Output is correct
23 Correct 3 ms 376 KB Output is correct
24 Correct 3 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 420 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 3 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 380 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 3 ms 376 KB Output is correct
15 Correct 3 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 1 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 3 ms 376 KB Output is correct
21 Correct 3 ms 376 KB Output is correct
22 Correct 3 ms 376 KB Output is correct
23 Correct 3 ms 376 KB Output is correct
24 Correct 3 ms 376 KB Output is correct
25 Correct 51 ms 3948 KB Output is correct
26 Correct 82 ms 4588 KB Output is correct
27 Correct 105 ms 5232 KB Output is correct
28 Correct 110 ms 5740 KB Output is correct
29 Correct 111 ms 5616 KB Output is correct
30 Correct 58 ms 3060 KB Output is correct
31 Correct 56 ms 4844 KB Output is correct
32 Correct 86 ms 5484 KB Output is correct
33 Correct 87 ms 5108 KB Output is correct
34 Correct 93 ms 5488 KB Output is correct
35 Correct 64 ms 5488 KB Output is correct
36 Correct 89 ms 5300 KB Output is correct
37 Correct 43 ms 4172 KB Output is correct