답안 #101888

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
101888 2019-03-20T18:39:25 Z brcode Growing Vegetable is Fun 3 (JOI19_ho_t3) C++14
100 / 100
290 ms 196824 KB
#include <iostream>
#include <vector>
using namespace std;
const int inf = 1e9;
int dp[3][410][410][410];
int cnt[3][410];
int a[450];
vector<int> col[3];
int main(){
    int n;
    cin>>n;
    string s;
    cin>>s;
    for(int i=0;i<n;i++){
        if(s[i] == 'R'){
            a[i+1] = 0;
        }else if(s[i] == 'G'){
            a[i+1] = 1;
        }else{
            a[i+1] = 2;
        }
    }
    col[0].push_back(0);
	col[1].push_back(0);
	col[2].push_back(0);
	
	for(int i=1;i<=n;i++){
	    for(int j=0;j<3;j++){
	        cnt[j][i] = cnt[j][i-1];
	    }
	    //stores the number of colours encountered up to a certain position
	    cnt[a[i]][i]++;
	    //stores the positions of each colour
	    col[a[i]].push_back(i);
	}
	int R = col[0].size()-1;
	int G = col[2].size()-1;
	int B = col[1].size()-1;
	
	for(int last = 0; last < 3; last++){
		for(int r = 0; r <= R; r++){
			for(int b = 0; b <= B; b++){
				for(int g = 0; g <= G; g++){
					dp[last][r][b][g] = inf;
				}
			}
		}
	}
	dp[0][0][0][0] = 0;
	dp[1][0][0][0] = 0;
	dp[2][0][0][0] = 0;
	
	for(int r=0;r<=R;r++){
	    for(int b=0;b<=B;b++){
	        for(int g=0;g<=G;g++){
	            for(int last = 0;last<3;last++){
	                int ans = dp[last][r][b][g];
	                int c[3];
	                c[0] = r;
	                c[1] = b;
	                c[2] = g;
	                //c[i] = number of ith colour grasses we've encountered as of now
	                for(int i=0;i<3;i++){
	                    if(col[i].size()<=c[i]+1||last == i){
	                        continue;
	                    }
	                    //i = current colour
	                    int pos = col[i][c[i]+1];
	                    int tot = -1;
	                    for(int x =0;x<3;x++){
	                        //actual last encounter 
	                        int prev = col[x][c[x]];
	                        tot+=max(0,cnt[x][pos]-cnt[x][prev]);
	                    }
	                    c[i]++;
	                    dp[i][c[0]][c[1]][c[2]] = min(dp[i][c[0]][c[1]][c[2]],tot+ans);
	                    c[i]--;
	                }
	            }
	        }
	    }
	}
	int ans = inf;
	for(int i=0;i<3;i++){
	    ans = min(ans,dp[i][R][B][G]);
	}
	if(ans == inf){
	    cout<<-1;
	}else{
	    cout<<ans<<endl;
	}
	
}

Compilation message

joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:64:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                      if(col[i].size()<=c[i]+1||last == i){
                         ~~~~~~~~~~~~~^~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 3 ms 640 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 4 ms 640 KB Output is correct
9 Correct 3 ms 768 KB Output is correct
10 Correct 3 ms 768 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 3 ms 640 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 2 ms 640 KB Output is correct
15 Correct 3 ms 512 KB Output is correct
16 Correct 4 ms 640 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 3 ms 640 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 4 ms 640 KB Output is correct
9 Correct 3 ms 768 KB Output is correct
10 Correct 3 ms 768 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 3 ms 640 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 2 ms 640 KB Output is correct
15 Correct 3 ms 512 KB Output is correct
16 Correct 4 ms 640 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 5 ms 2816 KB Output is correct
19 Correct 5 ms 2304 KB Output is correct
20 Correct 4 ms 2688 KB Output is correct
21 Correct 4 ms 2816 KB Output is correct
22 Correct 5 ms 2048 KB Output is correct
23 Correct 4 ms 2560 KB Output is correct
24 Correct 4 ms 1920 KB Output is correct
25 Correct 6 ms 4992 KB Output is correct
26 Correct 5 ms 5120 KB Output is correct
27 Correct 5 ms 3712 KB Output is correct
28 Correct 4 ms 2816 KB Output is correct
29 Correct 5 ms 2688 KB Output is correct
30 Correct 6 ms 2688 KB Output is correct
31 Correct 5 ms 2432 KB Output is correct
32 Correct 6 ms 2816 KB Output is correct
33 Correct 5 ms 4736 KB Output is correct
34 Correct 5 ms 4352 KB Output is correct
35 Correct 5 ms 3328 KB Output is correct
36 Correct 4 ms 2304 KB Output is correct
37 Correct 4 ms 2048 KB Output is correct
38 Correct 5 ms 2560 KB Output is correct
39 Correct 4 ms 2816 KB Output is correct
40 Correct 3 ms 384 KB Output is correct
41 Correct 4 ms 2688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 191 ms 196720 KB Output is correct
3 Correct 155 ms 195764 KB Output is correct
4 Correct 158 ms 196708 KB Output is correct
5 Correct 155 ms 196668 KB Output is correct
6 Correct 152 ms 196728 KB Output is correct
7 Correct 152 ms 195704 KB Output is correct
8 Correct 160 ms 195680 KB Output is correct
9 Correct 148 ms 194688 KB Output is correct
10 Correct 191 ms 196728 KB Output is correct
11 Correct 153 ms 196728 KB Output is correct
12 Correct 43 ms 53496 KB Output is correct
13 Correct 84 ms 93432 KB Output is correct
14 Correct 108 ms 134648 KB Output is correct
15 Correct 151 ms 196576 KB Output is correct
16 Correct 161 ms 196824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 3 ms 640 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 4 ms 640 KB Output is correct
9 Correct 3 ms 768 KB Output is correct
10 Correct 3 ms 768 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 3 ms 640 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 2 ms 640 KB Output is correct
15 Correct 3 ms 512 KB Output is correct
16 Correct 4 ms 640 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 5 ms 2816 KB Output is correct
19 Correct 5 ms 2304 KB Output is correct
20 Correct 4 ms 2688 KB Output is correct
21 Correct 4 ms 2816 KB Output is correct
22 Correct 5 ms 2048 KB Output is correct
23 Correct 4 ms 2560 KB Output is correct
24 Correct 4 ms 1920 KB Output is correct
25 Correct 6 ms 4992 KB Output is correct
26 Correct 5 ms 5120 KB Output is correct
27 Correct 5 ms 3712 KB Output is correct
28 Correct 4 ms 2816 KB Output is correct
29 Correct 5 ms 2688 KB Output is correct
30 Correct 6 ms 2688 KB Output is correct
31 Correct 5 ms 2432 KB Output is correct
32 Correct 6 ms 2816 KB Output is correct
33 Correct 5 ms 4736 KB Output is correct
34 Correct 5 ms 4352 KB Output is correct
35 Correct 5 ms 3328 KB Output is correct
36 Correct 4 ms 2304 KB Output is correct
37 Correct 4 ms 2048 KB Output is correct
38 Correct 5 ms 2560 KB Output is correct
39 Correct 4 ms 2816 KB Output is correct
40 Correct 3 ms 384 KB Output is correct
41 Correct 4 ms 2688 KB Output is correct
42 Correct 2 ms 384 KB Output is correct
43 Correct 191 ms 196720 KB Output is correct
44 Correct 155 ms 195764 KB Output is correct
45 Correct 158 ms 196708 KB Output is correct
46 Correct 155 ms 196668 KB Output is correct
47 Correct 152 ms 196728 KB Output is correct
48 Correct 152 ms 195704 KB Output is correct
49 Correct 160 ms 195680 KB Output is correct
50 Correct 148 ms 194688 KB Output is correct
51 Correct 191 ms 196728 KB Output is correct
52 Correct 153 ms 196728 KB Output is correct
53 Correct 43 ms 53496 KB Output is correct
54 Correct 84 ms 93432 KB Output is correct
55 Correct 108 ms 134648 KB Output is correct
56 Correct 151 ms 196576 KB Output is correct
57 Correct 161 ms 196824 KB Output is correct
58 Correct 255 ms 78232 KB Output is correct
59 Correct 237 ms 99056 KB Output is correct
60 Correct 256 ms 90104 KB Output is correct
61 Correct 290 ms 87032 KB Output is correct
62 Correct 158 ms 191740 KB Output is correct
63 Correct 174 ms 188964 KB Output is correct
64 Correct 240 ms 165112 KB Output is correct
65 Correct 266 ms 143736 KB Output is correct
66 Correct 235 ms 88312 KB Output is correct
67 Correct 227 ms 84856 KB Output is correct
68 Correct 241 ms 91000 KB Output is correct
69 Correct 240 ms 87800 KB Output is correct
70 Correct 258 ms 94968 KB Output is correct
71 Correct 256 ms 86396 KB Output is correct
72 Correct 95 ms 44544 KB Output is correct
73 Correct 17 ms 2944 KB Output is correct