QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#669762 | #7630. Yet Another Maximize Permutation Subarrays | MiniLong | AC ✓ | 1467ms | 128184kb | C++20 | 7.3kb | 2024-10-23 19:35:23 | 2024-10-23 19:35:23 |
Judging History
answer
#include <bits/stdc++.h>
#define _rep(i, x, y) for(int i = x; i <= y; ++i)
#define _req(i, x, y) for(int i = x; i >= y; --i)
#define _rev(i, u) for(int i = head[u]; i; i = e[i].nxt)
#define pb push_back
#define fi first
#define se second
#define mst(f, i) memset(f, i, sizeof f)
using namespace std;
#ifdef ONLINE_JUDGE
#define debug(...) 0
#else
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#endif
typedef long long ll;
typedef pair<int, int> PII;
namespace fastio{
#ifdef ONLINE_JUDGE
char ibuf[1 << 20],*p1 = ibuf, *p2 = ibuf;
#define get() p1 == p2 && (p2 = (p1 = ibuf) + fread(ibuf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++
#else
#define get() getchar()
#endif
template<typename T> inline void read(T &t){
T x = 0, f = 1;
char c = getchar();
while(!isdigit(c)){
if(c == '-') f = -f;
c = getchar();
}
while(isdigit(c)) x = x * 10 + c - '0', c = getchar();
t = x * f;
}
template<typename T, typename ... Args> inline void read(T &t, Args&... args){
read(t);
read(args...);
}
template<typename T> void write(T t){
if(t < 0) putchar('-'), t = -t;
if(t >= 10) write(t / 10);
putchar(t % 10 + '0');
}
template<typename T, typename ... Args> void write(T t, Args... args){
write(t), putchar(' '), write(args...);
}
template<typename T> void writeln(T t){
write(t);
puts("");
}
template<typename T> void writes(T t){
write(t), putchar(' ');
}
#undef get
};
using namespace fastio;
#define multitest() int T; read(T); _rep(tCase, 1, T)
namespace Calculation{
const ll mod = 998244353;
ll ksm(ll p, ll h){ll base = p % mod, res = 1; while(h){if(h & 1ll) res = res * base % mod; base = base * base % mod, h >>= 1ll;} return res;}
void dec(ll &x, ll y){x = ((x - y) % mod + mod) % mod;}
void add(ll &x, ll y){x = (x + y) % mod;}
void mul(ll &x, ll y){x = x * y % mod;}
ll sub(ll x, ll y){return ((x - y) % mod + mod) % mod;}
ll pls(ll x, ll y){return ((x + y) % mod + mod) % mod;}
ll mult(ll x, ll y){return x * y % mod;}
}
using namespace Calculation;
const int N = 1e6 + 5;
int n, a[N], p[N], L[N], R[N], f[N];
bool vis[N];
struct dsu{
int fa[N], maxn[N];
void init(){_rep(i, 1, n) fa[i] = maxn[i] = i;}
int find(int x){return fa[x] == x ? x : fa[x] = find(fa[x]);}
int merge(int x, int y){
int fx = find(x), fy = find(y);
if(fx < fy) swap(fx, fy);
if(fx != fy) return fa[fx] = fy, maxn[fy] = maxn[fx], 1;
return 0;
}
void clr(){_rep(i, 1, n) fa[i] = maxn[i] = 0;}
}D;
namespace bit{
int c[N];
void add(int x){for(; x <= n; x += x & -x) c[x]++;}
int ask(int k){
int pos = 0;
_req(i, __lg(n), 0) if(pos + (1 << i) <= n && c[pos + (1 << i)] < k){
pos += (1 << i), k -= c[pos];
}
return pos + 1;
}
void clr(){_rep(i, 1, n) c[i] = 0;}
}
namespace sgt{
int tr[N << 2], tag[N << 2], pos[N << 2];
#define ls x << 1
#define rs x << 1 | 1
void pushtag(int x, int val){tr[x] += val, tag[x] += val;}
void pushdown(int x){
if(!tag[x]) return;
pushtag(ls, tag[x]), pushtag(rs, tag[x]);
tag[x] = 0;
}
void build(int x, int l, int r){
pos[x] = l;
if(l == r) return;
int mid = l + r >> 1;
build(ls, l, mid), build(rs, mid + 1, r);
}
void modify(int x, int l, int r, int L, int R, int val){
if(l >= L && r <= R) return pushtag(x, val);
pushdown(x);
int mid = l + r >> 1;
if(L <= mid) modify(ls, l, mid, L, R, val);
if(R > mid) modify(rs, mid + 1, r, L, R, val);
tr[x] = max(tr[ls], tr[rs]);
pos[x] = tr[x] == tr[ls] ? pos[ls] : pos[rs];
}
void clr(int x, int l, int r){
tr[x] = tag[x] = pos[x] = 0;
if(l == r) return;
int mid = l + r >> 1;
clr(ls, l, mid), clr(rs, mid + 1, r);
}
}
struct node{
int l, r, v;
};
vector<node> h[N];
void ins(int l, int r, int L, int R, int val){
debug("i:[%d,%d] j:[%d,%d] val:%d\n", l, r, L, R, val);
h[l].pb({L, R, val}), h[r + 1].pb({L, R, -val});
}
void clr(){
_rep(i, 0, n + 1) L[i] = R[i] = p[i] = a[i] = vis[i] = 0, h[i].clear();
sgt::clr(1, 1, n), bit::clr(), D.clr();
}
typedef unsigned long long ull;
void check(int x, int y){
swap(a[x], a[y]);
int ans = 0;
_rep(i, 1, n){
int maxn = 0;
_rep(j, i, n) maxn = max(maxn, a[j]), ans += (maxn == j - i + 1);
}
debug("check (%d,%d) ans:%d\n", x, y, ans);
swap(a[x], a[y]);
}
int main(){
multitest(){
read(n), D.init();
_rep(i, 1, n) read(a[i]), p[a[i]] = i;
L[1] = R[1] = p[1];
int ans = 0, tot = 0, cur = 0;
_rep(i, 1, n){
vis[p[i]] = 1, bit::add(p[i]);
if(i > 1) L[i] = L[i - 1], R[i] = R[i - 1];
L[i] = min(L[i], p[i]), R[i] = max(R[i], p[i]);
cur++;
if(vis[p[i] - 1]) cur -= D.merge(p[i], p[i] - 1);
if(vis[p[i] + 1]) cur -= D.merge(p[i], p[i] + 1);
debug("i:%d\n", i);
if(R[i] - L[i] + 1 == i){
tot++;
if(i > 1){
if(L[i] > 2) ins(1, L[i] - 2, L[i], R[i], -1);
if(L[i] > 1) ins(L[i] - 1, L[i] - 1, L[i], R[i] - 1, -1);
if(R[i] + 1 < n) ins(L[i], L[i], R[i] + 2, n, -1);
if(R[i] < n) ins(L[i] + 1, R[i], R[i] + 1, n, -1);
}
}else{
if(cur > 3) continue;
if(cur == 2){
int a = L[i], b = D.maxn[a], c = D.find(R[i]), d = R[i];
if(a == b){
ins(a, a, c - 1, c - 1, 1);
if(d < n) ins(a, a, d + 1, d + 1, 1);
}else if(b + 2 == c){
ins(a, a, c - 1, c - 1, 1);
}
if(c == d){
ins(b + 1, b + 1, c, c, 1);
if(a > 1) ins(a - 1, a - 1, c, c, 1);
}else if(b + 2 == c){
ins(b + 1, b + 1, d, d, 1);
}
}
if(cur == 3){
int a = L[i], b = D.maxn[a], c = 0, d = 0, e = D.find(R[i]), f = R[i];
d = bit::ask(i - (f - e + 1)), c = D.find(d);
if(a == b && d + 2 == e) ins(a, a, d + 1, d + 1, 1);
if(e == f && b + 2 == c) ins(b + 1, b + 1, e, e, 1);
}
}
}
ans = tot; PII res = {1, 1};
sgt::build(1, 1, n);
_rep(i, 1, n){
for(auto &j : h[i]){
// debug("modify [%d,%d] %d", j.l, j.r, j.v);
sgt::modify(1, 1, n, j.l, j.r, j.v);
}
if(tot + sgt::tr[1] > ans){
// debug("???\n");
ans = tot + sgt::tr[1];
res = {i, sgt::pos[1]};
}
ans = max(ans, tot + sgt::tr[1]);
}
write(res.fi, res.se), puts("");
debug("ans:%d\n", ans); check(9, 9);
clr();
}
return 0;
}
这程序好像有点Bug,我给组数据试试?
详细
Test #1:
score: 100
Accepted
time: 5ms
memory: 22120kb
input:
8 3 1 2 3 3 1 3 2 5 1 3 2 5 4 6 4 5 6 1 2 3 9 8 7 6 3 2 1 4 5 9 10 7 10 5 1 9 8 3 2 6 4 10 8 5 10 9 2 1 3 4 6 7 10 2 3 5 7 10 1 8 6 4 9
output:
1 1 1 2 1 4 1 3 1 1 4 9 2 4 1 5
result:
ok 8 cases
Test #2:
score: 0
Accepted
time: 0ms
memory: 24104kb
input:
10 9 6 2 8 3 4 5 7 1 9 17 15 10 11 9 4 5 17 1 2 7 6 8 14 12 13 16 3 19 5 10 12 11 14 6 15 19 2 18 8 1 16 13 7 4 3 17 9 18 5 8 10 6 4 7 9 2 11 3 18 1 13 17 12 14 16 15 2 1 2 8 8 7 6 5 4 1 3 2 9 1 2 3 4 9 7 5 8 6 11 1 11 3 4 2 9 7 6 8 10 5 6 1 6 5 2 3 4 11 7 8 10 1 9 11 4 2 3 5 6
output:
3 8 7 17 1 8 9 12 1 1 5 8 5 9 2 5 1 3 4 6
result:
ok 10 cases
Test #3:
score: 0
Accepted
time: 0ms
memory: 26072kb
input:
10 18 10 13 12 1 2 3 4 5 6 8 9 7 17 11 15 14 16 18 8 3 1 4 2 6 5 8 7 12 10 9 1 7 6 5 3 2 8 4 11 12 15 15 14 12 13 10 5 9 7 8 6 1 2 3 4 11 6 5 4 3 2 1 6 14 13 3 4 2 10 5 8 1 7 12 9 11 6 14 1 1 1 1 5 5 2 1 4 3 17 1 8 9 7 10 6 2 5 4 3 11 17 13 15 16 14 12
output:
1 13 3 4 3 9 6 9 1 1 5 8 1 1 1 1 1 5 1 5
result:
ok 10 cases
Test #4:
score: 0
Accepted
time: 0ms
memory: 24104kb
input:
9 4 4 2 3 1 5 5 2 3 1 4 8 7 8 6 5 3 2 1 4 17 15 16 13 12 14 1 3 4 2 5 6 7 9 11 10 8 17 2 1 2 17 16 14 11 8 5 4 3 15 2 6 7 9 10 12 13 1 17 8 1 5 6 7 2 3 4 8 15 10 7 13 2 8 4 1 15 6 5 11 14 3 12 9 7 2 4 3 1 5 6 7
output:
1 4 2 3 1 2 5 16 1 1 8 16 1 4 1 8 1 5
result:
ok 9 cases
Test #5:
score: 0
Accepted
time: 0ms
memory: 26160kb
input:
9 80 78 77 73 72 69 70 66 64 62 63 61 60 58 57 56 54 53 55 50 51 49 43 44 38 36 35 34 33 27 25 24 26 19 17 14 16 15 9 1 5 2 3 4 6 8 7 10 11 12 13 18 20 21 22 23 28 31 30 29 32 37 41 42 39 40 45 46 47 48 52 59 68 65 67 71 75 74 76 79 80 52 49 48 35 31 30 32 27 25 1 23 22 21 17 15 16 14 13 12 9 10 11 ...
output:
39 40 9 28 11 14 2 11 1 147 1 18 1 66 12 32 51 63
result:
ok 9 cases
Test #6:
score: 0
Accepted
time: 2ms
memory: 24216kb
input:
8 151 150 151 149 148 129 131 130 125 126 124 128 127 123 119 115 117 122 121 116 120 118 111 112 113 110 109 108 107 105 106 103 104 102 100 98 101 99 97 96 95 80 79 72 73 71 68 69 70 56 51 47 50 48 49 46 33 35 27 34 31 30 29 32 26 28 25 24 23 22 21 20 17 19 18 16 11 12 7 5 6 1 4 3 2 10 8 9 14 13 1...
output:
58 63 2 12 1 14 1 32 1 23 4 12 70 163 86 170
result:
ok 8 cases
Test #7:
score: 0
Accepted
time: 0ms
memory: 26196kb
input:
7 144 144 143 142 141 139 137 138 136 135 133 131 130 129 127 125 120 119 116 117 109 108 107 105 104 103 80 99 100 98 95 92 91 89 88 85 81 101 79 73 72 64 63 61 59 60 58 57 56 52 51 50 45 43 44 42 41 36 34 32 33 31 30 29 27 26 21 22 20 19 13 12 11 9 5 4 3 2 1 6 7 8 10 16 17 15 14 18 24 23 25 28 35 ...
output:
26 37 19 22 53 141 40 96 1 105 1 47 1 53
result:
ok 7 cases
Test #8:
score: 0
Accepted
time: 3ms
memory: 26332kb
input:
8 487 487 486 485 484 481 480 479 477 475 472 473 469 465 464 463 458 453 452 451 450 448 449 446 435 434 433 432 431 430 429 428 427 426 419 418 414 413 408 406 405 396 390 391 389 384 386 385 382 381 380 379 376 374 375 373 372 370 371 369 368 366 367 360 359 357 356 355 354 353 351 349 350 345 34...
output:
365 413 1 1088 1 112 73 490 324 794 1 531 59 609 1 1861
result:
ok 8 cases
Test #9:
score: 0
Accepted
time: 5ms
memory: 24440kb
input:
8 403 402 400 399 398 394 393 395 396 391 390 389 388 387 386 385 384 383 376 373 372 371 370 366 368 369 367 364 360 361 359 355 356 352 351 350 349 341 340 338 339 336 335 327 326 325 324 319 318 317 315 314 313 311 308 310 309 307 306 305 304 299 302 300 301 296 293 290 288 289 287 286 285 284 28...
output:
218 245 228 282 2 1464 482 565 1 1229 100 596 224 747 199 1465
result:
ok 8 cases
Test #10:
score: 0
Accepted
time: 4ms
memory: 26464kb
input:
9 464 427 67 114 302 21 32 268 339 309 366 432 286 88 10 347 290 353 248 249 261 360 9 284 191 405 17 464 110 341 349 188 49 33 235 451 100 81 95 410 457 355 62 381 266 37 351 189 402 107 263 184 246 350 94 186 438 93 233 279 441 158 140 379 417 55 156 439 342 278 320 13 435 142 265 270 36 173 218 7...
output:
1 27 736 1473 1 92 515 568 63 208 397 530 100 597 433 725 19 200
result:
ok 9 cases
Test #11:
score: 0
Accepted
time: 41ms
memory: 33800kb
input:
7 2236 2234 2235 2236 2222 2221 2220 2219 2212 2213 2208 2209 2206 2205 2207 2197 2196 2194 2193 2192 2195 2191 2189 2188 2190 2180 2181 2175 2176 2177 2179 2178 2171 2168 2166 2165 2170 2167 2164 2169 2155 2154 2153 2152 2151 2129 2125 2126 2123 2120 2122 2121 2119 2117 2114 2115 2107 2106 2105 210...
output:
1500 1939 9639 14094 15053 16385 105 709 11301 14867 1 7295 1596 8769
result:
ok 7 cases
Test #12:
score: 0
Accepted
time: 39ms
memory: 33580kb
input:
9 14132 14128 14132 14131 14129 14130 14126 14117 14115 14114 14111 14110 14112 14108 14105 14104 14103 14102 14101 14100 14091 14093 14092 14090 14088 14082 14083 14081 14077 14075 14073 14072 14074 14071 14068 14067 14060 14059 14058 14057 14056 14055 14053 14052 14051 14043 14042 14038 14034 1403...
output:
2533 2905 1 10846 5335 6106 1 8914 1 2158 1 11293 54 87 2160 5749 1432 1647
result:
ok 9 cases
Test #13:
score: 0
Accepted
time: 31ms
memory: 30564kb
input:
9 11668 122 3358 10900 8761 11606 6985 11355 6706 11563 3705 7295 627 11650 6491 8905 8408 1648 3401 4976 9571 8524 2075 10416 9513 3700 3565 4160 5098 8140 909 3951 11517 7913 9256 10863 3141 5284 5805 784 4403 3751 8015 10058 6313 3547 8244 726 719 9645 9538 8839 10802 681 4118 9712 8591 4722 6306...
output:
1 2170 1 7463 1 4856 1503 2840 5422 5651 313 472 9101 9799 535 6035 1766 2792
result:
ok 9 cases
Test #14:
score: 0
Accepted
time: 391ms
memory: 60568kb
input:
7 193626 193626 193625 193622 193621 193624 193623 193620 193617 193618 193619 193605 193606 193587 193578 193575 193574 193555 193563 193558 193561 193566 193554 193571 193570 193556 193567 193557 193560 193562 193559 193568 193553 193569 193565 193564 193573 193572 193550 193549 193547 193548 1935...
output:
82765 139831 58002 116795 6379 14664 1 17491 1 13722 24113 38850 490 1448
result:
ok 7 cases
Test #15:
score: 0
Accepted
time: 511ms
memory: 61752kb
input:
10 55747 55745 55744 55737 55734 55731 55729 55726 55724 55723 55725 55721 55720 55719 55718 55715 55713 55706 55705 55702 55701 55697 55695 55694 55692 55690 55687 55686 55685 55683 55681 55682 55680 55678 55669 55667 55666 55662 55654 55653 55649 55648 55645 55644 55643 55642 55641 55628 55627 556...
output:
25048 28220 1 19115 41617 167132 5147 11329 331 1530 5963 53264 3056 30379 126366 133195 6947 31910 90122 92794
result:
ok 10 cases
Test #16:
score: 0
Accepted
time: 534ms
memory: 78400kb
input:
8 161886 161886 161884 161883 161881 161880 161877 161874 161873 161871 161872 161869 161868 161866 161865 161862 161858 161856 161855 161852 161851 161845 161846 161841 161839 161837 161836 161834 161833 161830 161828 161826 161820 161819 161821 161818 161817 161815 161810 161809 161807 161803 1618...
output:
14047 145753 1 14461 1 16855 35562 48992 54249 54651 33403 41987 78930 82807 5621 39152
result:
ok 8 cases
Test #17:
score: 0
Accepted
time: 1467ms
memory: 128184kb
input:
8 477214 477214 477209 477208 477207 477206 477204 477205 477199 477198 477200 477196 477192 477193 477189 477190 477188 477185 477184 477182 477181 477180 477174 477173 477172 477171 477170 477163 477164 477162 477157 477158 477156 477155 477154 477151 477145 477146 477134 477135 477133 477132 4771...
output:
69720 198310 151 10152 127265 139375 101073 402556 175992 198298 129760 142366 5990 14234 191453 368817
result:
ok 8 cases
Test #18:
score: 0
Accepted
time: 948ms
memory: 113104kb
input:
7 104640 80979 94896 93379 84998 101575 69108 32621 13413 19543 63751 74406 100342 91227 41571 7722 15382 88496 71052 98146 91567 98194 76721 20313 26614 79982 69719 85888 82373 104512 10114 34784 77042 99044 54249 83799 63420 72841 67390 96124 80151 102095 44828 82307 74818 58750 94133 63179 74514 ...
output:
1 110 46196 108910 85667 108875 11560 27356 1 18761 218547 334416 31523 85710
result:
ok 7 cases
Test #19:
score: 0
Accepted
time: 664ms
memory: 93528kb
input:
7 3719 3719 3718 3715 3712 3710 3711 3701 3700 3698 3696 3692 3690 3687 3686 3683 3682 3681 3678 3677 3676 3675 3673 3674 3670 3669 3664 3663 3661 3660 3658 3659 3655 3654 3651 3650 3643 3641 3635 3634 3633 3631 3632 3630 3628 3629 3627 3626 3624 3622 3618 3617 3614 3611 3610 3606 3605 3604 3599 359...
output:
2315 3633 1 26862 11209 21572 1 10845 207417 216701 150862 161659 58611 264533
result:
ok 7 cases
Extra Test:
score: 0
Extra Test Passed