Submission #668008

# Submission time Handle Problem Language Result Execution time Memory
668008 2022-12-02T14:17:44 Z chanhchuong123 Palembang Bridges (APIO15_bridge) C++14
100 / 100
82 ms 5744 KB
#include <bits/stdc++.h>
using namespace std;
#define task "C"
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
  	if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
  	if (a < b) {a = b; return true;} return false;
}

const int N = 1e5 + 4;
int n, k;
long long sameSide;
vector<pair<int, int>> a;

void solve1() {
	vector<int> v;
	for (int i = 0; i < a.size(); i++) {
		v.push_back(a[i].fi);
		v.push_back(a[i].se);
	}
	sort(all(v));
	long long sum = 0;
	int median = v[v.size() / 2];
	for (int i = 0; i < a.size(); i++) {
		sum += abs(a[i].fi - median);
		sum += abs(a[i].se - median);
	}
	cout << sum + sameSide + a.size();
}


long long lsum, rsum, pre[N]; priority_queue<int> pql;
priority_queue<int, vector<int>, greater<int>> pqr;

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

	if (pqr.size() + 1 < pql.size()) {
		int change = pql.top();
		pql.pop();
		pqr.push(change);
		lsum -= change;
		rsum += change;
	} else if (pql.size() < pqr.size()) {
		int change = pqr.top();
		pqr.pop();
		pql.push(change);
		lsum += change;
		rsum -= change;
	}
}

void solve2() {
	sort(all(a), [](pair<int, int> &u, pair<int, int> &v) {
		return u.fi + u.se < v.fi + v.se;
	});

	for (int i = 0; i < a.size(); i++) {
		insert(a[i].fi); 
		insert(a[i].se);
		pre[i] = rsum - lsum;
	}

	while (pql.size()) pql.pop();
	while (pqr.size()) pqr.pop();
	lsum = 0; rsum = 0;

	long long ans = pre[a.size() - 1];
	for (int i = a.size() - 1; i > 0; i--) {
		insert(a[i].fi); insert(a[i].se);
		mini(ans, pre[i - 1] + rsum - lsum);
	}
	cout << ans + sameSide + a.size();
}

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

  	if (fopen(task".inp","r")) {
  		freopen(task".inp","r",stdin);
   		freopen(task".out","w",stdout);
 	}

 	cin >> k >> n;
 	for (int i = 1; i <= n; i++) {
 		char P, Q; int S, T;
 		cin >> P >> S >> Q >> T;
 		if (P == Q) sameSide += abs(S - T);
 		else a.push_back(make_pair(S, T));
 	}
 	if (k == 1) solve1();
 	else solve2();
}

Compilation message

bridge.cpp: In function 'void solve1()':
bridge.cpp:23:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   23 |  for (int i = 0; i < a.size(); i++) {
      |                  ~~^~~~~~~~~~
bridge.cpp:30:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |  for (int i = 0; i < a.size(); i++) {
      |                  ~~^~~~~~~~~~
bridge.cpp: In function 'void solve2()':
bridge.cpp:71:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |  for (int i = 0; i < a.size(); i++) {
      |                  ~~^~~~~~~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:94:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   94 |     freopen(task".inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:95:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |      freopen(task".out","w",stdout);
      |      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory 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 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 1 ms 480 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 288 KB Output is correct
4 Correct 1 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 1 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
12 Correct 19 ms 3492 KB Output is correct
13 Correct 40 ms 4868 KB Output is correct
14 Correct 29 ms 3780 KB Output is correct
15 Correct 24 ms 3020 KB Output is correct
16 Correct 29 ms 4284 KB Output is correct
17 Correct 29 ms 4972 KB Output is correct
18 Correct 34 ms 4556 KB Output is correct
19 Correct 38 ms 4968 KB Output is correct
20 Correct 27 ms 4484 KB Output is correct
21 Correct 40 ms 4724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 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 332 KB Output is correct
9 Correct 1 ms 328 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 336 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 324 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 368 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 340 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 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 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 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 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 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 340 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 340 KB Output is correct
20 Correct 1 ms 352 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 360 KB Output is correct
25 Correct 33 ms 3916 KB Output is correct
26 Correct 62 ms 4316 KB Output is correct
27 Correct 78 ms 5124 KB Output is correct
28 Correct 82 ms 5744 KB Output is correct
29 Correct 82 ms 5472 KB Output is correct
30 Correct 41 ms 3056 KB Output is correct
31 Correct 40 ms 4756 KB Output is correct
32 Correct 62 ms 5468 KB Output is correct
33 Correct 64 ms 5392 KB Output is correct
34 Correct 65 ms 5492 KB Output is correct
35 Correct 42 ms 5352 KB Output is correct
36 Correct 64 ms 5112 KB Output is correct
37 Correct 27 ms 3868 KB Output is correct