QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#771202 | #9742. 优秀的拆分 | Symmetree | RE | 0ms | 0kb | C++20 | 2.6kb | 2024-11-22 10:47:02 | 2024-11-22 10:47:02 |
answer
#include<bits/stdc++.h>
const int N = 2e5+5, mod = 1e9+7;
int n, c[N], p[N], f[2][N], g[2][N], d[N], cntf[2][N], cntg[2][N];
void add(int x, int k, int cnt) {
for(; x <= n; x += x&-x) {
if(c[x] < k) c[x] = k, d[x] = cnt;
else if(c[x] == k) d[x] += cnt, d[x] %= mod;
}
}
int ask1(int x) {
int ans = 0;
for(; x; x -= x&-x) ans = std::max(ans, c[x]);
return ans;
}
int ask2(int x) {
int now = 0, ans = 1;
for(; x; x -= x&-x) {
if(now < c[x]) ans = d[x], now = c[x];
else if(now == c[x]) ans += d[x], ans %= mod;
}
return ans;
}
void calc(int f[], int cntf[]) {
for(int i = 1; i <= n; ++i) c[i] = d[i] = f[i] = cntf[i] = 0;
for(int i = 1; i <= n; ++i) {
f[i] = ask1(p[i]) + 1;
cntf[i] = ask2(p[i]);
add(p[i], f[i], cntf[i]);
}
}
int t, tt;
void solve() {
scanf("%d", &n);
for(int i = 1; i <= n; ++i) scanf("%d", &p[i]);
// if(t == 20 && tt == 15) {
// printf("%d ", n);
// for(int i = 1; i <= n; ++i) printf("%d ", p[i]);
// }
// for(int i = 1; i <= n; ++i) printf("%d %lld\n", c[i], d[i]);
// for(int i = 1 ; i <= n; ++i) printf("%d %lld\n", f[0][i], cntf[0][i]);
calc(f[0], cntf[0]);
std::reverse(p + 1, p + n + 1);
calc(g[1], cntg[1]);
std::reverse(g[1] + 1, g[1] + n + 1);
std::reverse(cntg[1] + 1, cntg[1] + n + 1);
for(int i = 1; i <= n; ++i) p[i] = n + 1 - p[i];
calc(f[1], cntf[1]);
std::reverse(f[1] + 1, f[1] + n + 1);
std::reverse(cntf[1] + 1, cntf[1] + n + 1);
std::reverse(p + 1, p + n + 1);
calc(g[0], cntg[0]);
// for(int i = 1; i <= n; ++i) printf("%d %lld %d %lld\n", f[0][i], cntf[0][i], f[1][i], cntf[1][i]);
// puts("");
// for(int i = 1; i <= n; ++i) printf("%d %lld %d %lld\n", g[0][i], cntg[0][i], g[1][i], cntg[1][i]);
int LIS = 1, LDS = 1;
for(int i = 1; i <= n; ++i) {
LIS = std::max(LIS, f[0][i]);
LDS = std::max(LDS, g[0][i]);
}
int numi = 0, numd = 0, num = 0;
for(int i = 1; i <= n; ++i) {
if(f[0][i] == LIS) numi += cntf[0][i], numi %= mod;
if(g[0][i] == LDS) numd += cntg[0][i], numd %= mod;
if((f[0][i] + f[1][i] - 1 == LIS) && (g[0][i] + g[1][i] - 1 == LDS)) {
num += 1ll * cntf[0][i] * cntf[1][i] % mod * cntg[0][i] % mod * cntg[1][i] % mod;
num %= mod;
}
}
// for(int i = 1; i <= n; ++i) printf("%lld ", cntf[0][i]); puts("");
// assert(num >= 0), assert((i128)numi * numd >= 0);
if(1ll * numi * numd % mod == num) printf("%d\n", LIS + LDS - 1);
else printf("%d\n", LIS + LDS);
}
signed main() {
freopen("in.in", "r", stdin);
freopen("out.out", "w", stdout);
scanf("%d", &t);
for(tt = 1; tt <= t; ++tt) solve();
return 0;
}
详细
Test #1:
score: 0
Dangerous Syscalls
input:
3 5 3 5 1 4 2 4 1 2 4 3 5 3 5 2 1 4