Submission #466666

# Submission time Handle Problem Language Result Execution time Memory
466666 2021-08-20T03:41:56 Z cgiosy Palembang Bridges (APIO15_bridge) C++17
100 / 100
90 ms 3132 KB
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using PQ=priority_queue<int>;

struct pii {
	int l, r;
	bool operator<(pii b) const { return l+r<b.l+b.r; }
};
int main() {
	int K, N, M=0;
	scanf("%d%d", &K, &N);
	pii A[N];
	ll B[N], s=0, w=0;
	for(int i=0; i<N; i++) {
		char a, b;
		int l, r;
		scanf(" %c%d %c%d", &a, &l, &b, &r);
		if(l>r) swap(l, r);
		if(a!=b) A[M++]={l, r};
		else s+=r-l;
	}
	s+=N=M;
	sort(A, A+N);

	PQ L, R;
	auto add=[&](pii v) {
		L.push(v.l), R.push(-v.r);
		w+=v.r-v.l;
		if(L.top()>-R.top()) {
			int l=L.top(), r=-R.top();
			L.pop(), R.pop();
			L.push(r), R.push(-l);
			w+=(l-r)*2;
		}
		return w;
	};

	for(int i=0; i<N; i++) B[i]=add(A[i]);
	w=0;
	PQ().swap(L);
	PQ().swap(R);
	ll t=N ? B[N-1] : 0;
	if(N && K==2) for(int i=N; --i;) t=min(t, add(A[i])+B[i-1]);
	printf("%lld\n", s+t);
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:12:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 |  scanf("%d%d", &K, &N);
      |  ~~~~~^~~~~~~~~~~~~~~~
bridge.cpp:18:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |   scanf(" %c%d %c%d", &a, &l, &b, &r);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 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 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 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 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 36 ms 2844 KB Output is correct
13 Correct 64 ms 2724 KB Output is correct
14 Correct 54 ms 2572 KB Output is correct
15 Correct 37 ms 1836 KB Output is correct
16 Correct 39 ms 2740 KB Output is correct
17 Correct 64 ms 2732 KB Output is correct
18 Correct 41 ms 2752 KB Output is correct
19 Correct 70 ms 2844 KB Output is correct
20 Correct 51 ms 2880 KB Output is correct
21 Correct 67 ms 2852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 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 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 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 0 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 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 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 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 272 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 45 ms 3092 KB Output is correct
26 Correct 66 ms 3132 KB Output is correct
27 Correct 84 ms 3080 KB Output is correct
28 Correct 84 ms 3044 KB Output is correct
29 Correct 90 ms 3084 KB Output is correct
30 Correct 44 ms 1768 KB Output is correct
31 Correct 42 ms 2984 KB Output is correct
32 Correct 84 ms 3092 KB Output is correct
33 Correct 46 ms 3096 KB Output is correct
34 Correct 90 ms 3092 KB Output is correct
35 Correct 62 ms 2984 KB Output is correct
36 Correct 90 ms 3004 KB Output is correct
37 Correct 34 ms 2984 KB Output is correct