답안 #15671

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
15671 2015-07-14T10:08:25 Z cki86201 Palembang Bridges (APIO15_bridge) C++
100 / 100
393 ms 12892 KB
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<set>
using namespace std;

typedef long long ll;
int k, n, np;
ll ans;
struct Pair{
	Pair(){}
	Pair(int x, int y) :x(x), y(y){}
	int x, y;
	bool operator<(const Pair &l)const{
		return x + y < l.x + l.y;
	}
}p[100010];
ll dp[2][100010];

multiset <int> S;
multiset <int>::iterator it, jt;

void Do(int t){
	S.clear();
	S.insert(p[0].x), S.insert(p[0].y);
	it = S.begin();
	dp[t][0] = abs(p[0].x - p[0].y);
	for (int i = 1; i < np; i++){
		int cnt = (*it <= p[i].x) + (*it <= p[i].y);
		int delta = abs(*it - p[i].x) + abs(*it - p[i].y);
		S.insert(p[i].x), S.insert(p[i].y);
		if (cnt == 0)jt = it--;
		else if (cnt == 2){
			jt = it++;
			delta -= 2 * (*it - *jt);
		}
		dp[t][i] = dp[t][i - 1] + delta;
	}
}

int main(){
	scanf("%d%d", &k, &n);
	for (int i = 0; i < n; i++){
		char a[2], b[2];
		int c, d;
		scanf("%s%d%s%d", a, &c, b, &d);
		if (a[0] == b[0])ans += abs(c - d);
		else p[np++] = Pair(c, d);
	}
	sort(p, p + np);
	Do(0);
	if (k == 1)printf("%lld", np + ans + dp[0][np - 1]);
	else{
		reverse(p, p + np);
		Do(1);
		ll mn = min(dp[1][np - 1], dp[0][np - 1]);
		for (int i = 0; i < np - 1; i++){
			mn = min(mn, dp[0][i] + dp[1][np - i - 2]);
		}
		printf("%lld", mn + ans + np);
	}
	return 0;
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:43:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &k, &n);
                       ^
bridge.cpp:47:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s%d%s%d", a, &c, b, &d);
                                  ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 3520 KB Output is correct
2 Correct 0 ms 3520 KB Output is correct
3 Correct 0 ms 3652 KB Output is correct
4 Correct 0 ms 3652 KB Output is correct
5 Correct 0 ms 3652 KB Output is correct
6 Correct 0 ms 3652 KB Output is correct
7 Correct 0 ms 3652 KB Output is correct
8 Correct 0 ms 3652 KB Output is correct
9 Correct 0 ms 3652 KB Output is correct
10 Correct 0 ms 3652 KB Output is correct
11 Correct 0 ms 3652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 3520 KB Output is correct
2 Correct 0 ms 3520 KB Output is correct
3 Correct 0 ms 3652 KB Output is correct
4 Correct 0 ms 3652 KB Output is correct
5 Correct 0 ms 3652 KB Output is correct
6 Correct 0 ms 3652 KB Output is correct
7 Correct 0 ms 3652 KB Output is correct
8 Correct 0 ms 3652 KB Output is correct
9 Correct 0 ms 3652 KB Output is correct
10 Correct 0 ms 3652 KB Output is correct
11 Correct 0 ms 3652 KB Output is correct
12 Correct 83 ms 12892 KB Output is correct
13 Correct 193 ms 12892 KB Output is correct
14 Correct 149 ms 11968 KB Output is correct
15 Correct 109 ms 9064 KB Output is correct
16 Correct 99 ms 12892 KB Output is correct
17 Correct 103 ms 12892 KB Output is correct
18 Correct 93 ms 12892 KB Output is correct
19 Correct 113 ms 12892 KB Output is correct
20 Correct 119 ms 12892 KB Output is correct
21 Correct 133 ms 12892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 3520 KB Output is correct
2 Correct 0 ms 3520 KB Output is correct
3 Correct 0 ms 3520 KB Output is correct
4 Correct 0 ms 3520 KB Output is correct
5 Correct 0 ms 3520 KB Output is correct
6 Correct 0 ms 3520 KB Output is correct
7 Correct 0 ms 3520 KB Output is correct
8 Correct 0 ms 3520 KB Output is correct
9 Correct 0 ms 3520 KB Output is correct
10 Correct 0 ms 3520 KB Output is correct
11 Correct 0 ms 3520 KB Output is correct
12 Correct 0 ms 3520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 3520 KB Output is correct
2 Correct 0 ms 3520 KB Output is correct
3 Correct 0 ms 3520 KB Output is correct
4 Correct 0 ms 3520 KB Output is correct
5 Correct 0 ms 3520 KB Output is correct
6 Correct 0 ms 3520 KB Output is correct
7 Correct 0 ms 3520 KB Output is correct
8 Correct 0 ms 3520 KB Output is correct
9 Correct 0 ms 3520 KB Output is correct
10 Correct 0 ms 3520 KB Output is correct
11 Correct 0 ms 3520 KB Output is correct
12 Correct 0 ms 3520 KB Output is correct
13 Correct 0 ms 3652 KB Output is correct
14 Correct 0 ms 3652 KB Output is correct
15 Correct 0 ms 3652 KB Output is correct
16 Correct 0 ms 3520 KB Output is correct
17 Correct 0 ms 3652 KB Output is correct
18 Correct 0 ms 3520 KB Output is correct
19 Correct 0 ms 3652 KB Output is correct
20 Correct 0 ms 3652 KB Output is correct
21 Correct 0 ms 3652 KB Output is correct
22 Correct 0 ms 3652 KB Output is correct
23 Correct 0 ms 3652 KB Output is correct
24 Correct 0 ms 3652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 3520 KB Output is correct
2 Correct 0 ms 3520 KB Output is correct
3 Correct 0 ms 3520 KB Output is correct
4 Correct 0 ms 3520 KB Output is correct
5 Correct 0 ms 3520 KB Output is correct
6 Correct 0 ms 3520 KB Output is correct
7 Correct 0 ms 3520 KB Output is correct
8 Correct 0 ms 3520 KB Output is correct
9 Correct 0 ms 3520 KB Output is correct
10 Correct 0 ms 3520 KB Output is correct
11 Correct 0 ms 3520 KB Output is correct
12 Correct 0 ms 3520 KB Output is correct
13 Correct 0 ms 3652 KB Output is correct
14 Correct 0 ms 3652 KB Output is correct
15 Correct 0 ms 3652 KB Output is correct
16 Correct 0 ms 3520 KB Output is correct
17 Correct 0 ms 3652 KB Output is correct
18 Correct 0 ms 3520 KB Output is correct
19 Correct 0 ms 3652 KB Output is correct
20 Correct 0 ms 3652 KB Output is correct
21 Correct 0 ms 3652 KB Output is correct
22 Correct 0 ms 3652 KB Output is correct
23 Correct 0 ms 3652 KB Output is correct
24 Correct 0 ms 3652 KB Output is correct
25 Correct 129 ms 12892 KB Output is correct
26 Correct 243 ms 12892 KB Output is correct
27 Correct 383 ms 12892 KB Output is correct
28 Correct 393 ms 12892 KB Output is correct
29 Correct 373 ms 12892 KB Output is correct
30 Correct 143 ms 8536 KB Output is correct
31 Correct 149 ms 12892 KB Output is correct
32 Correct 169 ms 12892 KB Output is correct
33 Correct 113 ms 12892 KB Output is correct
34 Correct 149 ms 12892 KB Output is correct
35 Correct 139 ms 12892 KB Output is correct
36 Correct 163 ms 12892 KB Output is correct
37 Correct 146 ms 12892 KB Output is correct