답안 #32065

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
32065 2017-09-24T06:17:14 Z imeimi2000 Roller Coaster Railroad (IOI16_railroad) C++14
100 / 100
436 ms 48736 KB
#include "railroad.h"
#include <algorithm>

using namespace std;
typedef long long llong;
typedef pair<int, int> pi;

int n;
vector<int> comp;
int bsearch(int x) {
	int s = 0, e = comp.size() - 1, m;
	while (s < e) {
		m = (s + e) / 2;
		if (comp[m] == x) return m;
		if (comp[m] < x) s = m + 1;
		else e = m - 1;
	}
	return s;
}

int degree[400000];
int group[400000];
int par[400000];
vector<int> edge[400000];
pi mst[400000];

void dfs(int x, int s) {
	group[x] = s;
	for (int i : edge[x]) {
		if (group[i] > 0) continue;
		dfs(i, s);
	}
}

int find(int x) {
	if (par[x]) return par[x] = find(par[x]);
	return x;
}

long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
    n = s.size();
	comp.resize(n << 1);
	for (int i = 0; i < n; ++i) {
		comp[i << 1] = s[i];
		comp[i << 1 | 1] = t[i];
	}
	sort(comp.begin(), comp.end());
	comp.erase(unique(comp.begin(), comp.end()), comp.end());
	for (int i = 0; i < n; ++i) {
		s[i] = bsearch(s[i]);
		t[i] = bsearch(t[i]);
		++degree[s[i]];
		--degree[t[i]];
		edge[s[i]].push_back(t[i]);
	}
	int need;
	llong ans = 0ll;
	for (int i = 0; i + 1 < comp.size(); ++i) {
		need = (i == 0) - degree[i];
		degree[i + 1] -= need;
		degree[i] += need;
		if (need > 0) {
			edge[i].push_back(i + 1);
		}
		else if (need < 0) {
			edge[i + 1].push_back(i);
			ans -= 1ll * need * (comp[i + 1] - comp[i]);
		}
	}
	for (int i = 0, j = 0; i < comp.size(); ++i) {
		if (group[i]) continue;
		dfs(i, ++j);
	}
	int k = 0;
	for (int i = 0; i + 1 < comp.size(); ++i) {
		if (group[i] != group[i + 1]) {
			mst[k++] = { comp[i + 1] - comp[i], i };
		}
	}
	sort(mst, mst + k);
	for (int i = 0; i < k; ++i) {
		int x = find(group[mst[i].second]), y = find(group[mst[i].second + 1]);
		if (x == y) continue;
		ans += mst[i].first;
		par[x] = y;
	}
    return ans;
}

Compilation message

railroad.cpp: In function 'long long int plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:58:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i + 1 < comp.size(); ++i) {
                        ^
railroad.cpp:70:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0, j = 0; i < comp.size(); ++i) {
                           ^
railroad.cpp:75:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i + 1 < comp.size(); ++i) {
                        ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 19120 KB n = 2
2 Correct 3 ms 19120 KB n = 2
3 Correct 6 ms 19120 KB n = 2
4 Correct 3 ms 19120 KB n = 2
5 Correct 3 ms 19120 KB n = 2
6 Correct 3 ms 19120 KB n = 2
7 Correct 6 ms 19120 KB n = 3
8 Correct 3 ms 19120 KB n = 3
9 Correct 3 ms 19120 KB n = 3
10 Correct 3 ms 19120 KB n = 8
11 Correct 3 ms 19120 KB n = 8
12 Correct 0 ms 19120 KB n = 8
13 Correct 3 ms 19120 KB n = 8
14 Correct 3 ms 19120 KB n = 8
15 Correct 0 ms 19120 KB n = 8
16 Correct 0 ms 19120 KB n = 8
17 Correct 0 ms 19120 KB n = 8
18 Correct 0 ms 19120 KB n = 8
19 Correct 3 ms 19120 KB n = 3
20 Correct 3 ms 19120 KB n = 7
21 Correct 0 ms 19120 KB n = 8
22 Correct 3 ms 19120 KB n = 8
23 Correct 3 ms 19120 KB n = 8
24 Correct 0 ms 19120 KB n = 8
25 Correct 3 ms 19120 KB n = 8
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 19120 KB n = 2
2 Correct 3 ms 19120 KB n = 2
3 Correct 6 ms 19120 KB n = 2
4 Correct 3 ms 19120 KB n = 2
5 Correct 3 ms 19120 KB n = 2
6 Correct 3 ms 19120 KB n = 2
7 Correct 6 ms 19120 KB n = 3
8 Correct 3 ms 19120 KB n = 3
9 Correct 3 ms 19120 KB n = 3
10 Correct 3 ms 19120 KB n = 8
11 Correct 3 ms 19120 KB n = 8
12 Correct 0 ms 19120 KB n = 8
13 Correct 3 ms 19120 KB n = 8
14 Correct 3 ms 19120 KB n = 8
15 Correct 0 ms 19120 KB n = 8
16 Correct 0 ms 19120 KB n = 8
17 Correct 0 ms 19120 KB n = 8
18 Correct 0 ms 19120 KB n = 8
19 Correct 3 ms 19120 KB n = 3
20 Correct 3 ms 19120 KB n = 7
21 Correct 0 ms 19120 KB n = 8
22 Correct 3 ms 19120 KB n = 8
23 Correct 3 ms 19120 KB n = 8
24 Correct 0 ms 19120 KB n = 8
25 Correct 3 ms 19120 KB n = 8
26 Correct 0 ms 19120 KB n = 8
27 Correct 0 ms 19120 KB n = 8
28 Correct 0 ms 19120 KB n = 8
29 Correct 3 ms 19120 KB n = 16
30 Correct 3 ms 19120 KB n = 16
31 Correct 0 ms 19120 KB n = 16
32 Correct 0 ms 19120 KB n = 16
33 Correct 0 ms 19120 KB n = 16
34 Correct 0 ms 19120 KB n = 16
35 Correct 0 ms 19120 KB n = 16
36 Correct 0 ms 19120 KB n = 15
37 Correct 0 ms 19120 KB n = 8
38 Correct 0 ms 19120 KB n = 16
39 Correct 6 ms 19120 KB n = 16
40 Correct 0 ms 19120 KB n = 9
41 Correct 0 ms 19120 KB n = 16
42 Correct 6 ms 19120 KB n = 16
43 Correct 6 ms 19120 KB n = 16
44 Correct 0 ms 19120 KB n = 9
45 Correct 3 ms 19120 KB n = 15
46 Correct 0 ms 19120 KB n = 16
47 Correct 3 ms 19120 KB n = 16
48 Correct 0 ms 19120 KB n = 16
# 결과 실행 시간 메모리 Grader output
1 Correct 413 ms 48732 KB n = 199999
2 Correct 433 ms 41604 KB n = 199991
3 Correct 389 ms 48264 KB n = 199993
4 Correct 299 ms 35876 KB n = 152076
5 Correct 176 ms 29456 KB n = 93249
6 Correct 326 ms 37236 KB n = 199910
7 Correct 369 ms 41932 KB n = 199999
8 Correct 343 ms 39056 KB n = 199997
9 Correct 349 ms 38268 KB n = 171294
10 Correct 246 ms 34604 KB n = 140872
11 Correct 326 ms 37392 KB n = 199886
12 Correct 383 ms 41760 KB n = 199996
13 Correct 339 ms 39724 KB n = 200000
14 Correct 376 ms 44512 KB n = 199998
15 Correct 333 ms 42424 KB n = 200000
16 Correct 329 ms 42512 KB n = 199998
17 Correct 343 ms 42484 KB n = 200000
18 Correct 376 ms 41712 KB n = 190000
19 Correct 283 ms 45372 KB n = 177777
20 Correct 156 ms 30148 KB n = 100000
21 Correct 413 ms 41576 KB n = 200000
22 Correct 399 ms 36360 KB n = 200000
23 Correct 436 ms 48732 KB n = 200000
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 19120 KB n = 2
2 Correct 3 ms 19120 KB n = 2
3 Correct 6 ms 19120 KB n = 2
4 Correct 3 ms 19120 KB n = 2
5 Correct 3 ms 19120 KB n = 2
6 Correct 3 ms 19120 KB n = 2
7 Correct 6 ms 19120 KB n = 3
8 Correct 3 ms 19120 KB n = 3
9 Correct 3 ms 19120 KB n = 3
10 Correct 3 ms 19120 KB n = 8
11 Correct 3 ms 19120 KB n = 8
12 Correct 0 ms 19120 KB n = 8
13 Correct 3 ms 19120 KB n = 8
14 Correct 3 ms 19120 KB n = 8
15 Correct 0 ms 19120 KB n = 8
16 Correct 0 ms 19120 KB n = 8
17 Correct 0 ms 19120 KB n = 8
18 Correct 0 ms 19120 KB n = 8
19 Correct 3 ms 19120 KB n = 3
20 Correct 3 ms 19120 KB n = 7
21 Correct 0 ms 19120 KB n = 8
22 Correct 3 ms 19120 KB n = 8
23 Correct 3 ms 19120 KB n = 8
24 Correct 0 ms 19120 KB n = 8
25 Correct 3 ms 19120 KB n = 8
26 Correct 0 ms 19120 KB n = 8
27 Correct 0 ms 19120 KB n = 8
28 Correct 0 ms 19120 KB n = 8
29 Correct 3 ms 19120 KB n = 16
30 Correct 3 ms 19120 KB n = 16
31 Correct 0 ms 19120 KB n = 16
32 Correct 0 ms 19120 KB n = 16
33 Correct 0 ms 19120 KB n = 16
34 Correct 0 ms 19120 KB n = 16
35 Correct 0 ms 19120 KB n = 16
36 Correct 0 ms 19120 KB n = 15
37 Correct 0 ms 19120 KB n = 8
38 Correct 0 ms 19120 KB n = 16
39 Correct 6 ms 19120 KB n = 16
40 Correct 0 ms 19120 KB n = 9
41 Correct 0 ms 19120 KB n = 16
42 Correct 6 ms 19120 KB n = 16
43 Correct 6 ms 19120 KB n = 16
44 Correct 0 ms 19120 KB n = 9
45 Correct 3 ms 19120 KB n = 15
46 Correct 0 ms 19120 KB n = 16
47 Correct 3 ms 19120 KB n = 16
48 Correct 0 ms 19120 KB n = 16
49 Correct 413 ms 48732 KB n = 199999
50 Correct 433 ms 41604 KB n = 199991
51 Correct 389 ms 48264 KB n = 199993
52 Correct 299 ms 35876 KB n = 152076
53 Correct 176 ms 29456 KB n = 93249
54 Correct 326 ms 37236 KB n = 199910
55 Correct 369 ms 41932 KB n = 199999
56 Correct 343 ms 39056 KB n = 199997
57 Correct 349 ms 38268 KB n = 171294
58 Correct 246 ms 34604 KB n = 140872
59 Correct 326 ms 37392 KB n = 199886
60 Correct 383 ms 41760 KB n = 199996
61 Correct 339 ms 39724 KB n = 200000
62 Correct 376 ms 44512 KB n = 199998
63 Correct 333 ms 42424 KB n = 200000
64 Correct 329 ms 42512 KB n = 199998
65 Correct 343 ms 42484 KB n = 200000
66 Correct 376 ms 41712 KB n = 190000
67 Correct 283 ms 45372 KB n = 177777
68 Correct 156 ms 30148 KB n = 100000
69 Correct 413 ms 41576 KB n = 200000
70 Correct 399 ms 36360 KB n = 200000
71 Correct 436 ms 48732 KB n = 200000
72 Correct 386 ms 48728 KB n = 200000
73 Correct 413 ms 41648 KB n = 200000
74 Correct 363 ms 47108 KB n = 200000
75 Correct 359 ms 48732 KB n = 200000
76 Correct 366 ms 48736 KB n = 200000
77 Correct 313 ms 36148 KB n = 200000
78 Correct 276 ms 30024 KB n = 200000
79 Correct 429 ms 38288 KB n = 184307
80 Correct 113 ms 27552 KB n = 76040
81 Correct 279 ms 37376 KB n = 199981
82 Correct 263 ms 41744 KB n = 199994
83 Correct 289 ms 39688 KB n = 199996
84 Correct 333 ms 44496 KB n = 199998
85 Correct 296 ms 42416 KB n = 200000
86 Correct 309 ms 42524 KB n = 199998
87 Correct 349 ms 42484 KB n = 200000
88 Correct 369 ms 42864 KB n = 200000
89 Correct 333 ms 48732 KB n = 200000
90 Correct 376 ms 41568 KB n = 200000
91 Correct 409 ms 36360 KB n = 200000
92 Correct 426 ms 48732 KB n = 200000