Submission #1035958

# Submission time Handle Problem Language Result Execution time Memory
1035958 2024-07-26T21:28:40 Z abyyskit Wiring (IOI17_wiring) C++14
100 / 100
48 ms 21440 KB
/*
Idea:
- dp[i] is the best answer if we have already connected first i sockets
- a socket can always be connected to the closest socket of other color
- we can also make a cluster of sockets where we have the same number of sockets of each color
(cost for a cluster can be easily computed using prefix sums)
- using two methods above we cover all possible transitions of dp
*/

#include "wiring.h"
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF (2e9)
#define MOD (1000 * 1000 * 1000 + 7)
#define maxn 500111

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;

vector<pll> arr;
vector<ll> b, r;
ll prer[maxn], preb[maxn], dp[maxn];
int prv[maxn], pos[maxn];

ll find_nearest(pll x){
	ll ret = INF;
	if(x.se == -1){
		auto it = lower_bound(b.begin(), b.end(), x.fi);
		if(it != b.end())
			ret = min(ret, *it - x.fi);

		if(it != b.begin()){
			it--;
			ret = min(ret, x.fi - *it);
		}
	}

	if(x.se == 1){
		auto it = lower_bound(r.begin(), r.end(), x.fi);
		if(it != r.end())
			ret = min(ret, *it - x.fi);

		if(it != r.begin()){
			it--;
			ret = min(ret, x.fi - *it);
		}
	}
	return ret;
}

long long min_total_length(vector<int> a, vector<int> c){
	int n = a.size() + c.size();

	arr.pb(mp(-1, 0));
	for(int x : a){
		r.pb(x);
		arr.pb(mp(x, -1));
	}

	for(int x : c){
		b.pb(x);
		arr.pb(mp(x, 1));
	}

	sort(arr.begin(), arr.end());
	memset(prv, -1, sizeof(prv));
	memset(pos, -1, sizeof(pos));

	prv[n] = 0;
	int balance = n;
	for(int i = 1; i <= n; i++){
		prer[i] = prer[i - 1];
		preb[i] = preb[i - 1];

		if(arr[i].se == -1){
			prer[i] += arr[i].fi;
			balance--;
		}

		if(arr[i].se == 1){
			preb[i] += arr[i].fi;
			balance++;
		}

		if(prv[balance] >= 0)
			pos[i] = prv[balance];

		prv[balance] = i;
	}

	dp[0] = 0;
	for(int i = 1; i <= n; i++){
		dp[i] = dp[i - 1] + find_nearest(arr[i]);
		if(pos[i] >= 0)
			dp[i] = min(dp[i], dp[pos[i]] + abs((prer[i] - prer[pos[i]]) - (preb[i] - preb[pos[i]])));
	}
	return dp[n];
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 1 ms 4700 KB Output is correct
11 Correct 1 ms 4700 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 1 ms 4700 KB Output is correct
15 Correct 1 ms 4700 KB Output is correct
16 Correct 1 ms 4700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4188 KB Output is correct
2 Correct 1 ms 4188 KB Output is correct
3 Correct 23 ms 15588 KB Output is correct
4 Correct 23 ms 15812 KB Output is correct
5 Correct 22 ms 18364 KB Output is correct
6 Correct 29 ms 19156 KB Output is correct
7 Correct 28 ms 19136 KB Output is correct
8 Correct 29 ms 20412 KB Output is correct
9 Correct 30 ms 20412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 43 ms 18120 KB Output is correct
4 Correct 42 ms 17852 KB Output is correct
5 Correct 1 ms 4696 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 1 ms 4700 KB Output is correct
11 Correct 1 ms 4856 KB Output is correct
12 Correct 1 ms 4512 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 1 ms 4540 KB Output is correct
15 Correct 1 ms 4700 KB Output is correct
16 Correct 1 ms 4700 KB Output is correct
17 Correct 1 ms 4700 KB Output is correct
18 Correct 44 ms 18116 KB Output is correct
19 Correct 44 ms 18268 KB Output is correct
20 Correct 45 ms 18108 KB Output is correct
21 Correct 42 ms 19112 KB Output is correct
22 Correct 48 ms 18360 KB Output is correct
23 Correct 44 ms 18136 KB Output is correct
24 Correct 44 ms 19168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Correct 32 ms 18416 KB Output is correct
3 Correct 36 ms 17600 KB Output is correct
4 Correct 34 ms 18048 KB Output is correct
5 Correct 36 ms 17848 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 1 ms 4700 KB Output is correct
11 Correct 1 ms 4700 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 1 ms 4700 KB Output is correct
15 Correct 1 ms 4700 KB Output is correct
16 Correct 1 ms 4184 KB Output is correct
17 Correct 1 ms 4700 KB Output is correct
18 Correct 31 ms 18304 KB Output is correct
19 Correct 35 ms 17452 KB Output is correct
20 Correct 33 ms 17608 KB Output is correct
21 Correct 33 ms 18368 KB Output is correct
22 Correct 30 ms 17856 KB Output is correct
23 Correct 32 ms 19400 KB Output is correct
24 Correct 31 ms 18964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4700 KB Output is correct
10 Correct 1 ms 4700 KB Output is correct
11 Correct 1 ms 4700 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 1 ms 4700 KB Output is correct
15 Correct 1 ms 4700 KB Output is correct
16 Correct 1 ms 4700 KB Output is correct
17 Correct 1 ms 4188 KB Output is correct
18 Correct 1 ms 4188 KB Output is correct
19 Correct 23 ms 15588 KB Output is correct
20 Correct 23 ms 15812 KB Output is correct
21 Correct 22 ms 18364 KB Output is correct
22 Correct 29 ms 19156 KB Output is correct
23 Correct 28 ms 19136 KB Output is correct
24 Correct 29 ms 20412 KB Output is correct
25 Correct 30 ms 20412 KB Output is correct
26 Correct 1 ms 4700 KB Output is correct
27 Correct 1 ms 4700 KB Output is correct
28 Correct 43 ms 18120 KB Output is correct
29 Correct 42 ms 17852 KB Output is correct
30 Correct 1 ms 4696 KB Output is correct
31 Correct 1 ms 4700 KB Output is correct
32 Correct 1 ms 4700 KB Output is correct
33 Correct 1 ms 4700 KB Output is correct
34 Correct 1 ms 4700 KB Output is correct
35 Correct 1 ms 4700 KB Output is correct
36 Correct 1 ms 4856 KB Output is correct
37 Correct 1 ms 4512 KB Output is correct
38 Correct 1 ms 4700 KB Output is correct
39 Correct 1 ms 4540 KB Output is correct
40 Correct 1 ms 4700 KB Output is correct
41 Correct 1 ms 4700 KB Output is correct
42 Correct 1 ms 4700 KB Output is correct
43 Correct 44 ms 18116 KB Output is correct
44 Correct 44 ms 18268 KB Output is correct
45 Correct 45 ms 18108 KB Output is correct
46 Correct 42 ms 19112 KB Output is correct
47 Correct 48 ms 18360 KB Output is correct
48 Correct 44 ms 18136 KB Output is correct
49 Correct 44 ms 19168 KB Output is correct
50 Correct 1 ms 4696 KB Output is correct
51 Correct 32 ms 18416 KB Output is correct
52 Correct 36 ms 17600 KB Output is correct
53 Correct 34 ms 18048 KB Output is correct
54 Correct 36 ms 17848 KB Output is correct
55 Correct 1 ms 4700 KB Output is correct
56 Correct 1 ms 4700 KB Output is correct
57 Correct 1 ms 4700 KB Output is correct
58 Correct 1 ms 4700 KB Output is correct
59 Correct 1 ms 4700 KB Output is correct
60 Correct 1 ms 4700 KB Output is correct
61 Correct 1 ms 4700 KB Output is correct
62 Correct 1 ms 4700 KB Output is correct
63 Correct 1 ms 4700 KB Output is correct
64 Correct 1 ms 4700 KB Output is correct
65 Correct 1 ms 4184 KB Output is correct
66 Correct 1 ms 4700 KB Output is correct
67 Correct 31 ms 18304 KB Output is correct
68 Correct 35 ms 17452 KB Output is correct
69 Correct 33 ms 17608 KB Output is correct
70 Correct 33 ms 18368 KB Output is correct
71 Correct 30 ms 17856 KB Output is correct
72 Correct 32 ms 19400 KB Output is correct
73 Correct 31 ms 18964 KB Output is correct
74 Correct 31 ms 20392 KB Output is correct
75 Correct 36 ms 19044 KB Output is correct
76 Correct 30 ms 21436 KB Output is correct
77 Correct 32 ms 18880 KB Output is correct
78 Correct 31 ms 18604 KB Output is correct
79 Correct 33 ms 18584 KB Output is correct
80 Correct 30 ms 19140 KB Output is correct
81 Correct 33 ms 20156 KB Output is correct
82 Correct 36 ms 19900 KB Output is correct
83 Correct 29 ms 18876 KB Output is correct
84 Correct 31 ms 19436 KB Output is correct
85 Correct 36 ms 20436 KB Output is correct
86 Correct 34 ms 20160 KB Output is correct
87 Correct 37 ms 20420 KB Output is correct
88 Correct 35 ms 20424 KB Output is correct
89 Correct 32 ms 19456 KB Output is correct
90 Correct 37 ms 20412 KB Output is correct
91 Correct 34 ms 21440 KB Output is correct
92 Correct 33 ms 19132 KB Output is correct
93 Correct 33 ms 21436 KB Output is correct
94 Correct 31 ms 19912 KB Output is correct
95 Correct 33 ms 19248 KB Output is correct
96 Correct 31 ms 20904 KB Output is correct
97 Correct 36 ms 19244 KB Output is correct
98 Correct 36 ms 19132 KB Output is correct
99 Correct 36 ms 19896 KB Output is correct
100 Correct 31 ms 20412 KB Output is correct
101 Correct 41 ms 19648 KB Output is correct
102 Correct 32 ms 19900 KB Output is correct