답안 #218965

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
218965 2020-04-03T10:13:44 Z Rakhmand Growing Vegetable is Fun 3 (JOI19_ho_t3) C++14
100 / 100
163 ms 163192 KB
//  I feel still alive.
//  Created by existence on 18/04/2003.
//  Copyright © 2020 Rakhman. All rights reserved.
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <iterator>

#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define S second
#define F first
#define nl '\n'

const long long mxn = 510501;
const long long mod = 1e5 + 7;
const long long inf = 1e18;

typedef long long llong;

using namespace std;

void istxt(bool yes){
    if(yes == 1){
        freopen("sum2.in", "r", stdin);
        freopen("sum2.out", "w", stdout);
    }else{
        freopen("/Users/rakhmanabdirashov/Desktop/folder/Programming/Road2Master/Road2Master/input.txt", "r", stdin);
    }
}

int n, cnt[3], dp[410][410][410][3], a[mxn], pr[mxn][3];
int nx[3][410];
string s;

int main(){
    ios;
    cin >> n;
    cin >> s;
    s = '#' + s;
    for(int i = 1; i <= n; i++){
        a[i] = (s[i] == 'R' ? 0 : (s[i] == 'G' ? 1 : 2));
        cnt[a[i]]++;
        nx[a[i]][cnt[a[i]]] = i;
    }
    for(int i = 1; i <= n; i++){
        for(int f = 0; f <= 2; f++){
            pr[i][f] = pr[i - 1][f];
        }
        pr[i][a[i]]++;
    }
    for(int i = 0; i <= cnt[0]; i++){
        for(int j = 0; j <= cnt[1]; j++){
            for(int k = 0; k <= cnt[2]; k++){
                for(int f = 0; f <= 2; f++){
                    dp[i][j][k][f] = mxn;
                }
            }
        }
    }
    for(int f = 0; f <= 2; f++) dp[0][0][0][f] = 0;
    for(int i = 0; i <= cnt[0]; i++){
        for(int j = 0; j <= cnt[1]; j++){
            for(int k = 0; k <= cnt[2]; k++){
                for(int last = 0; last < 3; last ++){
                    for(int cur = 0; cur < 3; cur++){
                        if(cur == last) continue;
                        if((cur == 0 && cnt[0] == i) || (cur == 1 && cnt[1] == j) || (cur == 2 && cnt[2] == k)) continue;
                        
                        int ans = 0;
                        int pos1, pos2, pos3;
                        int other = 3 - last - cur;
                        if(last == 0){
                            pos1 = nx[0][i];
                            if(cur == 1){
                                pos2 = nx[1][j + 1], pos3 = nx[2][k];
                            }else{
                                pos2 = nx[2][k + 1], pos3 = nx[1][j];
                            }
                        }else if(last == 1){
                            pos1 = nx[1][j];
                            if(cur == 0){
                                pos2 = nx[0][i + 1], pos3 = nx[2][k];
                            }else{
                                pos2 = nx[2][k + 1], pos3 = nx[0][i];
                            }
                        }else{
                            pos1 = nx[2][k];
                            if(cur == 0){
                                pos2 = nx[0][i + 1], pos3 = nx[1][j];
                            }else{
                                pos2 = nx[1][j + 1], pos3 = nx[0][i];
                            }
                        }
                        ans += max(0, pr[pos2][last] - pr[pos1][last]);
                        ans += max(0, pr[pos2][other] - pr[pos3][other]);
                        ans += dp[i][j][k][last];
                        if(cur == 0){
                            dp[i + 1][j][k][cur] = min(dp[i + 1][j][k][cur], ans);
                        }else if(cur == 1){
                            dp[i][j + 1][k][cur] = min(dp[i][j + 1][k][cur], ans);
                        }else{
                            dp[i][j][k + 1][cur] = min(dp[i][j][k + 1][cur], ans);
                        }
                    }
                }
            }
        }
    }
    int ans = mxn;
    for(int i = 0; i < 3; i++){
        ans = min(ans, dp[cnt[0]][cnt[1]][cnt[2]][i]);
    }
    if(ans == mxn) cout << -1;
    else cout << ans;
}

Compilation message

joi2019_ho_t3.cpp: In function 'void istxt(bool)':
joi2019_ho_t3.cpp:42:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
         freopen("sum2.in", "r", stdin);
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:43:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
         freopen("sum2.out", "w", stdout);
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:45:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
         freopen("/Users/rakhmanabdirashov/Desktop/folder/Programming/Road2Master/Road2Master/input.txt", "r", stdin);
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 5 ms 512 KB Output is correct
5 Correct 5 ms 512 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 5 ms 512 KB Output is correct
8 Correct 5 ms 512 KB Output is correct
9 Correct 5 ms 640 KB Output is correct
10 Correct 5 ms 640 KB Output is correct
11 Correct 5 ms 512 KB Output is correct
12 Correct 5 ms 512 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 4 ms 512 KB Output is correct
15 Correct 5 ms 512 KB Output is correct
16 Correct 5 ms 512 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 5 ms 512 KB Output is correct
5 Correct 5 ms 512 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 5 ms 512 KB Output is correct
8 Correct 5 ms 512 KB Output is correct
9 Correct 5 ms 640 KB Output is correct
10 Correct 5 ms 640 KB Output is correct
11 Correct 5 ms 512 KB Output is correct
12 Correct 5 ms 512 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 4 ms 512 KB Output is correct
15 Correct 5 ms 512 KB Output is correct
16 Correct 5 ms 512 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
18 Correct 6 ms 2432 KB Output is correct
19 Correct 6 ms 1920 KB Output is correct
20 Correct 6 ms 2304 KB Output is correct
21 Correct 6 ms 2432 KB Output is correct
22 Correct 5 ms 1792 KB Output is correct
23 Correct 6 ms 2176 KB Output is correct
24 Correct 5 ms 1664 KB Output is correct
25 Correct 6 ms 3968 KB Output is correct
26 Correct 6 ms 4224 KB Output is correct
27 Correct 6 ms 3200 KB Output is correct
28 Correct 6 ms 2432 KB Output is correct
29 Correct 6 ms 2304 KB Output is correct
30 Correct 6 ms 2432 KB Output is correct
31 Correct 6 ms 2048 KB Output is correct
32 Correct 6 ms 2432 KB Output is correct
33 Correct 7 ms 3968 KB Output is correct
34 Correct 8 ms 3584 KB Output is correct
35 Correct 6 ms 2720 KB Output is correct
36 Correct 6 ms 2048 KB Output is correct
37 Correct 5 ms 1792 KB Output is correct
38 Correct 6 ms 2176 KB Output is correct
39 Correct 6 ms 2432 KB Output is correct
40 Correct 5 ms 512 KB Output is correct
41 Correct 6 ms 2304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 77 ms 163064 KB Output is correct
3 Correct 79 ms 162216 KB Output is correct
4 Correct 77 ms 163064 KB Output is correct
5 Correct 78 ms 163064 KB Output is correct
6 Correct 77 ms 163192 KB Output is correct
7 Correct 78 ms 162244 KB Output is correct
8 Correct 78 ms 162168 KB Output is correct
9 Correct 75 ms 161400 KB Output is correct
10 Correct 78 ms 163064 KB Output is correct
11 Correct 79 ms 163064 KB Output is correct
12 Correct 25 ms 44160 KB Output is correct
13 Correct 40 ms 77176 KB Output is correct
14 Correct 58 ms 111480 KB Output is correct
15 Correct 77 ms 163064 KB Output is correct
16 Correct 79 ms 163064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 5 ms 512 KB Output is correct
5 Correct 5 ms 512 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 5 ms 512 KB Output is correct
8 Correct 5 ms 512 KB Output is correct
9 Correct 5 ms 640 KB Output is correct
10 Correct 5 ms 640 KB Output is correct
11 Correct 5 ms 512 KB Output is correct
12 Correct 5 ms 512 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 4 ms 512 KB Output is correct
15 Correct 5 ms 512 KB Output is correct
16 Correct 5 ms 512 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
18 Correct 6 ms 2432 KB Output is correct
19 Correct 6 ms 1920 KB Output is correct
20 Correct 6 ms 2304 KB Output is correct
21 Correct 6 ms 2432 KB Output is correct
22 Correct 5 ms 1792 KB Output is correct
23 Correct 6 ms 2176 KB Output is correct
24 Correct 5 ms 1664 KB Output is correct
25 Correct 6 ms 3968 KB Output is correct
26 Correct 6 ms 4224 KB Output is correct
27 Correct 6 ms 3200 KB Output is correct
28 Correct 6 ms 2432 KB Output is correct
29 Correct 6 ms 2304 KB Output is correct
30 Correct 6 ms 2432 KB Output is correct
31 Correct 6 ms 2048 KB Output is correct
32 Correct 6 ms 2432 KB Output is correct
33 Correct 7 ms 3968 KB Output is correct
34 Correct 8 ms 3584 KB Output is correct
35 Correct 6 ms 2720 KB Output is correct
36 Correct 6 ms 2048 KB Output is correct
37 Correct 5 ms 1792 KB Output is correct
38 Correct 6 ms 2176 KB Output is correct
39 Correct 6 ms 2432 KB Output is correct
40 Correct 5 ms 512 KB Output is correct
41 Correct 6 ms 2304 KB Output is correct
42 Correct 4 ms 384 KB Output is correct
43 Correct 77 ms 163064 KB Output is correct
44 Correct 79 ms 162216 KB Output is correct
45 Correct 77 ms 163064 KB Output is correct
46 Correct 78 ms 163064 KB Output is correct
47 Correct 77 ms 163192 KB Output is correct
48 Correct 78 ms 162244 KB Output is correct
49 Correct 78 ms 162168 KB Output is correct
50 Correct 75 ms 161400 KB Output is correct
51 Correct 78 ms 163064 KB Output is correct
52 Correct 79 ms 163064 KB Output is correct
53 Correct 25 ms 44160 KB Output is correct
54 Correct 40 ms 77176 KB Output is correct
55 Correct 58 ms 111480 KB Output is correct
56 Correct 77 ms 163064 KB Output is correct
57 Correct 79 ms 163064 KB Output is correct
58 Correct 151 ms 77432 KB Output is correct
59 Correct 159 ms 97784 KB Output is correct
60 Correct 152 ms 88960 KB Output is correct
61 Correct 152 ms 86008 KB Output is correct
62 Correct 87 ms 160760 KB Output is correct
63 Correct 95 ms 160248 KB Output is correct
64 Correct 126 ms 150136 KB Output is correct
65 Correct 155 ms 138576 KB Output is correct
66 Correct 156 ms 87160 KB Output is correct
67 Correct 152 ms 83960 KB Output is correct
68 Correct 155 ms 89848 KB Output is correct
69 Correct 163 ms 86528 KB Output is correct
70 Correct 155 ms 93688 KB Output is correct
71 Correct 149 ms 85096 KB Output is correct
72 Correct 51 ms 42112 KB Output is correct
73 Correct 12 ms 2944 KB Output is correct