# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1163226 | hainam2k9 | Growing Vegetable is Fun 3 (JOI19_ho_t3) | C++20 | 485 ms | 782176 KiB |
#include <bits/stdc++.h>
#define tt cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(0)
#define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define db long double
#define sz(a) ((int)(a).size())
#define pb emplace_back
#define pf emplace_front
#define pob pop_back
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define ins emplace
#define mp make_pair
using namespace std;
const int MOD = 1e9+7, MAXN = 1e5+5;
const string NAME = "";
int n,dp[405][405][405][3],rs=1e9,cost[3][3][405][405];
char c[405];
vector<int> pos[3];
int main()
{
tt;
if(fopen((NAME + ".INP").c_str(), "r")) fo;
cin >> n;
for(int i = 1; i<=n; ++i){
cin >> c[i];
if(c[i]=='R') pos[0].pb(i);
else if(c[i]=='G') pos[1].pb(i);
else pos[2].pb(i);
}
for(int i = 0; i<3; ++i)
for(int j = 0; j<3; ++j){
if(i==j) continue;
for(int ptr1 = 1; ptr1<=sz(pos[i]); ++ptr1)
for(int ptr2 = 1; ptr2<=sz(pos[j]); ++ptr2){
if(ptr2>0) cost[i][j][ptr1][ptr2]=cost[i][j][ptr1][ptr2-1];
cost[i][j][ptr1][ptr2]+=pos[i][ptr1-1]<pos[j][ptr2-1];
}
}
memset(dp,0x3f,sizeof(dp));
if(!pos[0].empty()) dp[1][1][0][0]=pos[0][0]-1;
if(!pos[1].empty()) dp[1][0][1][1]=pos[1][0]-1;
if(!pos[2].empty()) dp[1][0][0][2]=pos[2][0]-1;
for(int i = 2; i<=n; ++i)
for(int j = 0; j<=min(i,sz(pos[0])); ++j)
for(int k = 0; k<=min(i-j,sz(pos[1])); ++k){
if(i-j-k>sz(pos[2])) continue;
if(j>0) dp[i][j][k][0]=min(dp[i-1][j-1][k][1],dp[i-1][j-1][k][2])+abs(i-pos[0][j-1]-cost[0][1][j][k]-cost[0][2][j][i-j-k]);
if(k>0) dp[i][j][k][1]=min(dp[i-1][j][k-1][0],dp[i-1][j][k-1][2])+abs(i-pos[1][k-1]-cost[1][0][k][j]-cost[1][2][k][i-j-k]);
if(i-j-k>0) dp[i][j][k][2]=min(dp[i-1][j][k][0],dp[i-1][j][k][1])+abs(i-pos[2][i-j-k-1]-cost[2][0][i-j-k][j]-cost[2][1][i-j-k][k]);
}
for(int i=sz(pos[0]), j=sz(pos[1]), last=0; last<3; ++last)
rs=min(rs,dp[n][i][j][last]);
if(rs==1e9) rs=-1;
cout << rs;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |