답안 #109477

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
109477 2019-05-06T16:42:00 Z wasyl Coin Collecting (JOI19_ho_t4) C++11
0 / 100
3 ms 384 KB
#pragma GCC optimize("O3")
/*{{{*/
#include <bits/stdc++.h>
#ifdef LOCAL
int c_ = 0;
#define cout (c_?cerr:cout)
#define debug(...) \
{if(!c_++)cerr<<"\033[96;1m"; \
__VA_ARGS__; \
if(!--c_)cerr<<"\033[0m";}
#else
#define debug(...) {}
#endif
#define assrt(...) debug(\
if (not (__VA_ARGS__)) \
    exit((cerr << __LINE__ << ": " << #__VA_ARGS__ << '\n', 0));\
)
#define st first
#define nd second
#define all(...) (__VA_ARGS__).begin(), (__VA_ARGS__).end()
using namespace std; using ll = long long;
template < typename t > using V = vector< t >;
template < typename t, size_t s > using A = array< t, s >;
void print() {cout << '\n';}
template< typename t, typename... v >
void print(const t& a, const v&... b)
{cout << a << (sizeof...(b)?" ":""); print(b...);}
template< typename t > void print_range(t a, const t& b)
{ while (a != b) cout << (*a++) << ' '; cout << '\n';}
#define dump(...) debug(print(#__VA_ARGS__,'=',__VA_ARGS__))
#define dump_range(...) debug(cerr<<#__VA_ARGS__ << ": "; print_range(__VA_ARGS__))
/*}}}*/

using Triple = array< int, 3 >;
constexpr int nax = 400, ax = 200, cx = 3, inf = 1e9;

int n, mem[3][ax + 1][ax + 1][ax + 1], poz[cx][ax + 1], sum[cx][nax + 1];
string s;

int& dp(const int v, const Triple& s)
{
    return mem[v][s[0]][s[1]][s[2]];
}

void solve()
{
    for (int i = 0; i <= sum[0][n]; ++i)
        for (int j = 0; j <= sum[1][n]; ++j)
            for (int k = 0; k <= sum[2][n]; ++k)
                for (int l = 0; l < cx; ++l)
                {
                    Triple s{i, j, k};
                    for (int i = 0; i < cx; ++i)
                        if (s[i] < sum[i][n] and i != l)
                        {
                            int p = poz[i][s[i] + 1];
                            int d = p;
                            for (int j = 0; j < cx; ++j)
                                d -= min(sum[j][p], s[j] + (i == j));
                            int& a = dp(l, s);
                            ++s[i];
                            int& b = dp(i, s);
                            --s[i];
                            b = min(b, a + d);
                        }
                }
}

int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> s;

    {
        int su[3]{};
        for (int i = 1; i <= n; ++i)
        {
            for (int j = 0; j < cx; ++j)
                sum[j][i] = sum[j][i - 1];
            if (s[i - 1] == 'R')
            {
                if (sum[0][i] == ax) return print(-1), 0;
                ++sum[0][i], poz[0][sum[0][i]] = i;
            }
            if (s[i - 1] == 'Y')
            {
                if (sum[1][i] == ax) return print(-1), 0;
                ++sum[1][i], poz[1][sum[1][i]] = i;
            }
            if (s[i - 1] == 'G')
            {
                if (sum[2][i] == ax) return print(-1), 0;
                ++sum[2][i], poz[2][sum[2][i]] = i;
            }
        }
    }

    for (int i = 0; i <= sum[0][n]; ++i)
        for (int j = 0; j <= sum[1][n]; ++j)
            for (int k = 0; k <= sum[2][n]; ++k)
                for (int l = 0; l < cx; ++l)
                    mem[l][i][j][k] = inf;

    mem[0][0][0][0] = mem[1][0][0][0] = mem[2][0][0][0] = 0;

    solve();
    
    int res = inf;
    for (int i = 0; i < cx; ++i)
        res = min(res, mem[i][sum[0][n]][sum[1][n]][sum[2][n]]);

    print((res == inf? -1 : res));
}

Compilation message

joi2019_ho_t4.cpp: In function 'int32_t main()':
joi2019_ho_t4.cpp:77:13: warning: unused variable 'su' [-Wunused-variable]
         int su[3]{};
             ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Incorrect 2 ms 384 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Incorrect 2 ms 384 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 384 KB Output is correct
2 Incorrect 2 ms 384 KB Output isn't correct
3 Halted 0 ms 0 KB -