QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#617712 | #7743. Grand Finale | REN5511 | TL | 0ms | 3832kb | C++14 | 4.2kb | 2024-10-06 16:44:40 | 2024-10-06 16:44:41 |
Judging History
answer
#include <iostream>
#include <unordered_map>
#include <set>
using namespace std;
typedef long long ll;
int n, m;
int iniq[2500 + 100] = {0}, inib[2500 + 100] = { 0 };
char hand[2500 + 100], library[2500 + 100];
set<pair<int, pair<int, int>>>mp[2500 + 100];
//抽一 抽二 伤口
bool check(int mid) {
for (int i = 0; i <= m; i++) {
mp[i].clear();
}
int q=0, b=0, w=0;
for (int i = 1; i <= n; i++) {
if (hand[i] == 'Q') {
q++;
}
else 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 (iniq[i + d1] - iniq[i] + 2 * (inib[i + d1] - inib[i]) + d1 + d2 * 2 + i > m) {
return 1;
}
}
}
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+1 + 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 } });
if (iniq[i + d1] - iniq[i] + 2 * (inib[i + d1] - inib[i]) + d1 + d2 * 2 + i > m) {
return 1;
}
}
else {
if (library[i-1] == 'Q') {
d1++;
}
else if (library[i-1] == 'B') {
d2++;
}
else {
w++;
}
mp[i].insert({ d1, { d2, w } });
if (iniq[i+d1]-iniq[i]+ 2*(inib[i + d1] - inib[i]) +d1 + d2 * 2 + i > m) {
return 1;
}
}
}
}
}
}
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() {
cin >> n >> m;
fill(inib, inib + m + 1, 0);
fill(iniq, iniq + m + 1, 0);
for (int i = 1; i <= n; i++) {
cin >> hand[i];
}
for (int i = 1; i <= m; i++) {
cin >> library[i];
iniq[i] += iniq[i - 1];
inib[i] += inib[i - 1];
if (library[i] == 'Q') {
iniq[i]++;
}
else if (library[i] == 'B') {
inib[i]++;
}
}
int ans = findans(n, m);
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;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3740kb
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: 3832kb
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...