답안 #21833

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
21833 2017-04-26T07:35:31 Z gs12117 Palembang Bridges (APIO15_bridge) C++11
100 / 100
1096 ms 11748 KB
#include<stdio.h>
#include<algorithm>
#include<queue>
int p, tn;
long long int ansd;
struct psn {
	int a, b;
	bool operator<(const psn&r)const {
		return a + b < r.a + r.b;
	}
};
psn pl[100100];
int n;
long long int dp[3][100100];
int cmin[3][100100];
int sl[100100];
long long int ans;
struct pqn {
	int x;
	int idx;
	bool operator<(const pqn&r)const {
		return x < r.x;
	}
};
int dist(int x, int y) {
	if (x < y)return y - x;
	return x - y;
}
int main() {
	scanf("%d%d", &p, &tn);
	for (int i = 0; i < tn; i++) {
		char ap[10];
		char bp[10];
		int a, b;
		scanf("%s%d%s%d", ap, &a, bp, &b);
		if (ap[0] == bp[0]) {
			ansd += dist(a, b);
		}
		else {
			pl[n].a = a;
			pl[n].b = b;
			n++;
		}
	}
	std::sort(pl, pl + n);
	for (int i = 1; i <= n; i++) {
		dp[0][i] = 1e17;
	}
	for (int z = 0; z < p; z++) {
		dp[z + 1][0] = 0;
		cmin[z + 1][0] = 0;
		for (int ii = 17; ii >= 0; ii--) {
			std::priority_queue<pqn> l, r;
			long long int s = 0;
			int j = 0;
			int il, ir;
			il = 0;
			ir = 0;
			int bi = 0;
			for (int ti = (1 << ii); ti <= n; ti += (1 << (ii + 1))) {
				for (int i = bi; i < ti; i++) {
					sl[i] = 0;
					if (l.size() == 0 || l.top().x >= pl[i].a) {
						pqn t;
						t.idx = i;
						t.x = pl[i].a;
						l.push(t);
						sl[i]++;
					}
					else {
						pqn t;
						t.idx = i;
						t.x = -pl[i].a;
						r.push(t);
					}
					if (l.top().x >= pl[i].b) {
						pqn t;
						t.idx = i;
						t.x = pl[i].b;
						l.push(t);
						sl[i]++;
					}
					else {
						pqn t;
						t.idx = i;
						t.x = -pl[i].b;
						r.push(t);
					}
					s += dist(pl[i].a, l.top().x);
					s += dist(pl[i].b, l.top().x);
					if (l.size() - il > r.size() - ir) {
						pqn t = l.top();
						t.x = -t.x;
						sl[t.idx]--;
						r.push(t);
						l.pop();
					}
					if (l.size() - il < r.size() - ir) {
						s += r.top().x + l.top().x;
						s += r.top().x + l.top().x;
						pqn t = r.top();
						t.x = -t.x;
						sl[t.idx]++;
						l.push(t);
						r.pop();
					}
					while (l.top().idx < j) {
						il--;
						l.pop();
					}
					while (r.top().idx < j) {
						ir--;
						r.pop();
					}
				}
				bi = ti;
				int mj;
				if (ti + (1 << ii) > n)mj = ti;
				else mj = cmin[z + 1][ti + (1 << ii)];
				if (mj >= ti)mj = ti - 1;
				dp[z + 1][ti] = s;
				cmin[z + 1][ti] = j;
				while (j < mj) {
					s += dp[z][j + 1] - dp[z][j];
					s -= dist(pl[j].a, l.top().x);
					s -= dist(pl[j].b, l.top().x);
					if (pl[j].a <= l.top().x&&pl[j].b <= l.top().x) {
						s += r.top().x + l.top().x;
						s += r.top().x + l.top().x;
					}
					il += sl[j];
					ir += 2 - sl[j];
					j++;
					while (l.size() > 0 && l.top().idx < j) {
						il--;
						l.pop();
					}
					while (r.size() > 0 && r.top().idx < j) {
						ir--;
						r.pop();
					}
					if (l.size() - il > r.size() - ir) {
						pqn t = l.top();
						t.x = -t.x;
						sl[t.idx]--;
						r.push(t);
						l.pop();
					}
					if (l.size() - il < r.size() - ir) {
						pqn t = r.top();
						t.x = -t.x;
						sl[t.idx]++;
						l.push(t);
						r.pop();
					}
					while (l.top().idx < j) {
						il--;
						l.pop();
					}
					while (r.top().idx < j) {
						ir--;
						r.pop();
					}
					if (dp[z + 1][ti] > s) {
						dp[z + 1][ti] = s;
						cmin[z + 1][ti] = j;
					}
				}
			}
		}
	}
	ans = dp[p][n] + ansd + n;
	printf("%lld", ans);
	return 0;
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:30:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &p, &tn);
                        ^
bridge.cpp:35:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s%d%s%d", ap, &a, bp, &b);
                                    ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 5868 KB Output is correct
2 Correct 0 ms 5868 KB Output is correct
3 Correct 0 ms 5868 KB Output is correct
4 Correct 3 ms 5868 KB Output is correct
5 Correct 0 ms 5868 KB Output is correct
6 Correct 0 ms 5868 KB Output is correct
7 Correct 0 ms 5868 KB Output is correct
8 Correct 0 ms 5868 KB Output is correct
9 Correct 3 ms 5868 KB Output is correct
10 Correct 0 ms 5868 KB Output is correct
11 Correct 0 ms 5868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 5868 KB Output is correct
2 Correct 0 ms 5868 KB Output is correct
3 Correct 0 ms 5868 KB Output is correct
4 Correct 0 ms 5868 KB Output is correct
5 Correct 0 ms 5868 KB Output is correct
6 Correct 0 ms 5868 KB Output is correct
7 Correct 0 ms 5868 KB Output is correct
8 Correct 0 ms 5868 KB Output is correct
9 Correct 0 ms 5868 KB Output is correct
10 Correct 0 ms 5868 KB Output is correct
11 Correct 0 ms 5868 KB Output is correct
12 Correct 246 ms 9876 KB Output is correct
13 Correct 463 ms 11416 KB Output is correct
14 Correct 499 ms 10780 KB Output is correct
15 Correct 253 ms 8848 KB Output is correct
16 Correct 123 ms 8680 KB Output is correct
17 Correct 499 ms 11408 KB Output is correct
18 Correct 246 ms 8876 KB Output is correct
19 Correct 509 ms 11408 KB Output is correct
20 Correct 323 ms 11408 KB Output is correct
21 Correct 489 ms 11408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 5868 KB Output is correct
2 Correct 0 ms 5868 KB Output is correct
3 Correct 0 ms 5868 KB Output is correct
4 Correct 0 ms 5868 KB Output is correct
5 Correct 0 ms 5868 KB Output is correct
6 Correct 0 ms 5868 KB Output is correct
7 Correct 0 ms 5868 KB Output is correct
8 Correct 0 ms 5868 KB Output is correct
9 Correct 0 ms 5868 KB Output is correct
10 Correct 0 ms 5868 KB Output is correct
11 Correct 0 ms 5868 KB Output is correct
12 Correct 0 ms 5868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 5868 KB Output is correct
2 Correct 0 ms 5868 KB Output is correct
3 Correct 0 ms 5868 KB Output is correct
4 Correct 0 ms 5868 KB Output is correct
5 Correct 0 ms 5868 KB Output is correct
6 Correct 0 ms 5868 KB Output is correct
7 Correct 0 ms 5868 KB Output is correct
8 Correct 0 ms 5868 KB Output is correct
9 Correct 0 ms 5868 KB Output is correct
10 Correct 0 ms 5868 KB Output is correct
11 Correct 0 ms 5868 KB Output is correct
12 Correct 0 ms 5868 KB Output is correct
13 Correct 0 ms 5868 KB Output is correct
14 Correct 3 ms 5868 KB Output is correct
15 Correct 3 ms 5868 KB Output is correct
16 Correct 0 ms 5868 KB Output is correct
17 Correct 0 ms 5868 KB Output is correct
18 Correct 0 ms 5868 KB Output is correct
19 Correct 0 ms 5868 KB Output is correct
20 Correct 3 ms 5868 KB Output is correct
21 Correct 0 ms 5868 KB Output is correct
22 Correct 3 ms 5868 KB Output is correct
23 Correct 3 ms 5868 KB Output is correct
24 Correct 3 ms 5868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 5868 KB Output is correct
2 Correct 0 ms 5868 KB Output is correct
3 Correct 0 ms 5868 KB Output is correct
4 Correct 0 ms 5868 KB Output is correct
5 Correct 0 ms 5868 KB Output is correct
6 Correct 0 ms 5868 KB Output is correct
7 Correct 0 ms 5868 KB Output is correct
8 Correct 0 ms 5868 KB Output is correct
9 Correct 0 ms 5868 KB Output is correct
10 Correct 0 ms 5868 KB Output is correct
11 Correct 0 ms 5868 KB Output is correct
12 Correct 0 ms 5868 KB Output is correct
13 Correct 0 ms 5868 KB Output is correct
14 Correct 3 ms 5868 KB Output is correct
15 Correct 3 ms 5868 KB Output is correct
16 Correct 0 ms 5868 KB Output is correct
17 Correct 0 ms 5868 KB Output is correct
18 Correct 0 ms 5868 KB Output is correct
19 Correct 0 ms 5868 KB Output is correct
20 Correct 3 ms 5868 KB Output is correct
21 Correct 0 ms 5868 KB Output is correct
22 Correct 0 ms 5868 KB Output is correct
23 Correct 3 ms 5868 KB Output is correct
24 Correct 3 ms 5868 KB Output is correct
25 Correct 406 ms 9364 KB Output is correct
26 Correct 733 ms 11500 KB Output is correct
27 Correct 1029 ms 11440 KB Output is correct
28 Correct 1012 ms 11748 KB Output is correct
29 Correct 956 ms 11748 KB Output is correct
30 Correct 469 ms 8684 KB Output is correct
31 Correct 203 ms 9708 KB Output is correct
32 Correct 1016 ms 11408 KB Output is correct
33 Correct 306 ms 9716 KB Output is correct
34 Correct 1096 ms 11408 KB Output is correct
35 Correct 679 ms 11408 KB Output is correct
36 Correct 1029 ms 11408 KB Output is correct
37 Correct 569 ms 9356 KB Output is correct