제출 #1338384

#제출 시각아이디문제언어결과실행 시간메모리
1338384adscodingGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++20
100 / 100
37 ms3088 KiB
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define FOR(i, a, b) for (int i = a, _b = b; i <= _b; ++i)
#define FORD(i, a, b) for (int i = a, _b = b; i >= _b; --i)
#define FORLL(i, a, b) for (ll i = a, _b = b; i <= _b; ++i)
#define FORDLL(i, a, b) for (ll i = a, _b = b; i >= _b; --i)
#define all(x) x.begin(), x.end()
#define uni(x) sort(all(x)), x.erase(unique(all(x)), x.end())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__)

template<typename T>
void __print_one(const char *&s, const T &x)
{
    while (*s == ' ') ++s;
    const char *p = s;
    int bal = 0;
    while (*s)
    {
        if (*s == '(') ++bal;
        else if (*s == ')') --bal;
        else if (*s == ',' && bal == 0) break;
        ++s;
    }
    cerr.write(p, s - p) << " = " << x;
    if (*s == ',')
    {
        ++s;
        cerr << "  ,  ";
    }
}

template<typename... Args>
void debug(const char *s, Args... args)
{
    cerr << "[  ";
    int dummy[] = { 0 , ( __print_one(s, args) , 0 )... };
    (void)dummy;
    cerr << "  ]\n\n";
}

template<class X>
bool maximize(X &a, X b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}

template<class X>
bool minimize(X &a, X b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}

// --------------------------------------------------------------------------------------------

const int maxn = 405;
int n, f[maxn][maxn][3], dp[maxn][maxn][3], a[maxn], CNT[3][maxn], POS[3][maxn], sl[3];

// --------------------------------------------------------------------------------------------





void solve()
{
    cin >> n;
    FOR(i, 1, n)
    {
        char ch; cin >> ch;
        if (ch == 'R') a[i] = 0;
        else if (ch == 'G') a[i] = 1;
        else a[i] = 2;
    }

    FOR(type, 0, 2)
        FORD(i, n, 1)
            CNT[type][i] = (a[i] == type) + CNT[type][i + 1];

    FOR(type, 0, 2)
    {
        FOR(i, 1, n)
        {
            if (a[i] == type)
            {
                ++sl[type];
                POS[type][sl[type]] = i;
            }
        }
        POS[type][sl[type] + 1] = n + 1;
    }



    FOR(lst, 0, 2)
        dp[0][0][lst] = 0;

    FOR(i, 1, n)
    {
        FOR(zero, 0, sl[0])
            for (int one = 0; zero + one <= i; ++one)
                FOR(lst, 0, 2)
                {
                    f[zero][one][lst] = dp[zero][one][lst];
                    dp[zero][one][lst] = 1e9;
                }

        FOR(zero, 0, sl[0])
            for (int one = 0; zero + one <= i && one <= sl[1]; ++one)
            {
                int two = i - zero - one;
                if (two > sl[2]) continue;

                FOR(lst, 0, 2)
                {
                    if (lst == 0 && zero >= 1)
                    {
                        int pos = POS[0][zero] + max(0, CNT[1][POS[0][zero]] - CNT[1][POS[1][one] + 1])
                                               + max(0, CNT[2][POS[0][zero]] - CNT[2][POS[2][two] + 1]);
                        FOR(preLst, 0, 2)
                        {
                            if (lst == preLst) continue;
                            minimize(dp[zero][one][lst], f[zero - 1][one][preLst] + pos - i);
                        }

                    }
                    else if (lst == 1 && one >= 1)
                    {
                        int pos = POS[1][one] + max(0, CNT[0][POS[1][one]] - CNT[0][POS[0][zero] + 1])
                                              + max(0, CNT[2][POS[1][one]] - CNT[2][POS[2][two] + 1]);
                        FOR(preLst, 0, 2)
                        {
                            if (lst == preLst) continue;
                            minimize(dp[zero][one][lst], f[zero][one - 1][preLst] + pos - i);
                        }
                    }
                    else if (lst == 2 && two >= 1)
                    {
                        int pos = POS[2][two] + max(0, CNT[0][POS[2][two]] - CNT[0][POS[0][zero] + 1])
                                              + max(0, CNT[1][POS[2][two]] - CNT[1][POS[1][one] + 1]);
                        FOR(preLst, 0, 2)
                        {
                            if (lst == preLst) continue;
                            minimize(dp[zero][one][lst], f[zero][one][preLst] + pos - i);
                        }
                    }
                }
            }
    }

    int res = min({dp[sl[0]][sl[1]][0], dp[sl[0]][sl[1]][1], dp[sl[0]][sl[1]][2]});
    cout << (res >= int(1e9) ? -1 : res);
}

signed main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    #define TASK "TEST"
    if (fopen(TASK".INP", "r"))
    {
        freopen(TASK".INP", "r", stdin);
        freopen(TASK".OUT", "w", stdout);
    }
    solve();
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:178:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  178 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:179:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  179 |         freopen(TASK".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...