Submission #1047999

# Submission time Handle Problem Language Result Execution time Memory
1047999 2024-08-07T19:55:02 Z Trent Wiring (IOI17_wiring) C++17
100 / 100
25 ms 10048 KB
#include "wiring.h"
#include "bits/stdc++.h"
using namespace std;
#define forR(i, x) for(int i = 0; i < (x); ++i)
#define REP(i, a, b) for(int i = (a); i < (b); ++i)
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

const ll INF = 1e17;

struct grp{
	char ty;
	vi pos;
};
long long min_total_length(std::vector<int> r, std::vector<int> b) {
	int n = r.size(), m = b.size();
	vector<grp> gps;
	int api=0, bpi=0;
	while(api < n || bpi < m) {
		if(api < n && (bpi == m || r[api] < b[bpi])) {
			gps.push_back({'r', {}});
			while(api < n && (bpi == m || r[api] < b[bpi])) {
				gps.back().pos.push_back(r[api]);
				++api;
			}
		} else {
			gps.push_back({'b', {}});
			while(bpi < m && (api == n || r[api] > b[bpi])) {
				gps.back().pos.push_back(b[bpi]);
				++bpi;
			}
		}
	}

	int l = gps.size();
	vi cnt(l);
	forR(i, l) cnt[i] = gps[i].pos.size();
	vvll dp(l);
	forR(i, l) dp[i].resize(cnt[i] + 1);
	forR(i, l) {
		if(i == 0) {
			forR(j, cnt[i]) dp[i][j] = INF;
			dp[i][cnt[i]] = 0;
		} else {
			ll sam = INF;
			vector<ll> aCh(cnt[i-1] + 1), pm(cnt[i-1] + 1);
			ll totRig = 0;
			forR(las, cnt[i-1] + 1) {
				if(las > 0) totRig += gps[i-1].pos.back() - gps[i-1].pos[cnt[i-1] - las];
				ll cTot = dp[i-1][las] + totRig;
				cTot += (ll) las * (gps[i].pos.front() - gps[i-1].pos.back());
				aCh[las] = cTot;
			}
			pm[cnt[i-1]] = aCh[cnt[i-1]];
			for(int j = cnt[i-1] - 1; j >= 0; --j) pm[j] = min(pm[j+1], aCh[j]);
			ll bckTot = 0;
			forR(bck, cnt[i] + 1) {
				if(bck > 0) bckTot += gps[i].pos[bck-1] - gps[i].pos.front();
				if(bck <= cnt[i-1]) {
					sam = min(sam, aCh[bck] - (ll) bck * (gps[i].pos.front() - gps[i-1].pos.back()));
				}
				int fwd = cnt[i] - bck;
				dp[i][fwd] = sam + bckTot + (ll) bck * (gps[i].pos.front() - gps[i-1].pos.back());
				if(bck + 1 <= cnt[i-1]) {
					dp[i][fwd] = min(dp[i][fwd], pm[bck + 1] + bckTot);
				}
			}
		}
	}
	return dp[l-1][0];
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 11 ms 4992 KB Output is correct
4 Correct 11 ms 5076 KB Output is correct
5 Correct 11 ms 4184 KB Output is correct
6 Correct 15 ms 5976 KB Output is correct
7 Correct 14 ms 5976 KB Output is correct
8 Correct 15 ms 5840 KB Output is correct
9 Correct 19 ms 5976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 23 ms 10048 KB Output is correct
4 Correct 19 ms 7628 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 416 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 23 ms 9884 KB Output is correct
19 Correct 23 ms 9924 KB Output is correct
20 Correct 20 ms 7368 KB Output is correct
21 Correct 23 ms 9932 KB Output is correct
22 Correct 22 ms 9212 KB Output is correct
23 Correct 23 ms 9164 KB Output is correct
24 Correct 22 ms 9276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 12 ms 4676 KB Output is correct
3 Correct 17 ms 6208 KB Output is correct
4 Correct 12 ms 4952 KB Output is correct
5 Correct 16 ms 6352 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 15 ms 5168 KB Output is correct
19 Correct 12 ms 5564 KB Output is correct
20 Correct 15 ms 5724 KB Output is correct
21 Correct 17 ms 6100 KB Output is correct
22 Correct 13 ms 6880 KB Output is correct
23 Correct 18 ms 7080 KB Output is correct
24 Correct 13 ms 6780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 11 ms 4992 KB Output is correct
20 Correct 11 ms 5076 KB Output is correct
21 Correct 11 ms 4184 KB Output is correct
22 Correct 15 ms 5976 KB Output is correct
23 Correct 14 ms 5976 KB Output is correct
24 Correct 15 ms 5840 KB Output is correct
25 Correct 19 ms 5976 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 23 ms 10048 KB Output is correct
29 Correct 19 ms 7628 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 416 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 23 ms 9884 KB Output is correct
44 Correct 23 ms 9924 KB Output is correct
45 Correct 20 ms 7368 KB Output is correct
46 Correct 23 ms 9932 KB Output is correct
47 Correct 22 ms 9212 KB Output is correct
48 Correct 23 ms 9164 KB Output is correct
49 Correct 22 ms 9276 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 12 ms 4676 KB Output is correct
52 Correct 17 ms 6208 KB Output is correct
53 Correct 12 ms 4952 KB Output is correct
54 Correct 16 ms 6352 KB Output is correct
55 Correct 0 ms 348 KB Output is correct
56 Correct 0 ms 348 KB Output is correct
57 Correct 0 ms 348 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 0 ms 344 KB Output is correct
62 Correct 0 ms 348 KB Output is correct
63 Correct 0 ms 344 KB Output is correct
64 Correct 0 ms 348 KB Output is correct
65 Correct 0 ms 348 KB Output is correct
66 Correct 0 ms 348 KB Output is correct
67 Correct 15 ms 5168 KB Output is correct
68 Correct 12 ms 5564 KB Output is correct
69 Correct 15 ms 5724 KB Output is correct
70 Correct 17 ms 6100 KB Output is correct
71 Correct 13 ms 6880 KB Output is correct
72 Correct 18 ms 7080 KB Output is correct
73 Correct 13 ms 6780 KB Output is correct
74 Correct 20 ms 7512 KB Output is correct
75 Correct 14 ms 6808 KB Output is correct
76 Correct 16 ms 7352 KB Output is correct
77 Correct 14 ms 6620 KB Output is correct
78 Correct 13 ms 6908 KB Output is correct
79 Correct 13 ms 6788 KB Output is correct
80 Correct 14 ms 6868 KB Output is correct
81 Correct 15 ms 7136 KB Output is correct
82 Correct 16 ms 7092 KB Output is correct
83 Correct 14 ms 7380 KB Output is correct
84 Correct 25 ms 8144 KB Output is correct
85 Correct 18 ms 7032 KB Output is correct
86 Correct 17 ms 7504 KB Output is correct
87 Correct 16 ms 7380 KB Output is correct
88 Correct 15 ms 7160 KB Output is correct
89 Correct 16 ms 7148 KB Output is correct
90 Correct 16 ms 7204 KB Output is correct
91 Correct 16 ms 7740 KB Output is correct
92 Correct 16 ms 7472 KB Output is correct
93 Correct 15 ms 7632 KB Output is correct
94 Correct 16 ms 8116 KB Output is correct
95 Correct 18 ms 7280 KB Output is correct
96 Correct 15 ms 7636 KB Output is correct
97 Correct 16 ms 7692 KB Output is correct
98 Correct 16 ms 7776 KB Output is correct
99 Correct 16 ms 7772 KB Output is correct
100 Correct 15 ms 7544 KB Output is correct
101 Correct 16 ms 7608 KB Output is correct
102 Correct 15 ms 7380 KB Output is correct