ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#196379 | #2474. 白玉楼的阶梯 | tkswls | 100 | 518ms | 55948kb | C++11 | 1.2kb | 2023-10-24 10:29:22 | 2023-10-24 12:19:31 |
answer
#include <bits/stdc++.h>
#define int long long
using namespace std;
int l[1000006], r[1000006], a[1000006], n, top, cnt[1000006], ans[1000006], k, ll[1000006], rr[1000006];
stack<int> s;
inline void update(int p) {
int lst = 0;
while (s.size()) {
if (a[p] < a[s.top()]) {
lst = s.top();
s.pop();
continue;
} else {
l[p] = lst;
r[s.top()] = p;
s.push(p);
return;
}
}
l[p] = lst;
top = p;
s.push(p);
}
inline void dfs(int p, int q) {
ll[p] = p;
rr[p] = p;
if (l[p]) {
dfs(l[p], p);
ll[p] = ll[l[p]];
}
if (r[p]) {
dfs(r[p], p);
rr[p] = rr[r[p]];
}
}
inline void ddfs(int p, int q) {
int op = (a[p] - a[q]) * (rr[p] - ll[p] + 1);
if (l[p]) ddfs(l[p], p), ans[p] += ans[l[p]], cnt[p] += cnt[l[p]];
if (r[p]) ddfs(r[p], p), ans[p] += ans[r[p]], cnt[p] += cnt[r[p]];
cnt[p] -= op;
if (cnt[p] < 0) {
ans[p] += cnt[p] / (-k) + 1;
cnt[p] += (cnt[p] / (-k) + 1) * k;
if (cnt[p] >= k) cnt[p] -= k, ans[p]--;
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
s.push(1);
top = 1;
for (int i = 2; i <= n; i++) {
update(i);
}
dfs(top, 0);
ddfs(top, 0);
cout << ans[top];
}
Details
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 0ms
memory: 1272kb
input:
5 4 1 1 3 1 3
output:
3
result:
ok single line: '3'
Test #2:
score: 10
Accepted
time: 1ms
memory: 1272kb
input:
5 3 3 3 1 3 1
output:
4
result:
ok single line: '4'
Test #3:
score: 10
Accepted
time: 0ms
memory: 1272kb
input:
5 3 1 3 1 3 2
output:
4
result:
ok single line: '4'
Test #4:
score: 10
Accepted
time: 0ms
memory: 1336kb
input:
1000 1005 820 380 111 826 886 736 457 92 734 320 37 199 715 557 942 335 233 228 103 130 733 123 547 ...
output:
498
result:
ok single line: '498'
Test #5:
score: 10
Accepted
time: 0ms
memory: 1340kb
input:
1000 1005 82 913 161 750 986 636 348 54 380 375 394 864 113 128 35 963 148 102 245 33 965 53 507 433...
output:
506
result:
ok single line: '506'
Test #6:
score: 10
Accepted
time: 0ms
memory: 1340kb
input:
1000 11 909 324 954 572 7 164 640 310 362 400 337 3 770 47 481 144 651 297 131 348 202 320 982 235 1...
output:
45649
result:
ok single line: '45649'
Test #7:
score: 10
Accepted
time: 15ms
memory: 7296kb
input:
100000 20000000 11 99 198 225 252 314 324 400 426 452 488 644 705 852 1027 1040 1238 1240 1274 1316 ...
output:
8722
result:
ok single line: '8722'
Test #8:
score: 10
Accepted
time: 21ms
memory: 7296kb
input:
100000 20000000 289 576 615 635 664 665 764 889 1302 1307 1326 1408 1433 1515 1639 1669 1688 1741 17...
output:
8636
result:
ok single line: '8636'
Test #9:
score: 10
Accepted
time: 223ms
memory: 52900kb
input:
1000000 900000000 7302 39900 103576 124265 169556 172635 183482 201475 291892 307703 342204 393241 4...
output:
263234
result:
ok single line: '263234'
Test #10:
score: 10
Accepted
time: 258ms
memory: 55948kb
input:
1000000 900000000 676756046 426505319 99817423 67508707 279930343 147047530 857013926 982935064 2056...
output:
543629
result:
ok single line: '543629'
Extra Test:
score: 0
Extra Test Passed