답안 #1113618

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1113618 2024-11-16T20:12:10 Z Dan4Life Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
100 / 100
370 ms 382032 KB
#include <bits/stdc++.h>
using namespace std;
 
const int N = (int)4e2+2;
const int INF = 0x3f3f3f3f;
 
vector<int> v[3];
int dp[N][3][N/2][N];
int n, cnt[3], chk[3][3][N];
 
int num(int cnt0, int cnt1, int cnt2, int f0, int f1, int f2){
    return max(0,cnt1-chk[f0][f1][cnt0])+max(0,cnt2-chk[f0][f2][cnt0]);
}
 
void swp(int i, int j){ swap(cnt[i],cnt[j]); swap(v[i],v[j]); }
 
int main(){
    ios_base::sync_with_stdio(false); cin.tie(0);
    string s; cin >> n >> s;
    for(int i = 0; i < n; i++){
        char u = s[i];
        if(u=='R') s[i]=0;
        else if(u=='G') s[i]=1;
        else s[i]=2;
        cnt[s[i]]++; v[s[i]].push_back(i+1);
    }
    if(cnt[0]>cnt[1])swp(0,1);
    if(cnt[1]>cnt[2])swp(1,2);
    if(cnt[0]>cnt[1])swp(0,1);
    memset(dp,INF,sizeof(dp));
    for(int i : {0,1,2}) dp[0][i][0][0]=0;
    for(int f0 = 0; f0 < 3; f0++){
        for(int f1 = 0; f1 < 3; f1++){
            int j = 0;
            for(int cnt0 = 1; cnt0 <= cnt[f0] and f0!=f1; cnt0++){
                while(j<cnt[f1] and v[f1][j]<v[f0][cnt0-1]) j++;
                chk[f0][f1][cnt0] = j;
            }
        }
    }
    int ans = INF;
    for(int i = 1; i <= n; i++){
        for(int j = 0; j < 3; j++){
            for(int m = 0; m < 3; m++){
                for(int k = 0; k <= min(i,cnt[0]) and m!=j; k++){
                    for(int l = 0; l <= min(i,cnt[1]); l++){
                        int o = i-k-l; if(o>i or o>cnt[2]) continue;
                        if(k and j==0){
                            int f0 = v[0][k-1]+num(k,l,o,0,1,2);
                            if(f0>=i) dp[i][j][k][l] = min(dp[i][j][k][l], dp[i-1][m][k-1][l]+f0-i);
                        }
                        if(l and j==1){
                            int f1 = v[1][l-1]+num(l,k,o,1,0,2);
                            if(f1>=i) dp[i][j][k][l] = min(dp[i][j][k][l], dp[i-1][m][k][l-1]+f1-i);
                        }
                        if(o and j==2){
                            int f2 = v[2][o-1]+num(o,k,l,2,0,1);
                            if(f2>=i) dp[i][j][k][l] = min(dp[i][j][k][l], dp[i-1][m][k][l]+f2-i);
                        }
                    }
                }
            }
        }
    }
    for(int j = 0; j < 3; j++) 
        ans = min(ans, dp[n][j][cnt[0]][cnt[1]]);
    if(ans>=INF) ans=-1;
    cout << ans << "\n";
}

Compilation message

joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:25:17: warning: array subscript has type 'char' [-Wchar-subscripts]
   25 |         cnt[s[i]]++; v[s[i]].push_back(i+1);
      |                 ^
joi2019_ho_t3.cpp:25:28: warning: array subscript has type 'char' [-Wchar-subscripts]
   25 |         cnt[s[i]]++; v[s[i]].push_back(i+1);
      |                            ^
# 결과 실행 시간 메모리 Grader output
1 Correct 240 ms 381812 KB Output is correct
2 Correct 242 ms 381836 KB Output is correct
3 Correct 239 ms 381768 KB Output is correct
4 Correct 236 ms 381768 KB Output is correct
5 Correct 247 ms 381768 KB Output is correct
6 Correct 283 ms 381768 KB Output is correct
7 Correct 245 ms 381768 KB Output is correct
8 Correct 251 ms 381768 KB Output is correct
9 Correct 268 ms 381768 KB Output is correct
10 Correct 258 ms 381748 KB Output is correct
11 Correct 267 ms 381676 KB Output is correct
12 Correct 315 ms 381768 KB Output is correct
13 Correct 275 ms 381768 KB Output is correct
14 Correct 273 ms 381772 KB Output is correct
15 Correct 267 ms 381768 KB Output is correct
16 Correct 275 ms 381832 KB Output is correct
17 Correct 272 ms 381768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 240 ms 381812 KB Output is correct
2 Correct 242 ms 381836 KB Output is correct
3 Correct 239 ms 381768 KB Output is correct
4 Correct 236 ms 381768 KB Output is correct
5 Correct 247 ms 381768 KB Output is correct
6 Correct 283 ms 381768 KB Output is correct
7 Correct 245 ms 381768 KB Output is correct
8 Correct 251 ms 381768 KB Output is correct
9 Correct 268 ms 381768 KB Output is correct
10 Correct 258 ms 381748 KB Output is correct
11 Correct 267 ms 381676 KB Output is correct
12 Correct 315 ms 381768 KB Output is correct
13 Correct 275 ms 381768 KB Output is correct
14 Correct 273 ms 381772 KB Output is correct
15 Correct 267 ms 381768 KB Output is correct
16 Correct 275 ms 381832 KB Output is correct
17 Correct 272 ms 381768 KB Output is correct
18 Correct 278 ms 381824 KB Output is correct
19 Correct 254 ms 381832 KB Output is correct
20 Correct 269 ms 381740 KB Output is correct
21 Correct 280 ms 381736 KB Output is correct
22 Correct 271 ms 381652 KB Output is correct
23 Correct 274 ms 381608 KB Output is correct
24 Correct 277 ms 381632 KB Output is correct
25 Correct 272 ms 381768 KB Output is correct
26 Correct 283 ms 381844 KB Output is correct
27 Correct 274 ms 381772 KB Output is correct
28 Correct 251 ms 381772 KB Output is correct
29 Correct 269 ms 381672 KB Output is correct
30 Correct 277 ms 381828 KB Output is correct
31 Correct 268 ms 381768 KB Output is correct
32 Correct 263 ms 381772 KB Output is correct
33 Correct 273 ms 381784 KB Output is correct
34 Correct 280 ms 381788 KB Output is correct
35 Correct 270 ms 381692 KB Output is correct
36 Correct 254 ms 381768 KB Output is correct
37 Correct 263 ms 381768 KB Output is correct
38 Correct 251 ms 381768 KB Output is correct
39 Correct 257 ms 381768 KB Output is correct
40 Correct 247 ms 381776 KB Output is correct
41 Correct 241 ms 381768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 272 ms 381768 KB Output is correct
2 Correct 250 ms 381684 KB Output is correct
3 Correct 276 ms 381636 KB Output is correct
4 Correct 272 ms 381768 KB Output is correct
5 Correct 266 ms 381768 KB Output is correct
6 Correct 282 ms 381768 KB Output is correct
7 Correct 283 ms 381768 KB Output is correct
8 Correct 275 ms 381664 KB Output is correct
9 Correct 291 ms 381768 KB Output is correct
10 Correct 310 ms 381668 KB Output is correct
11 Correct 283 ms 381724 KB Output is correct
12 Correct 269 ms 381772 KB Output is correct
13 Correct 274 ms 381768 KB Output is correct
14 Correct 279 ms 381768 KB Output is correct
15 Correct 250 ms 381764 KB Output is correct
16 Correct 250 ms 381852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 240 ms 381812 KB Output is correct
2 Correct 242 ms 381836 KB Output is correct
3 Correct 239 ms 381768 KB Output is correct
4 Correct 236 ms 381768 KB Output is correct
5 Correct 247 ms 381768 KB Output is correct
6 Correct 283 ms 381768 KB Output is correct
7 Correct 245 ms 381768 KB Output is correct
8 Correct 251 ms 381768 KB Output is correct
9 Correct 268 ms 381768 KB Output is correct
10 Correct 258 ms 381748 KB Output is correct
11 Correct 267 ms 381676 KB Output is correct
12 Correct 315 ms 381768 KB Output is correct
13 Correct 275 ms 381768 KB Output is correct
14 Correct 273 ms 381772 KB Output is correct
15 Correct 267 ms 381768 KB Output is correct
16 Correct 275 ms 381832 KB Output is correct
17 Correct 272 ms 381768 KB Output is correct
18 Correct 278 ms 381824 KB Output is correct
19 Correct 254 ms 381832 KB Output is correct
20 Correct 269 ms 381740 KB Output is correct
21 Correct 280 ms 381736 KB Output is correct
22 Correct 271 ms 381652 KB Output is correct
23 Correct 274 ms 381608 KB Output is correct
24 Correct 277 ms 381632 KB Output is correct
25 Correct 272 ms 381768 KB Output is correct
26 Correct 283 ms 381844 KB Output is correct
27 Correct 274 ms 381772 KB Output is correct
28 Correct 251 ms 381772 KB Output is correct
29 Correct 269 ms 381672 KB Output is correct
30 Correct 277 ms 381828 KB Output is correct
31 Correct 268 ms 381768 KB Output is correct
32 Correct 263 ms 381772 KB Output is correct
33 Correct 273 ms 381784 KB Output is correct
34 Correct 280 ms 381788 KB Output is correct
35 Correct 270 ms 381692 KB Output is correct
36 Correct 254 ms 381768 KB Output is correct
37 Correct 263 ms 381768 KB Output is correct
38 Correct 251 ms 381768 KB Output is correct
39 Correct 257 ms 381768 KB Output is correct
40 Correct 247 ms 381776 KB Output is correct
41 Correct 241 ms 381768 KB Output is correct
42 Correct 272 ms 381768 KB Output is correct
43 Correct 250 ms 381684 KB Output is correct
44 Correct 276 ms 381636 KB Output is correct
45 Correct 272 ms 381768 KB Output is correct
46 Correct 266 ms 381768 KB Output is correct
47 Correct 282 ms 381768 KB Output is correct
48 Correct 283 ms 381768 KB Output is correct
49 Correct 275 ms 381664 KB Output is correct
50 Correct 291 ms 381768 KB Output is correct
51 Correct 310 ms 381668 KB Output is correct
52 Correct 283 ms 381724 KB Output is correct
53 Correct 269 ms 381772 KB Output is correct
54 Correct 274 ms 381768 KB Output is correct
55 Correct 279 ms 381768 KB Output is correct
56 Correct 250 ms 381764 KB Output is correct
57 Correct 250 ms 381852 KB Output is correct
58 Correct 365 ms 381772 KB Output is correct
59 Correct 357 ms 381768 KB Output is correct
60 Correct 342 ms 381768 KB Output is correct
61 Correct 343 ms 381760 KB Output is correct
62 Correct 314 ms 381860 KB Output is correct
63 Correct 271 ms 381768 KB Output is correct
64 Correct 293 ms 381712 KB Output is correct
65 Correct 304 ms 381684 KB Output is correct
66 Correct 358 ms 381736 KB Output is correct
67 Correct 353 ms 381784 KB Output is correct
68 Correct 355 ms 382032 KB Output is correct
69 Correct 370 ms 381768 KB Output is correct
70 Correct 366 ms 381768 KB Output is correct
71 Correct 316 ms 381768 KB Output is correct
72 Correct 258 ms 381776 KB Output is correct
73 Correct 256 ms 381768 KB Output is correct