QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#673632 | #7743. Grand Finale | tumu1t | RE | 0ms | 0kb | C++20 | 4.2kb | 2024-10-25 03:11:13 | 2024-10-25 03:11:13 |
answer
#include <bits/stdc++.h>
typedef long long LL;
using std::cin, std::cout, std::endl, std::vector, std::pair;
std::map<char, int> Card{{'Q', 1}, {'B', 2}, {'G', 0}, {'W', 0}};
const int INF = 100'000;
void Solve()
{
int n, m;
cin >> n >> m;
vector<vector<int>> Dp1(m + 1, vector<int>((n + m + 3) / 2, -1));
vector<vector<char>> Dp2(m + 1, vector<char>(n + m + 1));
std::string H, D;
cin >> H >> D;
D.insert(0, "0");
vector<vector<int>> Pref(3, vector<int>(m + 1));
for (auto i : H)
{
Pref[Card[i]][0] += 1;
}
for (int i = 1; i <= m; i++)
{
for (int j = 0; j < 3; j++)
Pref[j][i] = Pref[j][i - 1];
Pref[Card[D[i]]][i] += 1;
}
auto CheckOK = [&](int Drawn, int Backflip) -> bool
{
return (m - Drawn <= Backflip * 2);
};
auto Dfs1 = [&](auto self, int Drawn, int Backflip) -> int
{
if (CheckOK(Drawn, Backflip))
{
if (Drawn > m || Backflip < 0)
;
else
Dp1[Drawn][Backflip] = 0;
return 0;
}
if (Drawn > m || Backflip < 0)
return INF;
if (Drawn == m)
{
Dp1[Drawn][Backflip] = 0;
return 0;
}
if (Dp1[Drawn][Backflip] != -1)
return Dp1[Drawn][Backflip];
auto &at = Dp1[Drawn][Backflip];
at = INF;
if (D[Drawn + 1] == 'Q')
at = std::min<int>(at, std::max<int>(1, self(self, Drawn + 1, Backflip)));
if (D[Drawn + 1] == 'B')
at = std::min<int>(at, std::max<int>(1, self(self, Drawn + 1, Backflip + 1) + 1));
if (D[Drawn + 1] == 'W')
at = std::min<int>(at, std::max<int>(1, self(self, Drawn + 1, Backflip) + 1));
if (Backflip != 0)
{
if (D[Drawn + 1] == 'Q')
at = std::min<int>(at, std::max<int>(self(self, Drawn + 2, Backflip - 1) - 1, 0));
if (D[Drawn + 1] == 'B')
at = std::min<int>(at, self(self, Drawn + 2, Backflip));
if (D[Drawn + 1] == 'W')
at = std::min<int>(at, self(self, Drawn + 2, Backflip - 1));
}
return at;
};
for (int d = 0; d <= m; d++)
{
for (int b = 0; b <= n + m; b++)
Dfs1(Dfs1, d, b);
}
auto Dfs2 = [&](auto self, int Drawn, int Backflip) -> void
{
if (Drawn > m || Backflip < 0)
return;
if (Dp2[Drawn][Backflip] != 0)
return;
Dp2[Drawn][Backflip] = 1;
int BackflipUsed = Pref[2][Drawn] - Backflip;
int QuickslapUsed = Drawn - 2 * BackflipUsed;
int QuickslapInHand = Pref[1][Drawn] - QuickslapUsed;
if (QuickslapInHand < 0)
{
Dp2[Drawn][Backflip] = 2;
return;
}
if (QuickslapInHand > 0)
{
if (D[Drawn + 1] == 'B')
self(self, Drawn + 1, Backflip + 1);
else
self(self, Drawn + 1, Backflip);
}
if (Backflip > 0)
{
int tmp = (D[Drawn + 1] == 'B' ? 1 : 0) + (D[Drawn + 2] == 'B' ? 1 : 0);
self(self, Drawn + 2, Backflip - 1 + tmp);
}
};
Dfs2(Dfs2, 0, Pref[2][0]);
for (int ans = n; ans <= n + m; ans++)
{
for (int d = 0; d <= m; d++)
{
int BackflipUsed = ans - n;
int BackflipInHand = Pref[2][d] - BackflipUsed;
int QuickslapUsed = d - 2 * BackflipUsed;
int QuickslapInHand = Pref[1][d] - QuickslapUsed;
if (QuickslapUsed >= 0 && BackflipInHand >= 0 && QuickslapInHand >= 0 && Dp2[d][BackflipInHand] == 1 && Dp1[d][BackflipInHand] <= QuickslapInHand)
{
cout << ans << endl;
return;
}
}
}
cout << "IMPOSSIBLE" << endl;
return;
}
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int testcases = 1;
cin >> testcases;
for (int i = 1; i <= testcases; i++)
{
Solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
2 2 6 BG BQWBWW 4 6 GQBW WWWWQB