답안 #1087921

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1087921 2024-09-13T14:05:08 Z Zero_OP Growing Vegetable is Fun 3 (JOI19_ho_t3) C++14
20 / 100
500 ms 780516 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 testcase(){
    cin >> n >> s;
    for(int i = 0; i < n; ++i){
        int type = (s[i] == 'R' ? 0 : (s[i] == 'G' ? 1 : 2));
        pos[type][++num[type]] = 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;
                    //cout << dbg(last) << dbg(i) << dbg(j) << dbg(k) << '\n';
                    //current state dp : (last, i, j, k)
                    for(int nw = 0; nw < 3; ++nw){
                        //trans state dp : (nw, i + ?, j + ?, k + ?) + cost
                        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 '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:107:5: note: in expansion of macro 'file'
  107 |     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:107:5: note: in expansion of macro 'file'
  107 |     file("task");
      |     ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 349 ms 780364 KB Output is correct
2 Correct 352 ms 780368 KB Output is correct
3 Correct 350 ms 780364 KB Output is correct
4 Correct 352 ms 780328 KB Output is correct
5 Correct 365 ms 780344 KB Output is correct
6 Correct 367 ms 780336 KB Output is correct
7 Correct 401 ms 780368 KB Output is correct
8 Correct 356 ms 780332 KB Output is correct
9 Correct 359 ms 780360 KB Output is correct
10 Correct 363 ms 780204 KB Output is correct
11 Correct 367 ms 780372 KB Output is correct
12 Correct 365 ms 780368 KB Output is correct
13 Correct 368 ms 780372 KB Output is correct
14 Correct 375 ms 780372 KB Output is correct
15 Correct 359 ms 780320 KB Output is correct
16 Correct 362 ms 780372 KB Output is correct
17 Correct 391 ms 780368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 349 ms 780364 KB Output is correct
2 Correct 352 ms 780368 KB Output is correct
3 Correct 350 ms 780364 KB Output is correct
4 Correct 352 ms 780328 KB Output is correct
5 Correct 365 ms 780344 KB Output is correct
6 Correct 367 ms 780336 KB Output is correct
7 Correct 401 ms 780368 KB Output is correct
8 Correct 356 ms 780332 KB Output is correct
9 Correct 359 ms 780360 KB Output is correct
10 Correct 363 ms 780204 KB Output is correct
11 Correct 367 ms 780372 KB Output is correct
12 Correct 365 ms 780368 KB Output is correct
13 Correct 368 ms 780372 KB Output is correct
14 Correct 375 ms 780372 KB Output is correct
15 Correct 359 ms 780320 KB Output is correct
16 Correct 362 ms 780372 KB Output is correct
17 Correct 391 ms 780368 KB Output is correct
18 Correct 353 ms 780368 KB Output is correct
19 Correct 360 ms 780372 KB Output is correct
20 Correct 361 ms 780368 KB Output is correct
21 Correct 351 ms 780356 KB Output is correct
22 Correct 364 ms 780372 KB Output is correct
23 Correct 344 ms 780332 KB Output is correct
24 Correct 353 ms 780372 KB Output is correct
25 Correct 363 ms 780368 KB Output is correct
26 Correct 366 ms 780368 KB Output is correct
27 Correct 344 ms 780212 KB Output is correct
28 Correct 359 ms 780372 KB Output is correct
29 Correct 352 ms 780368 KB Output is correct
30 Correct 383 ms 780236 KB Output is correct
31 Correct 364 ms 780372 KB Output is correct
32 Correct 354 ms 780224 KB Output is correct
33 Correct 357 ms 780372 KB Output is correct
34 Correct 364 ms 780376 KB Output is correct
35 Correct 340 ms 780372 KB Output is correct
36 Correct 348 ms 780372 KB Output is correct
37 Correct 355 ms 780368 KB Output is correct
38 Correct 353 ms 780196 KB Output is correct
39 Correct 346 ms 780416 KB Output is correct
40 Correct 357 ms 780484 KB Output is correct
41 Execution timed out 521 ms 780372 KB Time limit exceeded
42 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 316 ms 780208 KB Output is correct
2 Correct 325 ms 780400 KB Output is correct
3 Correct 334 ms 780376 KB Output is correct
4 Correct 353 ms 780432 KB Output is correct
5 Correct 361 ms 780372 KB Output is correct
6 Correct 336 ms 780412 KB Output is correct
7 Correct 347 ms 780240 KB Output is correct
8 Correct 343 ms 780372 KB Output is correct
9 Correct 347 ms 780372 KB Output is correct
10 Correct 347 ms 780240 KB Output is correct
11 Correct 349 ms 780372 KB Output is correct
12 Correct 358 ms 780372 KB Output is correct
13 Correct 344 ms 780516 KB Output is correct
14 Correct 347 ms 780368 KB Output is correct
15 Correct 355 ms 780244 KB Output is correct
16 Correct 358 ms 780264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 349 ms 780364 KB Output is correct
2 Correct 352 ms 780368 KB Output is correct
3 Correct 350 ms 780364 KB Output is correct
4 Correct 352 ms 780328 KB Output is correct
5 Correct 365 ms 780344 KB Output is correct
6 Correct 367 ms 780336 KB Output is correct
7 Correct 401 ms 780368 KB Output is correct
8 Correct 356 ms 780332 KB Output is correct
9 Correct 359 ms 780360 KB Output is correct
10 Correct 363 ms 780204 KB Output is correct
11 Correct 367 ms 780372 KB Output is correct
12 Correct 365 ms 780368 KB Output is correct
13 Correct 368 ms 780372 KB Output is correct
14 Correct 375 ms 780372 KB Output is correct
15 Correct 359 ms 780320 KB Output is correct
16 Correct 362 ms 780372 KB Output is correct
17 Correct 391 ms 780368 KB Output is correct
18 Correct 353 ms 780368 KB Output is correct
19 Correct 360 ms 780372 KB Output is correct
20 Correct 361 ms 780368 KB Output is correct
21 Correct 351 ms 780356 KB Output is correct
22 Correct 364 ms 780372 KB Output is correct
23 Correct 344 ms 780332 KB Output is correct
24 Correct 353 ms 780372 KB Output is correct
25 Correct 363 ms 780368 KB Output is correct
26 Correct 366 ms 780368 KB Output is correct
27 Correct 344 ms 780212 KB Output is correct
28 Correct 359 ms 780372 KB Output is correct
29 Correct 352 ms 780368 KB Output is correct
30 Correct 383 ms 780236 KB Output is correct
31 Correct 364 ms 780372 KB Output is correct
32 Correct 354 ms 780224 KB Output is correct
33 Correct 357 ms 780372 KB Output is correct
34 Correct 364 ms 780376 KB Output is correct
35 Correct 340 ms 780372 KB Output is correct
36 Correct 348 ms 780372 KB Output is correct
37 Correct 355 ms 780368 KB Output is correct
38 Correct 353 ms 780196 KB Output is correct
39 Correct 346 ms 780416 KB Output is correct
40 Correct 357 ms 780484 KB Output is correct
41 Execution timed out 521 ms 780372 KB Time limit exceeded
42 Halted 0 ms 0 KB -