답안 #644147

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
644147 2022-09-24T01:26:19 Z ymm Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
100 / 100
31 ms 4308 KB
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;

const int N = 410;
int dp[2][N][N][3];
vector<int> pos[3];
int dis[3][N][3];
int n;
string s;

int to_int(char c)
{
	switch (toupper(c)) {
	case 'R': return 0;
	case 'G': return 1;
	case 'Y': return 2;
	default: return -1;
	}
}

int main()
{
	cin.tie(0) -> sync_with_stdio(false);
	cin >> n >> s;
	Loop (i,0,n) pos[to_int(s[i])].push_back(i);
	Loop (i,0,3) Loop (j,0,pos[i].size()) Loop (k,0,3) {
		dis[i][j][k] = lower_bound(pos[k].begin(), pos[k].end(), pos[i][j]) - pos[k].begin();
	}
	Loop (i,0,pos[0].size()+1) {
		memset(dp[i&1], 32, sizeof(dp[i&1]));
		if (i == 0) {
			dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
		}
		Loop (j,0,pos[1].size()+1) Loop (k,0,pos[2].size()+1) {
			if (i) {
				int kooft = max(0ll, j - dis[0][i-1][1]) + max(0ll, k - dis[0][i-1][2]);
				dp[i&1][j][k][1] = min(dp[i&1][j][k][1], dp[i-1&1][j][k][0] + kooft);
				dp[i&1][j][k][2] = min(dp[i&1][j][k][2], dp[i-1&1][j][k][0] + kooft);
			}
			if (j) {
				int kooft = max(0ll, i - dis[1][j-1][0]) + max(0ll, k - dis[1][j-1][2]);
				dp[i&1][j][k][0] = min(dp[i&1][j][k][0], dp[i&1][j-1][k][1] + kooft);
				dp[i&1][j][k][2] = min(dp[i&1][j][k][2], dp[i&1][j-1][k][1] + kooft);
			}
			if (k) {
				int kooft = max(0ll, i - dis[2][k-1][0]) + max(0ll, j - dis[2][k-1][1]);
				dp[i&1][j][k][0] = min(dp[i&1][j][k][0], dp[i&1][j][k-1][2] + kooft);
				dp[i&1][j][k][1] = min(dp[i&1][j][k][1], dp[i&1][j][k-1][2] + kooft);
			}
			//printf("dp[%lld][%lld][%lld] = {%d, %d, %d}\n", i, j, k, dp[i&1][j][k][0], dp[i&1][j][k][1], dp[i&1][j][k][2]);
		}
	}
	int ans = min({dp[pos[0].size()&1][pos[1].size()][pos[2].size()][0],
	               dp[pos[0].size()&1][pos[1].size()][pos[2].size()][1],
		       dp[pos[0].size()&1][pos[1].size()][pos[2].size()][2]});
	cout << (ans > N*N? -1: ans) << '\n';
}

Compilation message

joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:2:40: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    2 | #define Loop(x,l,r) for (ll x = (l); x < (r); ++x)
      |                                        ^
joi2019_ho_t3.cpp:31:15: note: in expansion of macro 'Loop'
   31 |  Loop (i,0,3) Loop (j,0,pos[i].size()) Loop (k,0,3) {
      |               ^~~~
joi2019_ho_t3.cpp:2:40: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    2 | #define Loop(x,l,r) for (ll x = (l); x < (r); ++x)
      |                                        ^
joi2019_ho_t3.cpp:34:2: note: in expansion of macro 'Loop'
   34 |  Loop (i,0,pos[0].size()+1) {
      |  ^~~~
joi2019_ho_t3.cpp:2:40: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    2 | #define Loop(x,l,r) for (ll x = (l); x < (r); ++x)
      |                                        ^
joi2019_ho_t3.cpp:39:3: note: in expansion of macro 'Loop'
   39 |   Loop (j,0,pos[1].size()+1) Loop (k,0,pos[2].size()+1) {
      |   ^~~~
joi2019_ho_t3.cpp:2:40: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    2 | #define Loop(x,l,r) for (ll x = (l); x < (r); ++x)
      |                                        ^
joi2019_ho_t3.cpp:39:30: note: in expansion of macro 'Loop'
   39 |   Loop (j,0,pos[1].size()+1) Loop (k,0,pos[2].size()+1) {
      |                              ^~~~
joi2019_ho_t3.cpp:42:50: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   42 |     dp[i&1][j][k][1] = min(dp[i&1][j][k][1], dp[i-1&1][j][k][0] + kooft);
      |                                                 ~^~
joi2019_ho_t3.cpp:43:50: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   43 |     dp[i&1][j][k][2] = min(dp[i&1][j][k][2], dp[i-1&1][j][k][0] + kooft);
      |                                                 ~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4180 KB Output is correct
2 Correct 2 ms 4180 KB Output is correct
3 Correct 1 ms 2260 KB Output is correct
4 Correct 3 ms 4180 KB Output is correct
5 Correct 2 ms 4180 KB Output is correct
6 Correct 2 ms 4180 KB Output is correct
7 Correct 2 ms 4180 KB Output is correct
8 Correct 2 ms 4188 KB Output is correct
9 Correct 3 ms 4180 KB Output is correct
10 Correct 2 ms 4180 KB Output is correct
11 Correct 2 ms 4180 KB Output is correct
12 Correct 2 ms 4180 KB Output is correct
13 Correct 2 ms 4180 KB Output is correct
14 Correct 2 ms 4180 KB Output is correct
15 Correct 2 ms 4180 KB Output is correct
16 Correct 3 ms 4180 KB Output is correct
17 Correct 1 ms 2260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4180 KB Output is correct
2 Correct 2 ms 4180 KB Output is correct
3 Correct 1 ms 2260 KB Output is correct
4 Correct 3 ms 4180 KB Output is correct
5 Correct 2 ms 4180 KB Output is correct
6 Correct 2 ms 4180 KB Output is correct
7 Correct 2 ms 4180 KB Output is correct
8 Correct 2 ms 4188 KB Output is correct
9 Correct 3 ms 4180 KB Output is correct
10 Correct 2 ms 4180 KB Output is correct
11 Correct 2 ms 4180 KB Output is correct
12 Correct 2 ms 4180 KB Output is correct
13 Correct 2 ms 4180 KB Output is correct
14 Correct 2 ms 4180 KB Output is correct
15 Correct 2 ms 4180 KB Output is correct
16 Correct 3 ms 4180 KB Output is correct
17 Correct 1 ms 2260 KB Output is correct
18 Correct 3 ms 4180 KB Output is correct
19 Correct 3 ms 4216 KB Output is correct
20 Correct 3 ms 4184 KB Output is correct
21 Correct 3 ms 4180 KB Output is correct
22 Correct 3 ms 4180 KB Output is correct
23 Correct 3 ms 4180 KB Output is correct
24 Correct 3 ms 4176 KB Output is correct
25 Correct 4 ms 4176 KB Output is correct
26 Correct 3 ms 4180 KB Output is correct
27 Correct 3 ms 4180 KB Output is correct
28 Correct 3 ms 4308 KB Output is correct
29 Correct 3 ms 4180 KB Output is correct
30 Correct 3 ms 4180 KB Output is correct
31 Correct 3 ms 4180 KB Output is correct
32 Correct 3 ms 4180 KB Output is correct
33 Correct 3 ms 4180 KB Output is correct
34 Correct 3 ms 4180 KB Output is correct
35 Correct 3 ms 4180 KB Output is correct
36 Correct 3 ms 4180 KB Output is correct
37 Correct 3 ms 4180 KB Output is correct
38 Correct 3 ms 4180 KB Output is correct
39 Correct 3 ms 4176 KB Output is correct
40 Correct 1 ms 2260 KB Output is correct
41 Correct 2 ms 4180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4180 KB Output is correct
2 Correct 12 ms 4284 KB Output is correct
3 Correct 11 ms 4292 KB Output is correct
4 Correct 11 ms 4180 KB Output is correct
5 Correct 11 ms 4176 KB Output is correct
6 Correct 11 ms 4176 KB Output is correct
7 Correct 12 ms 4184 KB Output is correct
8 Correct 12 ms 4180 KB Output is correct
9 Correct 11 ms 4288 KB Output is correct
10 Correct 12 ms 4180 KB Output is correct
11 Correct 11 ms 4288 KB Output is correct
12 Correct 7 ms 4284 KB Output is correct
13 Correct 9 ms 4180 KB Output is correct
14 Correct 9 ms 4180 KB Output is correct
15 Correct 12 ms 4180 KB Output is correct
16 Correct 11 ms 4180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4180 KB Output is correct
2 Correct 2 ms 4180 KB Output is correct
3 Correct 1 ms 2260 KB Output is correct
4 Correct 3 ms 4180 KB Output is correct
5 Correct 2 ms 4180 KB Output is correct
6 Correct 2 ms 4180 KB Output is correct
7 Correct 2 ms 4180 KB Output is correct
8 Correct 2 ms 4188 KB Output is correct
9 Correct 3 ms 4180 KB Output is correct
10 Correct 2 ms 4180 KB Output is correct
11 Correct 2 ms 4180 KB Output is correct
12 Correct 2 ms 4180 KB Output is correct
13 Correct 2 ms 4180 KB Output is correct
14 Correct 2 ms 4180 KB Output is correct
15 Correct 2 ms 4180 KB Output is correct
16 Correct 3 ms 4180 KB Output is correct
17 Correct 1 ms 2260 KB Output is correct
18 Correct 3 ms 4180 KB Output is correct
19 Correct 3 ms 4216 KB Output is correct
20 Correct 3 ms 4184 KB Output is correct
21 Correct 3 ms 4180 KB Output is correct
22 Correct 3 ms 4180 KB Output is correct
23 Correct 3 ms 4180 KB Output is correct
24 Correct 3 ms 4176 KB Output is correct
25 Correct 4 ms 4176 KB Output is correct
26 Correct 3 ms 4180 KB Output is correct
27 Correct 3 ms 4180 KB Output is correct
28 Correct 3 ms 4308 KB Output is correct
29 Correct 3 ms 4180 KB Output is correct
30 Correct 3 ms 4180 KB Output is correct
31 Correct 3 ms 4180 KB Output is correct
32 Correct 3 ms 4180 KB Output is correct
33 Correct 3 ms 4180 KB Output is correct
34 Correct 3 ms 4180 KB Output is correct
35 Correct 3 ms 4180 KB Output is correct
36 Correct 3 ms 4180 KB Output is correct
37 Correct 3 ms 4180 KB Output is correct
38 Correct 3 ms 4180 KB Output is correct
39 Correct 3 ms 4176 KB Output is correct
40 Correct 1 ms 2260 KB Output is correct
41 Correct 2 ms 4180 KB Output is correct
42 Correct 2 ms 4180 KB Output is correct
43 Correct 12 ms 4284 KB Output is correct
44 Correct 11 ms 4292 KB Output is correct
45 Correct 11 ms 4180 KB Output is correct
46 Correct 11 ms 4176 KB Output is correct
47 Correct 11 ms 4176 KB Output is correct
48 Correct 12 ms 4184 KB Output is correct
49 Correct 12 ms 4180 KB Output is correct
50 Correct 11 ms 4288 KB Output is correct
51 Correct 12 ms 4180 KB Output is correct
52 Correct 11 ms 4288 KB Output is correct
53 Correct 7 ms 4284 KB Output is correct
54 Correct 9 ms 4180 KB Output is correct
55 Correct 9 ms 4180 KB Output is correct
56 Correct 12 ms 4180 KB Output is correct
57 Correct 11 ms 4180 KB Output is correct
58 Correct 23 ms 4292 KB Output is correct
59 Correct 27 ms 4180 KB Output is correct
60 Correct 25 ms 4308 KB Output is correct
61 Correct 27 ms 4272 KB Output is correct
62 Correct 14 ms 4180 KB Output is correct
63 Correct 17 ms 4180 KB Output is correct
64 Correct 19 ms 4180 KB Output is correct
65 Correct 26 ms 4180 KB Output is correct
66 Correct 31 ms 4180 KB Output is correct
67 Correct 29 ms 4180 KB Output is correct
68 Correct 26 ms 4292 KB Output is correct
69 Correct 24 ms 4180 KB Output is correct
70 Correct 24 ms 4180 KB Output is correct
71 Correct 24 ms 4180 KB Output is correct
72 Correct 21 ms 4180 KB Output is correct
73 Correct 3 ms 4180 KB Output is correct