Submission #163522

# Submission time Handle Problem Language Result Execution time Memory
163522 2019-11-13T17:42:39 Z nvmdava Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
75 / 100
500 ms 163160 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define pb push_back
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define N 405
#define INF 0x3f3f3f3f
#define MOD 1000000007LL
 
char c;
int dp[N][N][N][3];
int a[N];
vector<int> app[3];
 
int i[3];

int cost(int k){
	i[k]--;
	int res = INF;
	for(int r = 0; r < 3; ++r)
		if(r != k)
			res = min(res, dp[i[0]][i[1]][i[2]][r]);
	
	int v = app[k][i[k]];
	i[k]++;
	int w = v;
	for(int j = 0; j < 3; j++){
		int a = upper_bound(app[j].begin(), app[j].begin() + i[j], v) - app[j].begin();
		w += max(0, i[j] - a);
	}
 
	return res + abs(i[0] + i[1] + i[2] - w);
}

int s[3];
 
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
 
    int n;
    cin>>n;
 
    for(int i = 1; i <= n; i++){
    	cin>>c;
    	if(c == 'R')
    		app[0].push_back(i);
    	else if(c == 'G')
    		app[1].push_back(i);
    	else
    		app[2].push_back(i);
    }

    for(int i = 0; i < 3; ++i)
    	s[i] = app[i].size();

    for(i[0] = 0; i[0] <= s[0]; ++i[0]){
    	for(i[1] = 0; i[1] <= s[1]; ++i[1]){
    		for(i[2] = 0; i[2] <= s[2]; ++i[2]){
    			for(int k = 0; k < 3; ++k){

    				if(i[0] + i[1] + i[2] == 0)
    					dp[i[0]][i[1]][i[2]][k] = 0;
    				else if(i[k] == 0)
	    				dp[i[0]][i[1]][i[2]][k] = INF;
	    			else
 		   				dp[i[0]][i[1]][i[2]][k] = cost(k);
    			}
    		}
    	}
    }
    int res = min(dp[s[0]][s[1]][s[2]][0], min(dp[s[0]][s[1]][s[2]][1], dp[s[0]][s[1]][s[2]][2]));
    if(res == 0x3f3f3f3f)
    	cout<<-1;
    else
    	cout<<res;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 8 ms 504 KB Output is correct
6 Correct 2 ms 504 KB Output is correct
7 Correct 2 ms 504 KB Output is correct
8 Correct 2 ms 508 KB Output is correct
9 Correct 3 ms 632 KB Output is correct
10 Correct 2 ms 632 KB Output is correct
11 Correct 2 ms 504 KB Output is correct
12 Correct 2 ms 508 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 504 KB Output is correct
15 Correct 2 ms 504 KB Output is correct
16 Correct 2 ms 504 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 8 ms 504 KB Output is correct
6 Correct 2 ms 504 KB Output is correct
7 Correct 2 ms 504 KB Output is correct
8 Correct 2 ms 508 KB Output is correct
9 Correct 3 ms 632 KB Output is correct
10 Correct 2 ms 632 KB Output is correct
11 Correct 2 ms 504 KB Output is correct
12 Correct 2 ms 508 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 504 KB Output is correct
15 Correct 2 ms 504 KB Output is correct
16 Correct 2 ms 504 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 5 ms 2296 KB Output is correct
19 Correct 5 ms 2040 KB Output is correct
20 Correct 5 ms 2296 KB Output is correct
21 Correct 5 ms 2424 KB Output is correct
22 Correct 4 ms 1784 KB Output is correct
23 Correct 5 ms 2168 KB Output is correct
24 Correct 4 ms 1656 KB Output is correct
25 Correct 5 ms 3960 KB Output is correct
26 Correct 5 ms 4216 KB Output is correct
27 Correct 5 ms 3064 KB Output is correct
28 Correct 5 ms 2424 KB Output is correct
29 Correct 5 ms 2296 KB Output is correct
30 Correct 5 ms 2300 KB Output is correct
31 Correct 4 ms 2040 KB Output is correct
32 Correct 5 ms 2424 KB Output is correct
33 Correct 5 ms 3960 KB Output is correct
34 Correct 5 ms 3576 KB Output is correct
35 Correct 5 ms 2680 KB Output is correct
36 Correct 4 ms 1912 KB Output is correct
37 Correct 4 ms 1784 KB Output is correct
38 Correct 4 ms 2168 KB Output is correct
39 Correct 5 ms 2296 KB Output is correct
40 Correct 2 ms 376 KB Output is correct
41 Correct 4 ms 2296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 123 ms 163064 KB Output is correct
3 Correct 171 ms 162280 KB Output is correct
4 Correct 138 ms 163064 KB Output is correct
5 Correct 138 ms 163068 KB Output is correct
6 Correct 139 ms 163024 KB Output is correct
7 Correct 140 ms 162296 KB Output is correct
8 Correct 126 ms 162200 KB Output is correct
9 Correct 137 ms 161448 KB Output is correct
10 Correct 130 ms 163160 KB Output is correct
11 Correct 132 ms 162948 KB Output is correct
12 Correct 36 ms 44152 KB Output is correct
13 Correct 67 ms 77308 KB Output is correct
14 Correct 97 ms 111352 KB Output is correct
15 Correct 126 ms 163064 KB Output is correct
16 Correct 125 ms 163040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 8 ms 504 KB Output is correct
6 Correct 2 ms 504 KB Output is correct
7 Correct 2 ms 504 KB Output is correct
8 Correct 2 ms 508 KB Output is correct
9 Correct 3 ms 632 KB Output is correct
10 Correct 2 ms 632 KB Output is correct
11 Correct 2 ms 504 KB Output is correct
12 Correct 2 ms 508 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 504 KB Output is correct
15 Correct 2 ms 504 KB Output is correct
16 Correct 2 ms 504 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 5 ms 2296 KB Output is correct
19 Correct 5 ms 2040 KB Output is correct
20 Correct 5 ms 2296 KB Output is correct
21 Correct 5 ms 2424 KB Output is correct
22 Correct 4 ms 1784 KB Output is correct
23 Correct 5 ms 2168 KB Output is correct
24 Correct 4 ms 1656 KB Output is correct
25 Correct 5 ms 3960 KB Output is correct
26 Correct 5 ms 4216 KB Output is correct
27 Correct 5 ms 3064 KB Output is correct
28 Correct 5 ms 2424 KB Output is correct
29 Correct 5 ms 2296 KB Output is correct
30 Correct 5 ms 2300 KB Output is correct
31 Correct 4 ms 2040 KB Output is correct
32 Correct 5 ms 2424 KB Output is correct
33 Correct 5 ms 3960 KB Output is correct
34 Correct 5 ms 3576 KB Output is correct
35 Correct 5 ms 2680 KB Output is correct
36 Correct 4 ms 1912 KB Output is correct
37 Correct 4 ms 1784 KB Output is correct
38 Correct 4 ms 2168 KB Output is correct
39 Correct 5 ms 2296 KB Output is correct
40 Correct 2 ms 376 KB Output is correct
41 Correct 4 ms 2296 KB Output is correct
42 Correct 2 ms 376 KB Output is correct
43 Correct 123 ms 163064 KB Output is correct
44 Correct 171 ms 162280 KB Output is correct
45 Correct 138 ms 163064 KB Output is correct
46 Correct 138 ms 163068 KB Output is correct
47 Correct 139 ms 163024 KB Output is correct
48 Correct 140 ms 162296 KB Output is correct
49 Correct 126 ms 162200 KB Output is correct
50 Correct 137 ms 161448 KB Output is correct
51 Correct 130 ms 163160 KB Output is correct
52 Correct 132 ms 162948 KB Output is correct
53 Correct 36 ms 44152 KB Output is correct
54 Correct 67 ms 77308 KB Output is correct
55 Correct 97 ms 111352 KB Output is correct
56 Correct 126 ms 163064 KB Output is correct
57 Correct 125 ms 163040 KB Output is correct
58 Correct 485 ms 76380 KB Output is correct
59 Execution timed out 501 ms 96632 KB Time limit exceeded
60 Halted 0 ms 0 KB -