답안 #960263

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
960263 2024-04-10T05:40:21 Z LCJLY Growing Vegetable is Fun 3 (JOI19_ho_t3) C++14
60 / 100
151 ms 325968 KB
#include <bits/stdc++.h>
using namespace std;	
 
#define int long long 
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << "  " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << "  " << j << " " << #i << "  " << q << " " << #p << endl;
#define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl;
typedef pair<long long,int>pii;
//typedef pair<pii,pii>pi2;
typedef array<int,6>pi2;

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

int n;
string s;
vector<int>v[3];
int arr[100];

int f(int color, int pos){
	int hold=lower_bound(v[color].begin(),v[color].end(),pos)-v[color].begin();
	return hold;	
}

int add(int a, int b){
	return  max(0LL,a+b);	
}

int memo[61][61][61][61][3];

int dp(int index, int r, int g, int b,int last){
	if(index==n) return 0;
	if(memo[index][r][g][b][last]!=-1) return memo[index][r][g][b][last];
	int ans=1e15;
	
	if(index==0){
		//put red
		if(r<(int)v[0].size())ans=min(ans,dp(index+1,r+1,g,b,0)+v[0][r]);
		
		//put green
		if(g<(int)v[1].size())ans=min(ans,dp(index+1,r,g+1,b,1)+v[1][g]);
		
		//put blue
		if(g<(int)v[2].size())ans=min(ans,dp(index+1,r,g,b+1,2)+v[2][b]);
	}
	else{
		//put red
		if(r<(int)v[0].size() && last!=0){
			int hold=f(1,v[0][r]);
			int hold2=f(2,v[0][r]);
			
			int cost=add(hold,-g)+add(hold2,-b);
			
			ans=min(ans,dp(index+1,r+1,g,b,0)+cost);
		}
		
		//put green
		if(g<(int)v[1].size() && last!=1){
			int hold=f(0,v[1][g]);
			int hold2=f(2,v[1][g]);
			
			int cost=add(hold,-r)+add(hold2,-b);
			
			ans=min(ans,dp(index+1,r,g+1,b,1)+cost);
		}
		
		//put blue
		if(b<(int)v[2].size() && last!=2){
			int hold=f(0,v[2][b]);
			int hold2=f(1,v[2][b]);
			
			int cost=add(hold,-r)+add(hold2,-g);
			
			ans=min(ans,dp(index+1,r,g,b+1,2)+cost);
		}
	}
	return memo[index][r][g][b][last]=ans;
}

void solve(){
	cin >> n >> s;
	
	for(int x=0;x<n;x++){
		if(s[x]=='R'){
			arr[x]=0;
		}
		else if(s[x]=='G'){
			arr[x]=1;
		}
		else arr[x]=2;
		v[arr[x]].push_back(x);
	}
	
	memset(memo,-1,sizeof(memo));
	int hold=dp(0,0,0,0,0);
	if(hold>=1e10){
		cout << -1;
	}
	else cout << hold;
}
 
int32_t main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int t=1;
	//cin >> t;
	while(t--){
		solve();
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 151 ms 325464 KB Output is correct
2 Correct 122 ms 325456 KB Output is correct
3 Correct 96 ms 325548 KB Output is correct
4 Correct 101 ms 325968 KB Output is correct
5 Correct 92 ms 325460 KB Output is correct
6 Correct 96 ms 325460 KB Output is correct
7 Correct 93 ms 325460 KB Output is correct
8 Correct 115 ms 325500 KB Output is correct
9 Correct 91 ms 325460 KB Output is correct
10 Correct 92 ms 325544 KB Output is correct
11 Correct 92 ms 325448 KB Output is correct
12 Correct 90 ms 325380 KB Output is correct
13 Correct 98 ms 325504 KB Output is correct
14 Correct 90 ms 325460 KB Output is correct
15 Correct 91 ms 325524 KB Output is correct
16 Correct 92 ms 325456 KB Output is correct
17 Correct 91 ms 325548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 151 ms 325464 KB Output is correct
2 Correct 122 ms 325456 KB Output is correct
3 Correct 96 ms 325548 KB Output is correct
4 Correct 101 ms 325968 KB Output is correct
5 Correct 92 ms 325460 KB Output is correct
6 Correct 96 ms 325460 KB Output is correct
7 Correct 93 ms 325460 KB Output is correct
8 Correct 115 ms 325500 KB Output is correct
9 Correct 91 ms 325460 KB Output is correct
10 Correct 92 ms 325544 KB Output is correct
11 Correct 92 ms 325448 KB Output is correct
12 Correct 90 ms 325380 KB Output is correct
13 Correct 98 ms 325504 KB Output is correct
14 Correct 90 ms 325460 KB Output is correct
15 Correct 91 ms 325524 KB Output is correct
16 Correct 92 ms 325456 KB Output is correct
17 Correct 91 ms 325548 KB Output is correct
18 Correct 96 ms 325476 KB Output is correct
19 Correct 96 ms 325716 KB Output is correct
20 Correct 94 ms 325496 KB Output is correct
21 Correct 100 ms 325472 KB Output is correct
22 Correct 95 ms 325624 KB Output is correct
23 Correct 94 ms 325668 KB Output is correct
24 Correct 93 ms 325584 KB Output is correct
25 Correct 93 ms 325492 KB Output is correct
26 Correct 99 ms 325536 KB Output is correct
27 Correct 93 ms 325360 KB Output is correct
28 Correct 94 ms 325464 KB Output is correct
29 Correct 98 ms 325548 KB Output is correct
30 Correct 96 ms 325504 KB Output is correct
31 Correct 92 ms 325456 KB Output is correct
32 Correct 87 ms 325556 KB Output is correct
33 Correct 78 ms 325484 KB Output is correct
34 Correct 79 ms 325512 KB Output is correct
35 Correct 79 ms 325588 KB Output is correct
36 Correct 78 ms 325384 KB Output is correct
37 Correct 79 ms 325460 KB Output is correct
38 Correct 86 ms 325452 KB Output is correct
39 Correct 84 ms 325492 KB Output is correct
40 Correct 77 ms 325456 KB Output is correct
41 Correct 78 ms 325460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 77 ms 325420 KB Output is correct
2 Runtime error 9 ms 600 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 151 ms 325464 KB Output is correct
2 Correct 122 ms 325456 KB Output is correct
3 Correct 96 ms 325548 KB Output is correct
4 Correct 101 ms 325968 KB Output is correct
5 Correct 92 ms 325460 KB Output is correct
6 Correct 96 ms 325460 KB Output is correct
7 Correct 93 ms 325460 KB Output is correct
8 Correct 115 ms 325500 KB Output is correct
9 Correct 91 ms 325460 KB Output is correct
10 Correct 92 ms 325544 KB Output is correct
11 Correct 92 ms 325448 KB Output is correct
12 Correct 90 ms 325380 KB Output is correct
13 Correct 98 ms 325504 KB Output is correct
14 Correct 90 ms 325460 KB Output is correct
15 Correct 91 ms 325524 KB Output is correct
16 Correct 92 ms 325456 KB Output is correct
17 Correct 91 ms 325548 KB Output is correct
18 Correct 96 ms 325476 KB Output is correct
19 Correct 96 ms 325716 KB Output is correct
20 Correct 94 ms 325496 KB Output is correct
21 Correct 100 ms 325472 KB Output is correct
22 Correct 95 ms 325624 KB Output is correct
23 Correct 94 ms 325668 KB Output is correct
24 Correct 93 ms 325584 KB Output is correct
25 Correct 93 ms 325492 KB Output is correct
26 Correct 99 ms 325536 KB Output is correct
27 Correct 93 ms 325360 KB Output is correct
28 Correct 94 ms 325464 KB Output is correct
29 Correct 98 ms 325548 KB Output is correct
30 Correct 96 ms 325504 KB Output is correct
31 Correct 92 ms 325456 KB Output is correct
32 Correct 87 ms 325556 KB Output is correct
33 Correct 78 ms 325484 KB Output is correct
34 Correct 79 ms 325512 KB Output is correct
35 Correct 79 ms 325588 KB Output is correct
36 Correct 78 ms 325384 KB Output is correct
37 Correct 79 ms 325460 KB Output is correct
38 Correct 86 ms 325452 KB Output is correct
39 Correct 84 ms 325492 KB Output is correct
40 Correct 77 ms 325456 KB Output is correct
41 Correct 78 ms 325460 KB Output is correct
42 Correct 77 ms 325420 KB Output is correct
43 Runtime error 9 ms 600 KB Execution killed with signal 11
44 Halted 0 ms 0 KB -