답안 #929843

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
929843 2024-02-18T08:18:18 Z beepbeepsheep Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
60 / 100
141 ms 779916 KB
#include <bits/stdc++.h>
using namespace std;

#define ll short
#define ii pair<ll,ll>
#define iii tuple<ll,ll,ll>

const short maxn=405;
const short inf=10000;

short dp[maxn][maxn][maxn][4];
short cost[maxn][maxn][maxn][4];
vector<short> r,g,y;
short suff[410];
short solve(short a, short b, short c, short flag){
	if (a<0 || b<0 || c<0) return inf;
	if (a==0 && b==0 && c==0) return 0;
	if (dp[a][b][c][flag]!=-1) return dp[a][b][c][flag];
	if (flag==1){
		dp[a][b][c][flag] = min(solve(a-1,b,c,2),solve(a-1,b,c,3));
	}
	if (flag==2){
		dp[a][b][c][flag] = min(solve(a,b-1,c,1),solve(a,b-1,c,3));
	}
	if (flag==3){
		dp[a][b][c][flag] = min(solve(a,b,c-1,1),solve(a,b,c-1,2));
	}
	dp[a][b][c][flag]+=cost[a][b][c][flag];
	return dp[a][b][c][flag];
}
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	short n;
	cin>>n;
	string s;
	cin>>s;
	for (short i=1;i<=n;i++){
		if (s[i-1]=='R') r.emplace_back(i);
		if (s[i-1]=='Y') y.emplace_back(i);
		if (s[i-1]=='G') g.emplace_back(i);
	}
	if (2*max({r.size(),g.size(),y.size()})>n+(n&1)){
		cout<<-1;
		return 0;
	}
	for (int i=0;i<=r.size();i++){
		for (int j=0;j<=g.size();j++){
			//create suffix sum array
			memset(suff,0,sizeof(suff));
			for (int k=0;k<i;k++) suff[r[k]]=1;
			for (int k=0;k<j;k++) suff[g[k]]=1;
			for (int k=405;k>=0;k--) suff[k]+=suff[k+1];
			for (int k=0;k<y.size();k++){
				cost[i][j][k+1][3]=abs((suff[y[k]]+y[k])-(i+j+k+1));
			}
		}
	}
	for (int i=0;i<=g.size();i++){
		for (int j=0;j<=y.size();j++){
			//create suffix sum array
			memset(suff,0,sizeof(suff));
			for (int k=0;k<i;k++) suff[g[k]]=1;
			for (int k=0;k<j;k++) suff[y[k]]=1;
			for (int k=405;k>=0;k--) suff[k]+=suff[k+1];
			for (int k=0;k<r.size();k++){
				cost[k+1][i][j][1]=abs((suff[r[k]]+r[k])-(i+j+k+1));
			}
		}
	}
	for (int i=0;i<=y.size();i++){
		for (int j=0;j<=r.size();j++){
			//create suffix sum array
			memset(suff,0,sizeof(suff));
			for (int k=0;k<i;k++) suff[y[k]]=1;
			for (int k=0;k<j;k++) suff[r[k]]=1;
			for (int k=405;k>=0;k--) suff[k]+=suff[k+1];
			for (int k=0;k<g.size();k++){
				cost[j][k+1][i][2]=abs((suff[g[k]]+g[k])-(i+j+k+1));
			}
		}
	}
	memset(dp,-1,sizeof(dp));
	cout<<min({solve(r.size(),g.size(),y.size(),1),
				solve(r.size(),g.size(),y.size(),2),
				solve(r.size(),g.size(),y.size(),3)});
	return 0;
}

Compilation message

joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:43:41: warning: comparison of integer expressions of different signedness: 'long unsigned int' and 'int' [-Wsign-compare]
   43 |  if (2*max({r.size(),g.size(),y.size()})>n+(n&1)){
      |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
joi2019_ho_t3.cpp:47:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<short int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |  for (int i=0;i<=r.size();i++){
      |               ~^~~~~~~~~~
joi2019_ho_t3.cpp:48:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<short int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |   for (int j=0;j<=g.size();j++){
      |                ~^~~~~~~~~~
joi2019_ho_t3.cpp:54:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<short int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |    for (int k=0;k<y.size();k++){
      |                 ~^~~~~~~~~
joi2019_ho_t3.cpp:59:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<short int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |  for (int i=0;i<=g.size();i++){
      |               ~^~~~~~~~~~
joi2019_ho_t3.cpp:60:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<short int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |   for (int j=0;j<=y.size();j++){
      |                ~^~~~~~~~~~
joi2019_ho_t3.cpp:66:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<short int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |    for (int k=0;k<r.size();k++){
      |                 ~^~~~~~~~~
joi2019_ho_t3.cpp:71:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<short int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |  for (int i=0;i<=y.size();i++){
      |               ~^~~~~~~~~~
joi2019_ho_t3.cpp:72:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<short int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |   for (int j=0;j<=r.size();j++){
      |                ~^~~~~~~~~~
joi2019_ho_t3.cpp:78:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<short int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |    for (int k=0;k<g.size();k++){
      |                 ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 523004 KB Output is correct
2 Correct 69 ms 523364 KB Output is correct
3 Correct 71 ms 521064 KB Output is correct
4 Correct 73 ms 525240 KB Output is correct
5 Correct 72 ms 529432 KB Output is correct
6 Correct 72 ms 529084 KB Output is correct
7 Correct 73 ms 531280 KB Output is correct
8 Correct 73 ms 529304 KB Output is correct
9 Correct 76 ms 529156 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 70 ms 529132 KB Output is correct
12 Correct 74 ms 529240 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 72 ms 529120 KB Output is correct
15 Correct 73 ms 527328 KB Output is correct
16 Correct 1 ms 504 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 523004 KB Output is correct
2 Correct 69 ms 523364 KB Output is correct
3 Correct 71 ms 521064 KB Output is correct
4 Correct 73 ms 525240 KB Output is correct
5 Correct 72 ms 529432 KB Output is correct
6 Correct 72 ms 529084 KB Output is correct
7 Correct 73 ms 531280 KB Output is correct
8 Correct 73 ms 529304 KB Output is correct
9 Correct 76 ms 529156 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 70 ms 529132 KB Output is correct
12 Correct 74 ms 529240 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 72 ms 529120 KB Output is correct
15 Correct 73 ms 527328 KB Output is correct
16 Correct 1 ms 504 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 76 ms 551916 KB Output is correct
19 Correct 75 ms 545600 KB Output is correct
20 Correct 77 ms 553988 KB Output is correct
21 Correct 77 ms 547664 KB Output is correct
22 Correct 79 ms 551732 KB Output is correct
23 Correct 74 ms 543572 KB Output is correct
24 Correct 78 ms 535512 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 79 ms 556036 KB Output is correct
28 Correct 77 ms 549696 KB Output is correct
29 Correct 78 ms 549712 KB Output is correct
30 Correct 75 ms 543596 KB Output is correct
31 Correct 78 ms 541652 KB Output is correct
32 Correct 74 ms 545616 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 82 ms 551748 KB Output is correct
36 Correct 76 ms 547680 KB Output is correct
37 Correct 73 ms 539472 KB Output is correct
38 Correct 78 ms 539596 KB Output is correct
39 Correct 78 ms 545760 KB Output is correct
40 Correct 0 ms 344 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 71 ms 525136 KB Output is correct
2 Incorrect 141 ms 779916 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 523004 KB Output is correct
2 Correct 69 ms 523364 KB Output is correct
3 Correct 71 ms 521064 KB Output is correct
4 Correct 73 ms 525240 KB Output is correct
5 Correct 72 ms 529432 KB Output is correct
6 Correct 72 ms 529084 KB Output is correct
7 Correct 73 ms 531280 KB Output is correct
8 Correct 73 ms 529304 KB Output is correct
9 Correct 76 ms 529156 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 70 ms 529132 KB Output is correct
12 Correct 74 ms 529240 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 72 ms 529120 KB Output is correct
15 Correct 73 ms 527328 KB Output is correct
16 Correct 1 ms 504 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 76 ms 551916 KB Output is correct
19 Correct 75 ms 545600 KB Output is correct
20 Correct 77 ms 553988 KB Output is correct
21 Correct 77 ms 547664 KB Output is correct
22 Correct 79 ms 551732 KB Output is correct
23 Correct 74 ms 543572 KB Output is correct
24 Correct 78 ms 535512 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 79 ms 556036 KB Output is correct
28 Correct 77 ms 549696 KB Output is correct
29 Correct 78 ms 549712 KB Output is correct
30 Correct 75 ms 543596 KB Output is correct
31 Correct 78 ms 541652 KB Output is correct
32 Correct 74 ms 545616 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 82 ms 551748 KB Output is correct
36 Correct 76 ms 547680 KB Output is correct
37 Correct 73 ms 539472 KB Output is correct
38 Correct 78 ms 539596 KB Output is correct
39 Correct 78 ms 545760 KB Output is correct
40 Correct 0 ms 344 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 71 ms 525136 KB Output is correct
43 Incorrect 141 ms 779916 KB Output isn't correct
44 Halted 0 ms 0 KB -