QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#667654 | #1281. Longest Common Subsequence | IBory | WA | 161ms | 24708kb | C++20 | 3.2kb | 2024-10-23 01:30:46 | 2024-10-23 01:30:51 |
Judging History
answer
#include <bits/stdc++.h>
#define pii pair<int, int>
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 Solve(int N, int M, int a, int b) {
vector<int> PA(N + 2), PB(M + 2);
for (int i = N; i > 0; --i) PA[i] = PA[i + 1] + (A[i] == b);
for (int i = M; i > 0; --i) PB[i] = PB[i + 1] + (B[i] == b);
vector<int> OA, OB;
for (int i = 1; i <= N; ++i) if (A[i] == a) OA.push_back(i);
for (int i = 1; i <= M; ++i) if (B[i] == a) OB.push_back(i);
int ret = min(PA[1], PB[1]);
for (int i = 0; i < min(OA.size(), OB.size()); ++i) {
int n1 = OA[i], n2 = OB[i];
ret = max(ret, i + 1 + min(PA[n1 + 1], PB[n2 + 1]));
}
return ret;
}
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()));
// ans = max(ans, Solve(N, M, 1, 2));
// ans = max(ans, Solve(N, M, 1, 3));
// ans = max(ans, Solve(N, M, 2, 3));
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());
vector<int> U = {-(1 << 30)};
for (auto [a, b, n] : P) U.push_back(SB[b] - SA[a]);
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());
queue<pii> temp;
for (auto [a, b, n] : P) {
int t = SB[b] - SA[a];
int tp = lower_bound(U.begin(), U.end(), t) - U.begin();
if (n == 1) temp.emplace(a, b);
else {
while (!temp.empty() && temp.front().second < b) {
auto [c, d] = temp.front(); temp.pop();
int tt = SB[d] - SA[c];
int tp2 = lower_bound(U.begin(), U.end(), tt) - U.begin();
c1++;
TL.Update(tp2, c1 - SA[c]);
TR.Update(tp2, c1 - SB[d]);
}
ans = max(ans, TL.Query(1, tp) + SA[a] + c3);
ans = max(ans, TR.Query(tp, SZ) + SB[b] + 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: 24304kb
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: 161ms
memory: 24708kb
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 1 0 1 2 1 1 1 1 5 1 0 1 2 2 2 4 3 1 1 1 0 1 2 3 4 4 1 1 2 1 1 1 1 2 1 1 1 3 1 1 1 3 2 1 3 1 3 1 2 2 1 3 1 1 2 2 1 2 3 4 2 2 1 1 1 2 4 2 2 1 1 2 2 1 2 2 1 5 1 4 1 1 2 3 2 5 2 1 3 1 3 1 2 1 2 2 4 2 2 0 1 1 3 2 3 2 2 0 0 1 3 1 2 0 3 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 3 2 2 ...
result:
wrong answer 6th words differ - expected: '2', found: '1'