답안 #124207

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
124207 2019-07-02T16:23:52 Z rajarshi_basu Growing Vegetable is Fun 3 (JOI19_ho_t3) C++14
100 / 100
336 ms 4352 KB
#include <iostream>
#include <vector>
#include <set>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <fstream>
#include <complex>
#include <stack>
#include <set>

#define FOR(i,n) for(int i=0;i<n;i++)
#define FORE(i,a,b) for(int i=a;i<=b;i++)
#define ll long long int
#define vi vector<int>
#define ii pair<int,int>
#define pb push_back
#define mp make_pair
#define vv vector
#define ff first
#define ss second
#define pll pair<ll,ll>
#define cd complex<double> 
#define ld long double
#define pld pair<ld,ld>
#define iii pair<ii,int>

using namespace std;

const int INF = 1e9;
const int MAXN = 400+5;
const int MAXK = 100+10;
// 0 = red, 1 = green, 2 = blue;
int dp[2][MAXN][MAXN][3]; // [index][redused][blueused][color of last cell] = min cost (blue used can be inferred)
int pref[MAXN][3];


int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);

   	int n;
   	string s;
   	FOR(i,2)FOR(j,MAXN)FOR(k,MAXN)FOR(l,3)dp[i][j][k][l] = INF;
   	cin >> n;
   	cin >> s;
   	vi r,g,b;
   	FOR(i,n){
   		switch(s[i]){
   			case 'R':r.pb(i);break;
   			case 'G':g.pb(i);break;
   			case 'Y':b.pb(i);break;
   		}
   		if(i == 0){
   			pref[i][0] = s[i] == 'R';
   			pref[i][1] = s[i] == 'G';
   			pref[i][2] = s[i] == 'Y';
   		}else{
   			pref[i][0] = (s[i] == 'R') + pref[i-1][0];
   			pref[i][1] = (s[i] == 'G') + pref[i-1][1];
   			pref[i][2] = (s[i] == 'Y') + pref[i-1][2];
   		}
   	}


   	FOR(i,n){
   		FOR(j,MAXN)FOR(k,MAXN)FOR(l,3){
   			dp[0][j][k][l] = dp[1][j][k][l];
   			dp[1][j][k][l] = INF;
   		}
   		FOR(rr,r.size()+1){
   			FOR(gg,g.size()+1){
   				int bb = (i+1) - (rr+gg);
   				if(min(bb,min(rr,gg)) < 0 or bb > b.size())continue;
   				//cout << rr << " " << gg << " " << bb << endl;
   				
   				// try making this a red color cell;
   				if(rr > 0){
   					int cst = 0;
	   				if(i == 0){
	   					cst = 0;
	   				}else{
	   					cst = min(dp[0][rr-1][gg][1],dp[0][rr-1][gg][2]);
	   				}

	   				int ctr = r[rr-1];
	   				ctr -= min(rr-1,pref[r[rr-1]][0]);
	   				ctr -= min(gg,pref[r[rr-1]][1]);
	   				ctr -= min(bb,pref[r[rr-1]][2]);
	   				dp[1][rr][gg][0] = min(dp[1][rr][gg][0],ctr + cst);
	   			}
	   			// try for green
	   			if(gg > 0){
   					int cst = 0;
	   				if(i == 0){
	   					cst = 0;
	   				}else{
	   					cst = min(dp[0][rr][gg-1][0],dp[0][rr][gg-1][2]);
	   				}
	   				
	   				int ctr = g[gg-1];
	   				ctr -= min(rr,pref[g[gg-1]][0]);
	   				ctr -= min(gg-1,pref[g[gg-1]][1]);
	   				ctr -= min(bb,pref[g[gg-1]][2]);
	   				dp[1][rr][gg][1] = min(dp[1][rr][gg][1],ctr + cst);
	   			}
	   			// try for blue
	   			if(bb > 0){
   					int cst = 0;
	   				if(i == 0){
	   					cst = 0;
	   				}else{
	   					cst = min(dp[0][rr][gg][0],dp[0][rr][gg][1]);
	   				}
	   				int ctr = b[bb-1];
	   				ctr -= min(rr,pref[b[bb-1]][0]);
	   				ctr -= min(gg,pref[b[bb-1]][1]);
	   				ctr -= min(bb-1,pref[b[bb-1]][2]);
	   				dp[1][rr][gg][2] = min(dp[1][rr][gg][2],ctr + cst);
	   			}
   			}
   		}
   	}
   	int ans = INF;
   	FOR(k,3)ans = min(dp[1][r.size()][g.size()][k],ans);
   	if(ans >= INF){
   		cout << -1 << endl;
   	}else{
   		cout << ans << endl;
   	}

    return 0;
}

Compilation message

joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:17:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define FOR(i,n) for(int i=0;i<n;i++)
joi2019_ho_t3.cpp:76:10:
      FOR(rr,r.size()+1){
          ~~~~~~~~~~~~~         
joi2019_ho_t3.cpp:76:6: note: in expansion of macro 'FOR'
      FOR(rr,r.size()+1){
      ^~~
joi2019_ho_t3.cpp:17:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define FOR(i,n) for(int i=0;i<n;i++)
joi2019_ho_t3.cpp:77:11:
       FOR(gg,g.size()+1){
           ~~~~~~~~~~~~~        
joi2019_ho_t3.cpp:77:7: note: in expansion of macro 'FOR'
       FOR(gg,g.size()+1){
       ^~~
joi2019_ho_t3.cpp:79:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
        if(min(bb,min(rr,gg)) < 0 or bb > b.size())continue;
                                     ~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4216 KB Output is correct
2 Correct 7 ms 4216 KB Output is correct
3 Correct 7 ms 4216 KB Output is correct
4 Correct 12 ms 4216 KB Output is correct
5 Correct 16 ms 4220 KB Output is correct
6 Correct 16 ms 4216 KB Output is correct
7 Correct 16 ms 4216 KB Output is correct
8 Correct 16 ms 4216 KB Output is correct
9 Correct 16 ms 4216 KB Output is correct
10 Correct 16 ms 4216 KB Output is correct
11 Correct 16 ms 4216 KB Output is correct
12 Correct 16 ms 4216 KB Output is correct
13 Correct 16 ms 4216 KB Output is correct
14 Correct 15 ms 4216 KB Output is correct
15 Correct 16 ms 4216 KB Output is correct
16 Correct 16 ms 4344 KB Output is correct
17 Correct 15 ms 4216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4216 KB Output is correct
2 Correct 7 ms 4216 KB Output is correct
3 Correct 7 ms 4216 KB Output is correct
4 Correct 12 ms 4216 KB Output is correct
5 Correct 16 ms 4220 KB Output is correct
6 Correct 16 ms 4216 KB Output is correct
7 Correct 16 ms 4216 KB Output is correct
8 Correct 16 ms 4216 KB Output is correct
9 Correct 16 ms 4216 KB Output is correct
10 Correct 16 ms 4216 KB Output is correct
11 Correct 16 ms 4216 KB Output is correct
12 Correct 16 ms 4216 KB Output is correct
13 Correct 16 ms 4216 KB Output is correct
14 Correct 15 ms 4216 KB Output is correct
15 Correct 16 ms 4216 KB Output is correct
16 Correct 16 ms 4344 KB Output is correct
17 Correct 15 ms 4216 KB Output is correct
18 Correct 49 ms 4216 KB Output is correct
19 Correct 49 ms 4264 KB Output is correct
20 Correct 49 ms 4264 KB Output is correct
21 Correct 49 ms 4216 KB Output is correct
22 Correct 48 ms 4216 KB Output is correct
23 Correct 49 ms 4216 KB Output is correct
24 Correct 48 ms 4216 KB Output is correct
25 Correct 49 ms 4344 KB Output is correct
26 Correct 49 ms 4344 KB Output is correct
27 Correct 48 ms 4216 KB Output is correct
28 Correct 48 ms 4268 KB Output is correct
29 Correct 49 ms 4216 KB Output is correct
30 Correct 48 ms 4216 KB Output is correct
31 Correct 49 ms 4216 KB Output is correct
32 Correct 48 ms 4216 KB Output is correct
33 Correct 48 ms 4344 KB Output is correct
34 Correct 48 ms 4216 KB Output is correct
35 Correct 47 ms 4344 KB Output is correct
36 Correct 48 ms 4216 KB Output is correct
37 Correct 47 ms 4216 KB Output is correct
38 Correct 48 ms 4216 KB Output is correct
39 Correct 48 ms 4216 KB Output is correct
40 Correct 47 ms 4216 KB Output is correct
41 Correct 48 ms 4216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 4216 KB Output is correct
2 Correct 319 ms 4220 KB Output is correct
3 Correct 316 ms 4216 KB Output is correct
4 Correct 322 ms 4344 KB Output is correct
5 Correct 320 ms 4344 KB Output is correct
6 Correct 318 ms 4344 KB Output is correct
7 Correct 317 ms 4344 KB Output is correct
8 Correct 317 ms 4216 KB Output is correct
9 Correct 316 ms 4216 KB Output is correct
10 Correct 320 ms 4344 KB Output is correct
11 Correct 317 ms 4216 KB Output is correct
12 Correct 159 ms 4216 KB Output is correct
13 Correct 213 ms 4240 KB Output is correct
14 Correct 264 ms 4216 KB Output is correct
15 Correct 325 ms 4216 KB Output is correct
16 Correct 318 ms 4344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 4216 KB Output is correct
2 Correct 7 ms 4216 KB Output is correct
3 Correct 7 ms 4216 KB Output is correct
4 Correct 12 ms 4216 KB Output is correct
5 Correct 16 ms 4220 KB Output is correct
6 Correct 16 ms 4216 KB Output is correct
7 Correct 16 ms 4216 KB Output is correct
8 Correct 16 ms 4216 KB Output is correct
9 Correct 16 ms 4216 KB Output is correct
10 Correct 16 ms 4216 KB Output is correct
11 Correct 16 ms 4216 KB Output is correct
12 Correct 16 ms 4216 KB Output is correct
13 Correct 16 ms 4216 KB Output is correct
14 Correct 15 ms 4216 KB Output is correct
15 Correct 16 ms 4216 KB Output is correct
16 Correct 16 ms 4344 KB Output is correct
17 Correct 15 ms 4216 KB Output is correct
18 Correct 49 ms 4216 KB Output is correct
19 Correct 49 ms 4264 KB Output is correct
20 Correct 49 ms 4264 KB Output is correct
21 Correct 49 ms 4216 KB Output is correct
22 Correct 48 ms 4216 KB Output is correct
23 Correct 49 ms 4216 KB Output is correct
24 Correct 48 ms 4216 KB Output is correct
25 Correct 49 ms 4344 KB Output is correct
26 Correct 49 ms 4344 KB Output is correct
27 Correct 48 ms 4216 KB Output is correct
28 Correct 48 ms 4268 KB Output is correct
29 Correct 49 ms 4216 KB Output is correct
30 Correct 48 ms 4216 KB Output is correct
31 Correct 49 ms 4216 KB Output is correct
32 Correct 48 ms 4216 KB Output is correct
33 Correct 48 ms 4344 KB Output is correct
34 Correct 48 ms 4216 KB Output is correct
35 Correct 47 ms 4344 KB Output is correct
36 Correct 48 ms 4216 KB Output is correct
37 Correct 47 ms 4216 KB Output is correct
38 Correct 48 ms 4216 KB Output is correct
39 Correct 48 ms 4216 KB Output is correct
40 Correct 47 ms 4216 KB Output is correct
41 Correct 48 ms 4216 KB Output is correct
42 Correct 8 ms 4216 KB Output is correct
43 Correct 319 ms 4220 KB Output is correct
44 Correct 316 ms 4216 KB Output is correct
45 Correct 322 ms 4344 KB Output is correct
46 Correct 320 ms 4344 KB Output is correct
47 Correct 318 ms 4344 KB Output is correct
48 Correct 317 ms 4344 KB Output is correct
49 Correct 317 ms 4216 KB Output is correct
50 Correct 316 ms 4216 KB Output is correct
51 Correct 320 ms 4344 KB Output is correct
52 Correct 317 ms 4216 KB Output is correct
53 Correct 159 ms 4216 KB Output is correct
54 Correct 213 ms 4240 KB Output is correct
55 Correct 264 ms 4216 KB Output is correct
56 Correct 325 ms 4216 KB Output is correct
57 Correct 318 ms 4344 KB Output is correct
58 Correct 332 ms 4240 KB Output is correct
59 Correct 335 ms 4344 KB Output is correct
60 Correct 331 ms 4220 KB Output is correct
61 Correct 333 ms 4344 KB Output is correct
62 Correct 321 ms 4216 KB Output is correct
63 Correct 325 ms 4344 KB Output is correct
64 Correct 333 ms 4352 KB Output is correct
65 Correct 331 ms 4216 KB Output is correct
66 Correct 332 ms 4216 KB Output is correct
67 Correct 332 ms 4316 KB Output is correct
68 Correct 335 ms 4316 KB Output is correct
69 Correct 336 ms 4344 KB Output is correct
70 Correct 333 ms 4216 KB Output is correct
71 Correct 333 ms 4216 KB Output is correct
72 Correct 308 ms 4344 KB Output is correct
73 Correct 305 ms 4344 KB Output is correct