답안 #15866

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
15866 2015-07-31T17:11:01 Z myungwoo Palembang Bridges (APIO15_bridge) C++14
100 / 100
86 ms 6324 KB
#include <bits/stdc++.h>
using namespace std;

#define MAXN 100005
#define pb push_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
typedef long long lld;

int K, N, M;
lld D[MAXN], E[MAXN];

struct Z{
	int a, b;
} A[MAXN];

void proc(lld V[])
{
	if (!N) return;
	lld val = abs(A[1].a - A[1].b);
	priority_queue <int> p, q;
	int mid = A[1].a;
	if (A[1].b < mid) p.push(A[1].b);
	else q.push(-A[1].b);
	V[1] = val;
	for (int i=2;i<=N;i++){
		val += abs(mid - A[i].a) + abs(mid - A[i].b);
		if (A[i].a < mid) p.push(A[i].a);
		else q.push(-A[i].a);
		if (A[i].b < mid) p.push(A[i].b);
		else q.push(-A[i].b);
		while (sz(p) < sz(q)){
			int nxt = -q.top(); q.pop();
			p.push(mid);
			val += (lld)(nxt - mid) * sz(p) - (lld)(nxt - mid) * (sz(q) + 1);
			mid = nxt;
		}
		while (sz(p) > sz(q)+1){
			int nxt = p.top(); p.pop();
			q.push(-mid);
			val += (lld)(mid - nxt) * sz(q) - (lld)(mid - nxt) * (sz(p) + 1);
			mid = nxt;
		}
		V[i] = val;
	}
}

int main()
{
	scanf("%d%d", &K, &M);
	vector <int> arr;
	lld def = 0;
	while (M--){
		char p, q; int a, b;
		scanf(" %c%d %c%d", &p, &a, &q, &b);
		if (p == q){ def += abs(a-b); continue; }
		A[++N] = {a, b};
	}
	sort(A+1, A+N+1, [](const Z &a, const Z &b){
		return a.a+a.b < b.a+b.b;
	});
	proc(D);
	if (K == 1){ printf("%lld\n", D[N] + def + N); return 0; }
	reverse(A+1, A+N+1);
	proc(E);
	lld ans = 1e18;
	for (int i=0;i<=N;i++) ans = min(ans, def + D[i] + E[N-i] + N);
	printf("%lld\n", ans);
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:50:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &K, &M);
                       ^
bridge.cpp:55:38: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf(" %c%d %c%d", &p, &a, &q, &b);
                                      ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 4368 KB Output is correct
2 Correct 0 ms 4368 KB Output is correct
3 Correct 0 ms 4368 KB Output is correct
4 Correct 0 ms 4368 KB Output is correct
5 Correct 0 ms 4368 KB Output is correct
6 Correct 0 ms 4368 KB Output is correct
7 Correct 0 ms 4368 KB Output is correct
8 Correct 0 ms 4368 KB Output is correct
9 Correct 0 ms 4368 KB Output is correct
10 Correct 0 ms 4368 KB Output is correct
11 Correct 0 ms 4368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 4368 KB Output is correct
2 Correct 0 ms 4368 KB Output is correct
3 Correct 0 ms 4368 KB Output is correct
4 Correct 0 ms 4368 KB Output is correct
5 Correct 0 ms 4368 KB Output is correct
6 Correct 0 ms 4368 KB Output is correct
7 Correct 0 ms 4368 KB Output is correct
8 Correct 0 ms 4368 KB Output is correct
9 Correct 0 ms 4368 KB Output is correct
10 Correct 0 ms 4368 KB Output is correct
11 Correct 0 ms 4368 KB Output is correct
12 Correct 33 ms 5820 KB Output is correct
13 Correct 66 ms 5736 KB Output is correct
14 Correct 49 ms 5736 KB Output is correct
15 Correct 36 ms 5184 KB Output is correct
16 Correct 33 ms 5820 KB Output is correct
17 Correct 53 ms 5820 KB Output is correct
18 Correct 43 ms 5820 KB Output is correct
19 Correct 63 ms 5820 KB Output is correct
20 Correct 46 ms 5820 KB Output is correct
21 Correct 63 ms 5820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 4368 KB Output is correct
2 Correct 0 ms 4368 KB Output is correct
3 Correct 0 ms 4368 KB Output is correct
4 Correct 0 ms 4368 KB Output is correct
5 Correct 0 ms 4368 KB Output is correct
6 Correct 0 ms 4368 KB Output is correct
7 Correct 0 ms 4368 KB Output is correct
8 Correct 0 ms 4368 KB Output is correct
9 Correct 0 ms 4368 KB Output is correct
10 Correct 0 ms 4368 KB Output is correct
11 Correct 0 ms 4368 KB Output is correct
12 Correct 0 ms 4368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 4368 KB Output is correct
2 Correct 0 ms 4368 KB Output is correct
3 Correct 0 ms 4368 KB Output is correct
4 Correct 0 ms 4368 KB Output is correct
5 Correct 0 ms 4368 KB Output is correct
6 Correct 0 ms 4368 KB Output is correct
7 Correct 0 ms 4368 KB Output is correct
8 Correct 0 ms 4368 KB Output is correct
9 Correct 0 ms 4368 KB Output is correct
10 Correct 0 ms 4368 KB Output is correct
11 Correct 0 ms 4368 KB Output is correct
12 Correct 0 ms 4368 KB Output is correct
13 Correct 0 ms 4368 KB Output is correct
14 Correct 0 ms 4368 KB Output is correct
15 Correct 0 ms 4368 KB Output is correct
16 Correct 0 ms 4368 KB Output is correct
17 Correct 0 ms 4368 KB Output is correct
18 Correct 0 ms 4368 KB Output is correct
19 Correct 0 ms 4368 KB Output is correct
20 Correct 0 ms 4368 KB Output is correct
21 Correct 0 ms 4368 KB Output is correct
22 Correct 0 ms 4368 KB Output is correct
23 Correct 0 ms 4368 KB Output is correct
24 Correct 0 ms 4368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 4368 KB Output is correct
2 Correct 0 ms 4368 KB Output is correct
3 Correct 0 ms 4368 KB Output is correct
4 Correct 0 ms 4368 KB Output is correct
5 Correct 0 ms 4368 KB Output is correct
6 Correct 0 ms 4368 KB Output is correct
7 Correct 0 ms 4368 KB Output is correct
8 Correct 0 ms 4368 KB Output is correct
9 Correct 0 ms 4368 KB Output is correct
10 Correct 0 ms 4368 KB Output is correct
11 Correct 0 ms 4368 KB Output is correct
12 Correct 0 ms 4368 KB Output is correct
13 Correct 0 ms 4368 KB Output is correct
14 Correct 0 ms 4368 KB Output is correct
15 Correct 0 ms 4368 KB Output is correct
16 Correct 0 ms 4368 KB Output is correct
17 Correct 0 ms 4368 KB Output is correct
18 Correct 0 ms 4368 KB Output is correct
19 Correct 0 ms 4368 KB Output is correct
20 Correct 0 ms 4368 KB Output is correct
21 Correct 0 ms 4368 KB Output is correct
22 Correct 0 ms 4368 KB Output is correct
23 Correct 0 ms 4368 KB Output is correct
24 Correct 0 ms 4368 KB Output is correct
25 Correct 39 ms 5820 KB Output is correct
26 Correct 43 ms 5808 KB Output is correct
27 Correct 69 ms 5808 KB Output is correct
28 Correct 83 ms 5808 KB Output is correct
29 Correct 79 ms 5808 KB Output is correct
30 Correct 39 ms 5296 KB Output is correct
31 Correct 33 ms 5820 KB Output is correct
32 Correct 86 ms 5820 KB Output is correct
33 Correct 53 ms 5820 KB Output is correct
34 Correct 76 ms 5820 KB Output is correct
35 Correct 66 ms 6324 KB Output is correct
36 Correct 76 ms 5820 KB Output is correct
37 Correct 36 ms 5820 KB Output is correct