QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#666407 | #1281. Longest Common Subsequence | IBory | WA | 192ms | 24232kb | C++17 | 2.5kb | 2024-10-22 18:17:39 | 2024-10-22 18:17:47 |
Judging History
answer
#include <bits/stdc++.h>
#define pii pair<ll, ll>
using namespace std;
const int SZ = 1 << 20;
int A[SZ], B[SZ], SA[SZ], SB[SZ];
struct Seg {
int T[SZ << 1];
Seg() {
memset(T, 0xf3, sizeof(T));
}
void Update(int i, int v, bool set = 0) {
i += SZ - 1;
T[i] = max(T[i], v);
if (set) T[i] = v;
while (i >>= 1) T[i] = max(T[i * 2], T[i * 2 + 1]);
}
int Query(int L, int R, int sL = 1, int sR = SZ, int n = 1) {
if (R < sL || sR < L) return -1E9;
if (L <= sL && sR <= R) return T[n];
int mid = (sL + sR) >> 1;
return max(Query(L, R, sL, mid, n * 2), Query(L, R, mid + 1, sR, n * 2 + 1));
}
} TL, TR;
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int _T;
cin >> _T;
while (_T--) {
int N, M;
cin >> N >> M;
vector<int> P1[2], P3[2];
for (int i = 1; i <= N; ++i) {
cin >> A[i];
if (A[i] == 1) P1[0].push_back(i);
SA[i] = SA[i - 1] + (A[i] == 2);
if (A[i] == 3) P3[0].push_back(i);
}
for (int i = 1; i <= M; ++i) {
cin >> B[i];
if (B[i] == 1) P1[1].push_back(i);
SB[i] = SB[i - 1] + (B[i] == 2);
if (B[i] == 3) P3[1].push_back(i);
}
int ans = 0;
ans = max(ans, (int)min(P1[0].size(), P1[1].size()));
ans = max(ans, min(SA[N], SB[M]));
ans = max(ans, (int)min(P3[0].size(), P3[1].size()));
vector<tuple<int, int, int>> P;
for (int i = 0; i < min(P1[0].size(), P1[1].size()); ++i) {
P.emplace_back(P1[0][i], P1[1][i], 1);
}
for (int i = 0; i < min(P3[0].size(), P3[1].size()); ++i) {
P.emplace_back(P3[0][P3[0].size() - 1 - i], P3[1][P3[1].size() - 1 - i], 3);
}
sort(P.begin(), P.end());
P.emplace_back(N + 1, M + 1, 3);
vector<int> U = {-(1 << 30)};
for (auto [a, b, n] : P) {
if (n == 1) U.push_back(SB[b] - SA[a]);
else U.push_back(SB[b - 1] - SA[a - 1]);
}
sort(U.begin(), U.end());
U.erase(unique(U.begin(), U.end()), U.end());
int c1 = 0, c3 = min(P3[0].size(), P3[1].size());
for (auto [a, b, n] : P) {
// cout << a << ' ' << b << ' ' << n << '\n';
int t = (n == 1 ? SB[b] - SA[a] : SB[b - 1] - SA[a - 1]);
int tp = lower_bound(U.begin(), U.end(), t) - U.begin();
if (n == 1) {
c1++;
TL.Update(tp, c1 - SA[a]);
TR.Update(tp, c1 - SB[b]);
}
else {
ans = max(ans, TL.Query(1, tp) + SA[a - 1] + c3);
ans = max(ans, TR.Query(tp, SZ) + SB[b - 1] + c3);
c3--;
}
}
cout << ans << '\n';
for (int i = 1; i <= U.size(); ++i) {
TL.Update(i, -1e9, 1);
TR.Update(i, -1e9, 1);
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 24232kb
input:
3 3 3 1 2 3 1 2 3 3 3 1 1 1 1 1 2 3 3 1 3 2 1 2 3
output:
3 2 2
result:
ok 3 tokens
Test #2:
score: -100
Wrong Answer
time: 192ms
memory: 24104kb
input:
139889 1 10 2 2 1 2 2 3 1 1 2 3 3 7 1 3 2 3 3 1 1 2 2 6 1 2 1 3 1 1 1 1 8 7 3 1 3 3 2 2 3 1 3 2 2 1 3 3 3 10 3 3 2 1 1 2 2 1 1 1 1 3 1 1 5 2 1 2 1 3 1 1 2 1 4 1 3 3 3 3 7 2 3 1 2 1 2 2 3 3 2 6 2 3 1 1 2 1 3 1 3 1 4 1 3 1 1 3 4 2 2 3 2 3 1 3 1 8 1 1 1 3 1 3 3 3 1 4 1 1 3 3 3 1 10 10 3 1 2 1 2 2 2 2 1...
output:
1 1 1 4 2 2 0 1 2 1 1 1 1 5 1 0 1 2 2 2 4 4 1 1 1 0 1 2 3 4 4 1 1 2 1 1 1 1 3 1 1 1 3 1 1 2 3 2 1 3 1 4 1 2 2 1 3 1 1 2 2 1 2 4 4 2 2 1 1 1 2 4 2 2 1 1 2 2 1 2 2 1 5 2 4 1 1 3 3 2 5 2 1 3 1 3 1 2 1 3 2 5 3 2 0 1 1 4 2 3 2 2 0 0 2 3 1 2 0 4 1 4 1 1 2 1 1 0 1 1 1 1 3 2 2 1 3 3 3 1 2 2 3 2 1 1 3 4 2 3 ...
result:
wrong answer 14th words differ - expected: '6', found: '5'