| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 590001 | boris_mihov | Growing Vegetable is Fun 3 (JOI19_ho_t3) | C++14 | 1004 ms | 108724 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <algorithm>
#include <iostream>
#include <vector>
typedef long long llong;
const int MAXN = 400 + 10;
const int INF = 1e9;
int n;
char s[MAXN];
int dp[MAXN][MAXN][MAXN][4];
bool bl[MAXN][MAXN][MAXN][4];
int byPos[4][MAXN];
int len[4];
int f(int ones, int twos, int threes, int last)
{
if (ones + twos + threes == n) return 0;
if (bl[ones][twos][threes][last]) return dp[ones][twos][threes][last];
bl[ones][twos][threes][last] = true;
dp[ones][twos][threes][last] = INF;
if (last != 1 && ones != len[1])
{
int add = 0;
for (int i = 0 ; i < twos ; ++i)
{
if (byPos[2][i] > byPos[1][ones]) add++;
}
for (int i = 0 ; i < threes ; ++i)
{
if (byPos[3][i] > byPos[1][ones]) add++;
}
dp[ones][twos][threes][last] = std::min(dp[ones][twos][threes][last], f(ones + 1, twos, threes, 1) + add + byPos[1][ones] - (ones + twos + threes + 1));
}
if (last != 2 && twos != len[2])
{
int add = 0;
for (int i = 0 ; i < ones ; ++i)
{
if (byPos[1][i] > byPos[2][twos]) add++;
}
for (int i = 0 ; i < threes ; ++i)
{
if (byPos[3][i] > byPos[2][twos]) add++;
}
dp[ones][twos][threes][last] = std::min(dp[ones][twos][threes][last], f(ones, twos + 1, threes, 2) + add + byPos[2][twos] - (ones + twos + threes + 1));
}
if (last != 3 && threes != len[3])
{
int add = 0;
for (int i = 0 ; i < ones ; ++i)
{
if (byPos[1][i] > byPos[3][threes]) add++;
}
for (int i = 0 ; i < twos ; ++i)
{
if (byPos[2][i] > byPos[3][threes]) add++;
}
dp[ones][twos][threes][last] = std::min(dp[ones][twos][threes][last], f(ones, twos, threes + 1, 3) + add + byPos[3][threes] - (ones + twos + threes + 1));
}
return dp[ones][twos][threes][last];
}
void solve()
{
for (int i = 1 ; i <= n ; ++i)
{
if (s[i] == 'R') s[i] = '1';
if (s[i] == 'G') s[i] = '2';
if (s[i] == 'Y') s[i] = '3';
byPos[s[i]-'0'][len[s[i]-'0']++] = i;
}
int result = f(0, 0, 0, 0);
if (result == INF) result = -1;
std::cout << result << '\n';
}
void read()
{
std::cin >> n;
std::cin >> s + 1;
}
void fastIO()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main ()
{
fastIO();
read();
solve();
return 0;
}컴파일 시 표준 에러 (stderr) 메시지
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
