QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#744186 | #9742. 优秀的拆分 | TJUHuangTao | WA | 30ms | 24312kb | C++20 | 3.3kb | 2024-11-13 21:05:24 | 2024-11-13 21:05:25 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
#define inf 0x3f3f3f3f
#define ll long long
#define pii pair<int, int>
#define tii tuple<int, int, int>
#define db double
#define all(a) a.begin(), a.end()
using namespace std;
const int maxn = 2e5 + 10;
const int mod = 998244353;
int arr[maxn];
struct BIT {
vector<pii> t;
int len;
BIT(int n) {len = n + 5;t.resize(len); fill(all(t), pii(0, 0));}
int lowbit(int x) {return x & -x;}
void update(int i, pii x) {
i++;
for (int pos = i; pos < len; pos += lowbit(pos)) {
if (t[pos].first == x.first) t[pos].second += x.second;
else if (t[pos].first < x.first)
t[pos] = x;
}
}
pii query(int i) {
i++;
pii res = {0, 0};
for (int pos = i; pos; pos -= lowbit(pos))
if (t[pos].first == res.first) res.second += t[pos].second;
else if (t[pos].first > res.first) res = t[pos];
return res;
}
};
int f[maxn], g[maxn]; //以 i 结尾的LIS长度和方案数
int F[maxn], G[maxn]; //以 i 结尾的LDS长度和方案数
int N[maxn], M[maxn]; //以 i 开头的LIS的长度和方案数
int N2[maxn], M2[maxn]; // 以 i 开头的LDS的长度和方案数
int h[maxn]; // 必选 i 位置时的LIS方案数
int H[maxn]; // 必选 i 位置时的LDS方案数
void solve() {
int n; cin >> n;
for (int i = 0; i <= n; i++)
h[i] = H[i] = 0;
for (int i = 1; i <= n; i++) cin >> arr[i];
int LIS = 0, LDS = 0;
BIT bit1(n);
for (int i = 1; i <= n; i++) {
auto [len, cnt] = bit1.query(arr[i] - 1);
if (++len == 1) cnt = 1;
f[i] = len;
g[i] = cnt;
bit1.update(arr[i], {f[i], g[i]});
LIS = max(LIS, f[i]);
}
BIT bit2(n);
for (int i = 1; i <= n; i++) {
auto [len, cnt] = bit2.query(n - arr[i]);
if (++len == 1) cnt = 1;
F[i] = len;
G[i] = cnt;
bit2.update(n + 1 - arr[i], {F[i], G[i]});
LDS = max(LDS, F[i]);
}
BIT bit3(n);
for (int i = n; i; i--) {
auto [len, cnt] = bit3.query(n - arr[i]);
if (++len == 1) cnt = 1;
N[i] = len;
M[i] = cnt;
bit3.update(n + 1 - arr[i], {N[i], M[i]});
}
for (int i = 1; i <= n; i++) {
if (f[i] + N[i] - 1 != LIS) continue;
h[i] = g[i] * M[i];
}
BIT bit4(n);
for (int i = n; i; i--) {
auto [len, cnt] = bit4.query(arr[i] - 1);
if (++len == 1) cnt = 1;
N2[i] = len;
M2[i] = cnt;
bit4.update(arr[i], {N2[i], M2[i]});
}
for (int i = 1; i <= n; i++) {
if (F[i] + N2[i] - 1 != LDS) continue;
H[i] = G[i] * M2[i];
}
int c1 = 0, c2 = 0;
for (int i = 1; i <= n; i++) {
if (f[i] == LIS) c1 += g[i];
if (F[i] == LDS) c2 += G[i];
}
int sum = c1 * c2;
for (int i = 1; i <= n; i++)
sum -= h[i] * H[i];
if (!sum) cout << LIS + LDS - 1 << "\n";
else cout << LIS + LDS << "\n";
}
signed main() {
// freopen("1.in", "r", stdin);
// freopen("1.out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t = 1;
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: 19944kb
input:
3 5 3 5 1 4 2 4 1 2 4 3 5 3 5 2 1 4
output:
4 4 5
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 11ms
memory: 18200kb
input:
20000 2 2 1 10 6 10 2 7 9 3 8 4 1 5 9 3 6 4 5 8 9 2 7 1 7 3 7 6 4 1 5 2 7 7 2 3 6 5 1 4 5 4 1 3 2 5 9 6 7 5 3 8 1 9 2 4 3 1 2 3 8 7 2 4 6 1 8 3 5 7 4 2 6 3 1 7 5 8 1 7 3 4 2 5 6 8 2 1 2 10 10 2 3 8 6 9 1 4 7 5 4 3 2 1 4 9 7 5 3 4 1 2 9 6 8 7 2 4 5 1 6 7 3 10 3 1 10 4 9 5 6 8 2 7 5 1 2 5 3 4 6 2 6 5 ...
output:
2 8 8 6 7 5 7 3 6 6 8 2 8 4 7 6 9 5 6 7 7 8 9 7 8 1 5 6 5 7 3 3 4 3 6 7 6 1 6 5 1 8 6 8 6 6 2 3 2 6 8 5 6 5 7 3 2 7 7 6 3 1 3 1 8 7 8 4 1 1 8 7 7 2 7 2 1 9 2 5 6 3 5 6 8 6 2 2 1 6 6 7 8 3 1 8 6 10 2 8 8 4 6 3 8 8 2 4 1 3 5 8 9 1 7 7 2 6 7 4 8 1 2 5 3 1 8 6 7 1 9 7 5 7 3 8 1 5 5 6 4 5 4 1 6 2 7 6 1 7...
result:
ok 20000 lines
Test #3:
score: 0
Accepted
time: 17ms
memory: 18464kb
input:
20 895 97 29 511 535 178 149 710 846 37 191 684 504 309 528 524 189 659 42 337 688 213 343 859 433 606 445 693 608 232 585 577 313 759 746 612 341 480 875 610 222 28 637 11 91 796 261 296 333 377 871 275 552 788 371 763 862 522 322 447 126 492 694 799 620 842 394 434 706 460 479 240 241 676 502 749 ...
output:
115 165 331 171 112 204 247 226 239 114 231 241 57 229 371 243 347 120 62 352
result:
ok 20 lines
Test #4:
score: -100
Wrong Answer
time: 30ms
memory: 24312kb
input:
2 66375 22486 8985 25896 9585 22371 18677 32794 51856 4976 20566 19519 11668 36820 19785 27213 14206 5728 54919 55392 20146 5373 20907 66131 64447 53265 22521 1393 31296 58406 54362 2294 13520 13660 59044 13345 44636 52942 37423 64998 54440 50291 61802 16224 5240 59589 52028 52268 6841 12466 65025 5...
output:
999 317
result:
wrong answer 1st lines differ - expected: '1000', found: '999'