Submission #1087922

# Submission time Handle Problem Language Result Execution time Memory
1087922 2024-09-13T14:10:00 Z Zero_OP Growing Vegetable is Fun 3 (JOI19_ho_t3) C++14
100 / 100
436 ms 780628 KB
#include<bits/stdc++.h>

using namespace std;

#define sz(v) (int)v.size()
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define dbg(v) "[" #v " = " << (v) << "]"
#define file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define rep(i, l, r) for(int i = (l); i < (r); ++i)

using ll = long long;
using vi = vector<int>;
using vl = vector<long long>;
using ld = long double;

template<typename T> 
    bool minimize(T& a, const T& b){
        if(a > b){
            return a = b, true;
        }  return false;
    }

template<typename T>
    bool maximize(T& a, const T& b){    
        if(a < b){
            return a = b, true;
        } return false;
    }

template<int dimension, typename T>
struct tensor : public vector<tensor<dimension - 1, T>> {
    static_assert(dimension > 0, "Dimension must be positive !\n");
    template<typename... Args>
    tensor(int n = 0, Args... args) : vector<tensor<dimension - 1, T>> (n, tensor<dimension - 1, T>(args...)) {}
};
 
template<typename T>
struct tensor<1, T> : public vector<T> {
    tensor(int n = 0, T val = T()) : vector<T>(n, val) {}
};

const int MAX = 405;

int n, num[3], cnt[3][MAX], pos[3][MAX], dp[3][MAX][MAX][MAX];
string s;

int count(int i, int l, int r){
    if(l > r) return 0;
    return cnt[i][r] - cnt[i][l];
}

int find_other(int a, int b){
    if(a > b) swap(a, b);
    if(a > 0) return 0;
    if(b < 2) return 2;
    return 1;
}

void relable(string& s){
    int r = 0, g = 0, y = 0;
    for(int i = 0; i < sz(s); ++i){
        r += (s[i] == 'R');
        g += (s[i] == 'G');
        y += (s[i] == 'Y');
    }

    vector<pair<int, int>> pts;
    pts.push_back({r, 'R'});
    pts.push_back({g, 'G'});
    pts.push_back({y, 'Y'});

    sort(all(pts));

    for(int i = 0; i < sz(s); ++i){
        for(int j = 0; j < 3; ++j){
            if(s[i] == pts[j].second){
                s[i] = j;
                break;
            }
        }
    }
}

void testcase(){
    cin >> n >> s;
    relable(s);

    for(int i = 0; i < n; ++i){
        pos[s[i]][++num[s[i]]] = i + 1;
        for(int j = 0; j < 3; ++j) cnt[j][i + 1] = num[j];
    }
    
    memset(dp, 0x3f, sizeof(dp));
    int inf = dp[0][0][0][0];
    for(int i = 0; i < 3; ++i) dp[i][0][0][0] = 0;

    for(int i = 0; i <= num[0]; ++i){
        for(int j = 0; j <= num[1]; ++j){
            for(int k = 0; k <= num[2]; ++k){
                for(int last = 0; last < 3; ++last){
                    if(dp[last][i][j][k] == inf) continue;
                    for(int nw = 0; nw < 3; ++nw){
                        if(last != nw && (nw != 0 || i < num[0]) && (nw != 1 || j < num[1]) && (nw != 2 || k < num[2])){
                            int oth = find_other(last, nw);
                            int pos_last = pos[last][(last == 0 ? i : (last == 1 ? j : k))];
                            int pos_nw = pos[nw][(nw == 0 ? i : (nw == 1 ? j : k)) + 1];
                            int pos_oth = pos[oth][(oth == 0 ? i : (oth == 1 ? j : k))];
                            int cost = count(last, pos_last, pos_nw) + count(oth, pos_oth, pos_nw);
                            minimize(dp[nw][i + (nw == 0)][j + (nw == 1)][k + (nw == 2)], dp[last][i][j][k] + cost);
                        }
                    }
                }
            }
        }
    }

    int ans = inf;
    for(int i = 0; i < 3; ++i){
        minimize(ans, dp[i][num[0]][num[1]][num[2]]);
    }

    cout << (ans == inf ? -1 : ans);
}

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

    file("task");

    int T = 1;
    // cin >> T;
    while(T--){
        testcase();
    }

    return 0;
}

Compilation message

joi2019_ho_t3.cpp: In function 'void testcase()':
joi2019_ho_t3.cpp:90:17: warning: array subscript has type 'char' [-Wchar-subscripts]
   90 |         pos[s[i]][++num[s[i]]] = i + 1;
      |                 ^
joi2019_ho_t3.cpp:90:29: warning: array subscript has type 'char' [-Wchar-subscripts]
   90 |         pos[s[i]][++num[s[i]]] = i + 1;
      |                             ^
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:9:55: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 | #define file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:130:5: note: in expansion of macro 'file'
  130 |     file("task");
      |     ^~~~
joi2019_ho_t3.cpp:9:88: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 | #define file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:130:5: note: in expansion of macro 'file'
  130 |     file("task");
      |     ^~~~
# Verdict Execution time Memory Grader output
1 Correct 319 ms 780372 KB Output is correct
2 Correct 336 ms 780372 KB Output is correct
3 Correct 376 ms 780368 KB Output is correct
4 Correct 367 ms 780368 KB Output is correct
5 Correct 387 ms 780216 KB Output is correct
6 Correct 364 ms 780628 KB Output is correct
7 Correct 381 ms 780208 KB Output is correct
8 Correct 361 ms 780372 KB Output is correct
9 Correct 365 ms 780368 KB Output is correct
10 Correct 360 ms 780372 KB Output is correct
11 Correct 368 ms 780344 KB Output is correct
12 Correct 364 ms 780292 KB Output is correct
13 Correct 360 ms 780300 KB Output is correct
14 Correct 365 ms 780372 KB Output is correct
15 Correct 354 ms 780372 KB Output is correct
16 Correct 317 ms 780220 KB Output is correct
17 Correct 354 ms 780372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 319 ms 780372 KB Output is correct
2 Correct 336 ms 780372 KB Output is correct
3 Correct 376 ms 780368 KB Output is correct
4 Correct 367 ms 780368 KB Output is correct
5 Correct 387 ms 780216 KB Output is correct
6 Correct 364 ms 780628 KB Output is correct
7 Correct 381 ms 780208 KB Output is correct
8 Correct 361 ms 780372 KB Output is correct
9 Correct 365 ms 780368 KB Output is correct
10 Correct 360 ms 780372 KB Output is correct
11 Correct 368 ms 780344 KB Output is correct
12 Correct 364 ms 780292 KB Output is correct
13 Correct 360 ms 780300 KB Output is correct
14 Correct 365 ms 780372 KB Output is correct
15 Correct 354 ms 780372 KB Output is correct
16 Correct 317 ms 780220 KB Output is correct
17 Correct 354 ms 780372 KB Output is correct
18 Correct 363 ms 780248 KB Output is correct
19 Correct 365 ms 780388 KB Output is correct
20 Correct 358 ms 780376 KB Output is correct
21 Correct 378 ms 780372 KB Output is correct
22 Correct 362 ms 780336 KB Output is correct
23 Correct 355 ms 780384 KB Output is correct
24 Correct 362 ms 780372 KB Output is correct
25 Correct 327 ms 780304 KB Output is correct
26 Correct 318 ms 780368 KB Output is correct
27 Correct 313 ms 780380 KB Output is correct
28 Correct 314 ms 780564 KB Output is correct
29 Correct 316 ms 780320 KB Output is correct
30 Correct 313 ms 780368 KB Output is correct
31 Correct 342 ms 780304 KB Output is correct
32 Correct 355 ms 780372 KB Output is correct
33 Correct 363 ms 780376 KB Output is correct
34 Correct 353 ms 780260 KB Output is correct
35 Correct 357 ms 780308 KB Output is correct
36 Correct 359 ms 780232 KB Output is correct
37 Correct 359 ms 780356 KB Output is correct
38 Correct 352 ms 780376 KB Output is correct
39 Correct 364 ms 780372 KB Output is correct
40 Correct 350 ms 780364 KB Output is correct
41 Correct 367 ms 780360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 369 ms 780436 KB Output is correct
2 Correct 375 ms 780628 KB Output is correct
3 Correct 365 ms 780260 KB Output is correct
4 Correct 379 ms 780368 KB Output is correct
5 Correct 364 ms 780248 KB Output is correct
6 Correct 363 ms 780324 KB Output is correct
7 Correct 351 ms 780372 KB Output is correct
8 Correct 359 ms 780192 KB Output is correct
9 Correct 366 ms 780372 KB Output is correct
10 Correct 370 ms 780372 KB Output is correct
11 Correct 378 ms 780340 KB Output is correct
12 Correct 350 ms 780372 KB Output is correct
13 Correct 359 ms 780248 KB Output is correct
14 Correct 349 ms 780256 KB Output is correct
15 Correct 363 ms 780320 KB Output is correct
16 Correct 352 ms 780404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 319 ms 780372 KB Output is correct
2 Correct 336 ms 780372 KB Output is correct
3 Correct 376 ms 780368 KB Output is correct
4 Correct 367 ms 780368 KB Output is correct
5 Correct 387 ms 780216 KB Output is correct
6 Correct 364 ms 780628 KB Output is correct
7 Correct 381 ms 780208 KB Output is correct
8 Correct 361 ms 780372 KB Output is correct
9 Correct 365 ms 780368 KB Output is correct
10 Correct 360 ms 780372 KB Output is correct
11 Correct 368 ms 780344 KB Output is correct
12 Correct 364 ms 780292 KB Output is correct
13 Correct 360 ms 780300 KB Output is correct
14 Correct 365 ms 780372 KB Output is correct
15 Correct 354 ms 780372 KB Output is correct
16 Correct 317 ms 780220 KB Output is correct
17 Correct 354 ms 780372 KB Output is correct
18 Correct 363 ms 780248 KB Output is correct
19 Correct 365 ms 780388 KB Output is correct
20 Correct 358 ms 780376 KB Output is correct
21 Correct 378 ms 780372 KB Output is correct
22 Correct 362 ms 780336 KB Output is correct
23 Correct 355 ms 780384 KB Output is correct
24 Correct 362 ms 780372 KB Output is correct
25 Correct 327 ms 780304 KB Output is correct
26 Correct 318 ms 780368 KB Output is correct
27 Correct 313 ms 780380 KB Output is correct
28 Correct 314 ms 780564 KB Output is correct
29 Correct 316 ms 780320 KB Output is correct
30 Correct 313 ms 780368 KB Output is correct
31 Correct 342 ms 780304 KB Output is correct
32 Correct 355 ms 780372 KB Output is correct
33 Correct 363 ms 780376 KB Output is correct
34 Correct 353 ms 780260 KB Output is correct
35 Correct 357 ms 780308 KB Output is correct
36 Correct 359 ms 780232 KB Output is correct
37 Correct 359 ms 780356 KB Output is correct
38 Correct 352 ms 780376 KB Output is correct
39 Correct 364 ms 780372 KB Output is correct
40 Correct 350 ms 780364 KB Output is correct
41 Correct 367 ms 780360 KB Output is correct
42 Correct 369 ms 780436 KB Output is correct
43 Correct 375 ms 780628 KB Output is correct
44 Correct 365 ms 780260 KB Output is correct
45 Correct 379 ms 780368 KB Output is correct
46 Correct 364 ms 780248 KB Output is correct
47 Correct 363 ms 780324 KB Output is correct
48 Correct 351 ms 780372 KB Output is correct
49 Correct 359 ms 780192 KB Output is correct
50 Correct 366 ms 780372 KB Output is correct
51 Correct 370 ms 780372 KB Output is correct
52 Correct 378 ms 780340 KB Output is correct
53 Correct 350 ms 780372 KB Output is correct
54 Correct 359 ms 780248 KB Output is correct
55 Correct 349 ms 780256 KB Output is correct
56 Correct 363 ms 780320 KB Output is correct
57 Correct 352 ms 780404 KB Output is correct
58 Correct 407 ms 780368 KB Output is correct
59 Correct 419 ms 780368 KB Output is correct
60 Correct 408 ms 780304 KB Output is correct
61 Correct 415 ms 780368 KB Output is correct
62 Correct 351 ms 780380 KB Output is correct
63 Correct 366 ms 780392 KB Output is correct
64 Correct 358 ms 780360 KB Output is correct
65 Correct 372 ms 780372 KB Output is correct
66 Correct 398 ms 780348 KB Output is correct
67 Correct 401 ms 780300 KB Output is correct
68 Correct 417 ms 780372 KB Output is correct
69 Correct 424 ms 780324 KB Output is correct
70 Correct 436 ms 780592 KB Output is correct
71 Correct 432 ms 780368 KB Output is correct
72 Correct 365 ms 780420 KB Output is correct
73 Correct 335 ms 780368 KB Output is correct