Submission #1295677

#TimeUsernameProblemLanguageResultExecution timeMemory
1295677baotoan655Growing Vegetable is Fun 3 (JOI19_ho_t3)C++20
100 / 100
62 ms163340 KiB
#include <bits/stdc++.h>
#define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
using namespace std;
const int N = 4e2 + 5;
const int INF = 1e9;

int n, f[3], mpx[300], dp[N][N][N][3];
vector <pair <int, pair<int, int>>> pos[3];
string s;
inline int calc(pair <int, pair<int, int>> x, pair <int, pair<int, int>> y) {
    return abs(x.first - y.first) + abs(x.second.first - y.second.first) + abs(x.second.second - y.second.second);
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    file("A") else file("task");
    mpx['R'] = 0;
    mpx['G'] = 1;
    mpx['Y'] = 2;
    cin >> n >> s;
    s = ' ' + s;
    for (int i = 1 ; i <= n ; i++) {
        f[mpx[s[i]]]++;
        pos[mpx[s[i]]].push_back({f[0], {f[1], f[2]}});
    }
    for (int r = 0 ; r <= f[0] ; r++) {
        for (int g = 0 ; g <= f[1] ; g++) {
            for (int y = 0 ; y <= f[2] ; y++) {
                if (r + g + y == 0) continue;
                for (int i = 0 ; i < 3 ; i++) dp[r][g][y][i] = INF;
                if (r > 0) dp[r][g][y][0] = min(dp[r - 1][g][y][1], dp[r - 1][g][y][2]) + calc(pos[0][r - 1], {r, {g, y}});
                if (g > 0) dp[r][g][y][1] = min(dp[r][g - 1][y][0], dp[r][g - 1][y][2]) + calc(pos[1][g - 1], {r, {g, y}});
                if (y > 0) dp[r][g][y][2] = min(dp[r][g][y - 1][0], dp[r][g][y - 1][1]) + calc(pos[2][y - 1], {r, {g, y}});
            }
        }
    }
    int ans = INF;
    for (int i = 0 ; i < 3 ; i++) ans = min(ans, dp[f[0]][f[1]][f[2]][i]);
    if (ans == INF) cout << "-1\n";
    else cout << ans / 2 << '\n';
    return 0;
}

Compilation message (stderr)

joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:2:58: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    2 | #define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:17:5: note: in expansion of macro 'file'
   17 |     file("A") else file("task");
      |     ^~~~
joi2019_ho_t3.cpp:2:91: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    2 | #define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:17:5: note: in expansion of macro 'file'
   17 |     file("A") else file("task");
      |     ^~~~
joi2019_ho_t3.cpp:2:58: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    2 | #define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:17:20: note: in expansion of macro 'file'
   17 |     file("A") else file("task");
      |                    ^~~~
joi2019_ho_t3.cpp:2:91: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    2 | #define file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:17:20: note: in expansion of macro 'file'
   17 |     file("A") else file("task");
      |                    ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...