QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#617369 | #7743. Grand Finale | REN5511 | TL | 0ms | 3768kb | C++14 | 3.5kb | 2024-10-06 15:10:16 | 2024-10-06 15:10:17 |
Judging History
answer
#include <iostream>
#include <unordered_map>
#include <set>
using namespace std;
typedef long long ll;
int n, m;
int iniq = 0, inib = 0;
char hand[2500 + 100], library[2500 + 100];
unordered_map<int, set<pair<int, pair<int, int>>>>mp;
//抽一 抽二 伤口
bool check(int mid) {
mp.clear();
int q=0, b=0, w=0;
for (int i = 1; i <= n; i++) {
if (hand[i] == 'Q') {
q++;
}
if (hand[i] == 'B') {
b++;
}
else {
w++;
}
}
mp[0].insert({q, { b, w }});
for (int i = 1; i <= m; i++) {
if (mp[m].size())return 1;
for (auto h : mp[i - 1]) {
int d1 = h.first, d2 = h.second.first, w = h.second.second;
if (d1 > 0) {
d1--;
if (library[i] == 'Q') {
d1++;
}
else if (library[i] == 'B') {
d2++;
}
else {
w++;
}
mp[i].insert({ d1, { d2, w } });
}
}
if (i > 1) {
for (auto h : mp[i - 2]) {
int d1 = h.first, d2 = h.second.first, w = h.second.second;
if (d2 > 0) {
d2--;
if (d1 + d2 + w < mid) {
if (library[i-1] == 'Q') {
d1++;
}
else if (library[i-1] == 'B') {
d2++;
}
else {
w++;
}
if (library[i] == 'Q') {
d1++;
}
else if (library[i] == 'B') {
d2++;
}
else {
w++;
}
mp[i].insert({ d1, { d2, w } });
}
else {
if (library[i-1] == 'Q') {
d1++;
}
else if (library[i-1] == 'B') {
d2++;
}
else {
w++;
}
mp[i].insert({ d1, { d2, w } });
}
}
}
}
}
if (mp[m].size())return 1;
return 0;
}
int findans(int l, int r) {
int ans = -1;
while (l <= r) {
int mid = (l + r) / 2;
if (check(mid)) {
ans = mid;
r = mid - 1;
}
else {
l = mid + 1;
}
}
return ans;
}
void solve() {
iniq = 0; inib = 0;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> hand[i];
if (hand[i] == 'Q') {
iniq++;
}
if (hand[i] == 'B') {
inib++;
}
}
for (int i = 1; i <= m; i++) {
cin >> library[i];
}
int ans = findans(n, 3000);
if (ans == -1) {
cout << "IMPOSSIBLE" << "\n";
}
else {
cout << ans << "\n";
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3768kb
input:
2 2 6 BG BQWBWW 4 6 GQBW WWWWQB
output:
3 IMPOSSIBLE
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 3524kb
input:
2 3 8 QBG BBBWBWWW 3 8 QBG BBBWBWWW
output:
3 3
result:
ok 2 lines
Test #3:
score: -100
Time Limit Exceeded
input:
13 184 887 WBQBBWWBQBQBQBWWBBQQWWQQQBBBQWWWQWBBBBWWWQQBQQQWQBBQQWQQBBWWQWQQBWBQWWWWQWQQWQBWWQQWWQQBWWQWBBBWWQWBQBQWQQWWBQBQQBWQBQBWWBWQWQWBWBQWWQBQQQBWQQWQWWBQBWWQQBQWBQQBQQBQBBQBWBQQWWQBWBBQQBQG QWBQBQBWBQQWWWWQBBBQQQBBBWWWWQWQWWQQQBQBWQQQBWQWQBWWBQQQWQWBQBBQBWBBWBQWQBWWQQBWQQWWQWWQQWBQQWQWBBQBWBQQ...