Submission #401461

# Submission time Handle Problem Language Result Execution time Memory
401461 2021-05-10T10:05:39 Z BERNARB01 Palembang Bridges (APIO15_bridge) C++17
100 / 100
79 ms 9052 KB
#include <bits/stdc++.h>

using namespace std;

template<typename T>
class twoPitchers {
private:
    priority_queue<T> lower;
    priority_queue<T, vector<T>, greater<T>> upper;
    T upperSum, lowerSum;
public:
    twoPitchers() { // O(1)
        upperSum = 0;
        lowerSum = 0;
    }
    void upd(const T& val) { // O(log(n))
        if (lower.empty()) {
            lower.push(val);
            lowerSum += val;
            return;
        }
        T med = lower.top();
        if (val <= med) {
            lower.push(val);
            lowerSum += val;
            if (lower.size() > upper.size() + 1) {
                upper.push(med);
                upperSum += med;
                lower.pop();
                lowerSum -= med;
            }
        } else {
            upper.push(val);
            upperSum += val;
            if (upper.size() > lower.size()) {
                med = upper.top();
                lower.push(med);
                lowerSum += med;
                upper.pop();
                upperSum -= med;
            }
        }
    }
    T qry() { // O(log(n))
        T med = lower.top();
        T lowerDist = med * lower.size() - lowerSum;
        T upperDist = upperSum - med * upper.size();
        return lowerDist + upperDist;
    }
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int k, n;
	cin >> k >> n;
	if (k == 1) {
		long long same = 0;
		vector<int> pa;
		for (int i = 0; i < n; i++) {
			char z, zz;
			int p, pp;
			cin >> z >> p >> zz >> pp;
			if (z == zz) {
				same += abs(p - pp);
			} else {
				pa.push_back(p);
				pa.push_back(pp);
			}
		}
		if (!pa.empty()) {
			sort(pa.begin(), pa.end());
			int loc = pa[pa.size() / 2];
			for (int i = 0; i < (int) pa.size(); i++) {
				same += abs(pa[i] - loc);
			}
			same += (pa.size() / 2);
		}
		cout << same << '\n';
		return 0;
	}
	long long same = 0;
	vector<array<int, 3>> pa;
	for (int i = 0; i < n; i++) {
		char z, zz;
		int p, pp;
		cin >> z >> p >> zz >> pp;
		if (z == zz) {
			same += abs(p - pp);
		} else {
			pa.emplace_back(array<int, 3>{p + pp, p, pp});
		}
	}
	if (!pa.empty()) {
		n = pa.size();
		sort(pa.begin(), pa.end());
		twoPitchers<long long> L, R;
		vector<long long> costR(n);
		for (int i = n - 1; i >= 0; i--) {
			R.upd(pa[i][1]);
			R.upd(pa[i][2]);
			costR[i] = n - i + R.qry();
		}
		long long res = costR[0];
		for (int i = 0; i + 1 < n; i++) {
			L.upd(pa[i][1]);
			L.upd(pa[i][2]);
			res = min(res, i + 1 + L.qry() + costR[i + 1]);
		}
		same += res;
	}
	cout << same << '\n';
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 328 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 312 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 328 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 1 ms 360 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 356 KB Output is correct
12 Correct 23 ms 2208 KB Output is correct
13 Correct 46 ms 3516 KB Output is correct
14 Correct 34 ms 2496 KB Output is correct
15 Correct 27 ms 2212 KB Output is correct
16 Correct 32 ms 2840 KB Output is correct
17 Correct 34 ms 3472 KB Output is correct
18 Correct 40 ms 3048 KB Output is correct
19 Correct 49 ms 3596 KB Output is correct
20 Correct 40 ms 3068 KB Output is correct
21 Correct 42 ms 3296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 316 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 316 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 316 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 328 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 312 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 368 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 45 ms 7448 KB Output is correct
26 Correct 62 ms 7408 KB Output is correct
27 Correct 77 ms 8760 KB Output is correct
28 Correct 79 ms 8672 KB Output is correct
29 Correct 79 ms 8680 KB Output is correct
30 Correct 41 ms 4832 KB Output is correct
31 Correct 55 ms 8372 KB Output is correct
32 Correct 59 ms 9052 KB Output is correct
33 Correct 71 ms 8724 KB Output is correct
34 Correct 68 ms 9008 KB Output is correct
35 Correct 54 ms 8672 KB Output is correct
36 Correct 66 ms 8896 KB Output is correct
37 Correct 40 ms 7532 KB Output is correct