답안 #953642

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
953642 2024-03-26T11:37:35 Z Iliya_ Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
100 / 100
433 ms 792276 KB
//IN THE NAME OF GOD
#include<bits/stdc++.h>
//#pragma GCC optimize("O2,unroll-loops")
//#pragma GCC target("sse4")
#define endl        '\n'
#define F           first
#define S           second
#define pii         pair<int,int>
#define mp          make_pair
#define all(x)      x.begin(),x.end()
#define pb          push_back
#define fast_io     ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define file_io     freopen("input.in" , "r" , stdin) ; freopen("output.out" , "w" , stdout);
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
typedef long double dll;

const int N = 407, inf=1e9;
int A[N], dp[3][N][N][N],sum[3][N], te[3], po[3][N];


int32_t main(){
     fast_io;

     int n; cin>>n; 
     string s; cin>>s; s='#'+s;
     for(int i=1; i<=n; i++){
          if(s[i]=='R'){
               A[i]=0;
               te[0]++; 
               po[0][te[0]]=i; 
          }
          else if(s[i]=='G'){
               A[i]=1;
               te[1]++; 
               po[1][te[1]]=i; 
          }
          else{
               A[i]=2; 
               te[2]++; 
               po[2][te[2]]=i; 
          }
     }
     if(te[0]>(te[1]+te[2]+1)||te[1]>(te[0]+te[2]+1)||te[2]>(te[0]+te[1]+1)) return cout<<-1<<endl,0;
     for(int i=0; i<3; i++){
          for(int j=1; j<=n; j++) sum[i][j]=sum[i][j-1]+(A[j]==i);
     }
     for(int id=0; id<3; id++) for(int a=0; a<N; a++) for(int b=0; b<N; b++) for(int c=0; c<N; c++) dp[id][a][b][c]=inf; 
     for(int i=0; i<3; i++) dp[i][0][0][0]=0; 
     for(int a=0; a<=te[0]; a++){
          for(int b=0; b<=te[1]; b++){
               for(int c=0; c<=te[2]; c++){
                    for(int now=0; now<3; now++){
                         for(int lst=0; lst<3; lst++){
                              if(lst==now||(a==te[0]&&now==0)||(b==te[1]&&now==1)||(c==te[2]&&now==2)) continue; 
                              int po0,po1,po2,fa,mn=dp[lst][a][b][c]; 
                              if(lst==0){
                                   po0=po[0][a]; 
                                   po1=(now==1?po[1][b+1]:po[2][c+1]); 
                                   po2=(now==1?po[2][c]:po[1][b]); 
                                   fa=(now==1?2:1);
                              }
                              else if(lst==1){
                                   po0=po[1][b]; 
                                   po1=(now==0?po[0][a+1]:po[2][c+1]); 
                                   po2=(now==0?po[2][c]:po[0][a]); 
                                   fa=(now==0?2:0); 
                              }
                              else{
                                   po0=po[2][c];  
                                   po1=(now==0?po[0][a+1]:po[1][b+1]);
                                   po2=(now==0?po[1][b]:po[0][a]); 
                                   fa=(now==0?1:0);
                              }
                              mn=mn+max(0,sum[lst][po1]-sum[lst][po0])+max(0,sum[fa][po1]-sum[fa][po2]); 
                              dp[now][a+(now==0)][b+(now==1)][c+(now==2)]=min(dp[now][a+(now==0)][b+(now==1)][c+(now==2)],mn);
                         }
                    }
               }
          }
     }
     int ans=inf; 
     for(int i=0; i<3; i++) ans=min(ans,dp[i][te[0]][te[1]][te[2]]); 
     cout<<(ans==inf?-1:ans)<<endl;

     return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 433 ms 792136 KB Output is correct
2 Correct 333 ms 791892 KB Output is correct
3 Correct 301 ms 791924 KB Output is correct
4 Correct 297 ms 792028 KB Output is correct
5 Correct 311 ms 791832 KB Output is correct
6 Correct 294 ms 792276 KB Output is correct
7 Correct 293 ms 791960 KB Output is correct
8 Correct 298 ms 791936 KB Output is correct
9 Correct 309 ms 792012 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 301 ms 791888 KB Output is correct
12 Correct 292 ms 791828 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 312 ms 792024 KB Output is correct
15 Correct 295 ms 791868 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 433 ms 792136 KB Output is correct
2 Correct 333 ms 791892 KB Output is correct
3 Correct 301 ms 791924 KB Output is correct
4 Correct 297 ms 792028 KB Output is correct
5 Correct 311 ms 791832 KB Output is correct
6 Correct 294 ms 792276 KB Output is correct
7 Correct 293 ms 791960 KB Output is correct
8 Correct 298 ms 791936 KB Output is correct
9 Correct 309 ms 792012 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 301 ms 791888 KB Output is correct
12 Correct 292 ms 791828 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 312 ms 792024 KB Output is correct
15 Correct 295 ms 791868 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 312 ms 791992 KB Output is correct
19 Correct 300 ms 791888 KB Output is correct
20 Correct 296 ms 791880 KB Output is correct
21 Correct 310 ms 791892 KB Output is correct
22 Correct 295 ms 792016 KB Output is correct
23 Correct 295 ms 791892 KB Output is correct
24 Correct 295 ms 791896 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 292 ms 791888 KB Output is correct
28 Correct 292 ms 791864 KB Output is correct
29 Correct 297 ms 792184 KB Output is correct
30 Correct 308 ms 791892 KB Output is correct
31 Correct 299 ms 792168 KB Output is correct
32 Correct 296 ms 791896 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 298 ms 792016 KB Output is correct
36 Correct 292 ms 792048 KB Output is correct
37 Correct 292 ms 791892 KB Output is correct
38 Correct 326 ms 791972 KB Output is correct
39 Correct 307 ms 791892 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 308 ms 792044 KB Output is correct
2 Correct 298 ms 791892 KB Output is correct
3 Correct 311 ms 791932 KB Output is correct
4 Correct 303 ms 791968 KB Output is correct
5 Correct 301 ms 791892 KB Output is correct
6 Correct 298 ms 791888 KB Output is correct
7 Correct 337 ms 791856 KB Output is correct
8 Correct 364 ms 791952 KB Output is correct
9 Correct 317 ms 791976 KB Output is correct
10 Correct 305 ms 792120 KB Output is correct
11 Correct 308 ms 791896 KB Output is correct
12 Correct 299 ms 791920 KB Output is correct
13 Correct 299 ms 791948 KB Output is correct
14 Correct 303 ms 792200 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 433 ms 792136 KB Output is correct
2 Correct 333 ms 791892 KB Output is correct
3 Correct 301 ms 791924 KB Output is correct
4 Correct 297 ms 792028 KB Output is correct
5 Correct 311 ms 791832 KB Output is correct
6 Correct 294 ms 792276 KB Output is correct
7 Correct 293 ms 791960 KB Output is correct
8 Correct 298 ms 791936 KB Output is correct
9 Correct 309 ms 792012 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 301 ms 791888 KB Output is correct
12 Correct 292 ms 791828 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 312 ms 792024 KB Output is correct
15 Correct 295 ms 791868 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 312 ms 791992 KB Output is correct
19 Correct 300 ms 791888 KB Output is correct
20 Correct 296 ms 791880 KB Output is correct
21 Correct 310 ms 791892 KB Output is correct
22 Correct 295 ms 792016 KB Output is correct
23 Correct 295 ms 791892 KB Output is correct
24 Correct 295 ms 791896 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 292 ms 791888 KB Output is correct
28 Correct 292 ms 791864 KB Output is correct
29 Correct 297 ms 792184 KB Output is correct
30 Correct 308 ms 791892 KB Output is correct
31 Correct 299 ms 792168 KB Output is correct
32 Correct 296 ms 791896 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 298 ms 792016 KB Output is correct
36 Correct 292 ms 792048 KB Output is correct
37 Correct 292 ms 791892 KB Output is correct
38 Correct 326 ms 791972 KB Output is correct
39 Correct 307 ms 791892 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 308 ms 792044 KB Output is correct
43 Correct 298 ms 791892 KB Output is correct
44 Correct 311 ms 791932 KB Output is correct
45 Correct 303 ms 791968 KB Output is correct
46 Correct 301 ms 791892 KB Output is correct
47 Correct 298 ms 791888 KB Output is correct
48 Correct 337 ms 791856 KB Output is correct
49 Correct 364 ms 791952 KB Output is correct
50 Correct 317 ms 791976 KB Output is correct
51 Correct 305 ms 792120 KB Output is correct
52 Correct 308 ms 791896 KB Output is correct
53 Correct 299 ms 791920 KB Output is correct
54 Correct 299 ms 791948 KB Output is correct
55 Correct 303 ms 792200 KB Output is correct
56 Correct 0 ms 348 KB Output is correct
57 Correct 0 ms 348 KB Output is correct
58 Correct 381 ms 791920 KB Output is correct
59 Correct 405 ms 792212 KB Output is correct
60 Correct 398 ms 791892 KB Output is correct
61 Correct 389 ms 791888 KB Output is correct
62 Correct 0 ms 348 KB Output is correct
63 Correct 325 ms 792144 KB Output is correct
64 Correct 343 ms 791892 KB Output is correct
65 Correct 368 ms 792144 KB Output is correct
66 Correct 384 ms 791892 KB Output is correct
67 Correct 385 ms 792252 KB Output is correct
68 Correct 392 ms 792272 KB Output is correct
69 Correct 386 ms 792004 KB Output is correct
70 Correct 383 ms 791888 KB Output is correct
71 Correct 400 ms 792236 KB Output is correct
72 Correct 1 ms 344 KB Output is correct
73 Correct 1 ms 348 KB Output is correct