QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#517146 | #1281. Longest Common Subsequence | Z_drj | TL | 7ms | 39124kb | C++14 | 1.9kb | 2024-08-13 09:20:51 | 2024-08-13 09:20:51 |
Judging History
answer
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using i64 = long long;
const int N = 1e6 + 5;
const int INF = 2E6;
int n, m;
int a[N], b[N];
int sa[N], sb[N];
struct FenwickTree {
int t[N << 2];
void clear() {
std::fill(t + 1, t + 2 * INF + 2, -INF);
}
int lowbit(int x) {
return x & -x;
}
void update(int x, int val) {
x += INF + 1;
for (; x; x -= lowbit(x)) {
t[x] = std::max(t[x], val);
}
}
int query(int x) {
x += INF + 1;
int ans = 0;
for (; x <= INF * 2 + 1; x += lowbit(x)) {
ans = std::max(ans, t[x]);
}
return ans;
}
}t1, t2;
void solve() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
sa[i] = sa[i - 1] + (a[i] == 2);
}
for (int i = 1; i <= m; i++) {
scanf("%d", b + i);
sb[i] = sb[i - 1] + (b[i] == 2);
}
sa[n + 1] = sa[n], sb[m + 1] = sb[m];
std::vector<int> la, ra, lb, rb;
la.push_back(0), lb.push_back(0);
ra.push_back(n + 1), rb.push_back(m + 1);
for (int i = 1; i <= n; i++) {
if (a[i] == 1) {
la.push_back(i);
}
}
for (int i = n; i >= 1; i--) {
if (a[i] == 3) {
ra.push_back(i);
}
}
for (int i = 1; i <= m; i++) {
if (b[i] == 1) {
lb.push_back(i);
}
}
for (int i = m; i >= 1; i--) {
if (b[i] == 3) {
rb.push_back(i);
}
}
t1.clear(), t2.clear();
int ans = std::min(sa[n], sb[m]);
for (int i = std::min((int)la.size(), (int)lb.size()) - 1, j = 0; i >= 0; i--) {
while (j < std::min((int)ra.size(), (int)rb.size()) && la[i] < ra[j] && lb[i] < rb[j]) {
t1.update(sa[ra[j]] - sb[rb[j]], j + sb[rb[j]]), t2.update(sb[rb[j]] - sa[ra[j]], j + sa[ra[j]]), ++j;
}
ans = std::max(ans, i + t1.query(sa[la[i]] - sb[lb[i]]) - sb[lb[i]]);
ans = std::max(ans, i + t2.query(sb[lb[i]] - sa[la[i]]) - sa[la[i]]);
}
printf("%d\n", ans);
}
int main() {
int T; scanf("%d", &T);
while (T--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 7ms
memory: 39124kb
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
Time Limit Exceeded
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...