QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#73104 | #193. Climbers | 12345678 | 100 ✓ | 240ms | 415584kb | C++14 | 2.7kb | 2023-01-22 10:50:56 | 2023-01-22 10:50:58 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mp make_pair
#define inf 1e18
#define pii pair <int, int>
const int mod = 1e9 + 7;
inline int read () {
int x = 0, f = 1;
char ch = getchar ();
while (ch < '0' || ch > '9') f = ((ch == '-') ? -1 : f), ch = getchar ();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar ();
return x * f;
}
inline void write (int x) {
if (x < 0) x = -x, putchar ('-');
if (x >= 10) write (x / 10);
putchar (x % 10 + '0');
}
inline int quickmod (int x, int y) {
int Ans = 1;
while (y) {
if (y & 1) Ans = (Ans * x) % mod;
x = (x * x) % mod;
y >>= 1;
}
return Ans;
}
inline int Abs(int x) {
if(x > 0) return x;
return -x;
}
inline void Mn(int &x, int y) {
if(x > y) x = y;
}
int n, m;
int a[5005], b[5005];
inline int rng(int l, int r, int x) {
l = a[l], r = a[r];
if(l > r) swap(l, r);
return l <= x && x <= r;
}
int pathl(int l, int h, int r, int H) {
if(l == r) return 1;
if(l < r) return a[r] == H;
return a[l] == h;
}
int pathr(int l, int h, int r, int H) {
if(l == r) return 1;
if(l < r) return a[l] == h;
return a[r] == H;
}
struct Node {
int i, j, k, e;
Node() {}
Node(int A, int B, int C, int D) {
i = A, j = B, k = C, e = D;
}
bool operator <(const Node &A) const {
return e > A.e;
}
};
int f[5005][5005][2];
bool vis[5005][5005][2];
signed main () {
// freopen (".in", "r", stdin);
// freopen (".out", "w", stdout);
n = read();
for(int i = 1; i <= n; i++) a[i] = read();
b[++m] = a[1];
for(int i = 2; i < n; i++) if((a[i-1] < a[i]) ^ (a[i] < a[i+1])) b[++m] = a[i];
b[++m] = a[n];
swap(a, b), swap(n, m);
memset(f, 0x3f, sizeof f);
priority_queue <Node> Q;
f[1][n][0] = 0, Q.push(Node(1, n, 0, 0));
while(!Q.empty()) {
Node now = Q.top();
Q.pop();
int i = now.i, j = now.j, k = now.k;
if(i >= j) return printf("%lld\n", f[i][j][k]) & 0;
if(vis[i][j][k]) continue;
vis[i][j][k] = 1;
int h = (!k ? a[i] : a[j]);
for(auto I : {i - 1, i, i + 1}) {
for(auto J : {j - 1, j, j + 1}) {
for(auto K : {0, 1}) {
int H = (!K ? a[I] : a[J]), val = Abs(H - h);
if(!rng(I, I + 1, H) || !rng(J - 1, J, H) || I < 1 || I > n || J < 1 || J > n) continue;
if(!pathl(i, h, I, H)) continue;
// if(i == 3 && j == 7 && k == 0 && I == 3 && J == 6 && K == 1) printf("--\n");
if(!pathr(j, h, J, H)) continue;
if(f[I][J][K] > f[i][j][k] + val) {
f[I][J][K] = f[i][j][k] + val;
Q.push(Node(I, J, K, f[I][J][K]));
}
}
}
}
}
// printf("vis:%lld\n", (int)vis[3][7][0]);
// printf("vis:%lld\n", (int)vis[3][6][1]);
return 0;
}
/*
7
0 10 1 20 5 10 0
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 5
Accepted
time: 24ms
memory: 395780kb
input:
30 0 35 18 22 4 41 22 41 22 41 22 50 41 49 16 37 7 41 22 41 22 41 22 41 22 40 8 39 1 0
output:
572
result:
ok single line: '572'
Test #2:
score: 5
Accepted
time: 33ms
memory: 395808kb
input:
60 0 81 73 118 36 65 34 40 15 44 23 13 12 13 12 79 54 120 4 103 31 148 30 133 13 12 13 12 13 150 12 13 12 130 150 91 112 79 113 39 149 114 13 12 13 12 13 12 13 12 122 121 130 111 144 27 118 31 72 0
output:
3965
result:
ok single line: '3965'
Test #3:
score: 5
Accepted
time: 28ms
memory: 396012kb
input:
150 0 90 4 62 8 87 36 39 7 68 66 92 90 121 29 108 64 68 39 121 107 150 26 132 122 163 157 79 157 79 157 79 157 79 157 79 157 79 157 79 37 58 56 145 143 165 67 167 4 135 51 148 27 36 27 100 12 141 33 55 51 167 3 170 26 157 79 157 79 157 79 157 79 157 79 157 79 157 79 157 7 57 56 131 25 33 27 130 34 1...
output:
54316
result:
ok single line: '54316'
Test #4:
score: 5
Accepted
time: 12ms
memory: 395412kb
input:
100 0 7 3 359 347 386 215 267 176 383 163 213 108 374 19 76 216 386 216 386 216 386 216 386 216 386 68 97 85 171 157 318 190 375 133 231 111 355 111 211 8 216 386 216 386 216 386 216 386 216 386 216 386 385 209 247 167 306 88 262 132 164 65 146 60 400 75 24 284 216 386 216 386 216 386 216 386 216 38...
output:
34978
result:
ok single line: '34978'
Test #5:
score: 5
Accepted
time: 20ms
memory: 397220kb
input:
400 0 9 1 69 25 68 56 99 92 100 70 90 70 81 10 30 2 69 62 87 41 47 45 76 55 77 57 62 31 93 32 100 92 97 17 97 51 96 20 46 3 96 66 82 19 55 4 78 50 96 69 79 71 95 87 92 60 94 13 76 39 99 88 99 53 25 16 25 16 25 16 25 16 25 16 25 16 25 16 25 16 25 16 25 16 25 16 25 16 25 16 59 13 32 22 67 14 55 49 77 ...
output:
18100
result:
ok single line: '18100'
Test #6:
score: 5
Accepted
time: 3ms
memory: 395772kb
input:
100 0 895 482 782 243 888 554 980 317 660 264 625 356 829 444 513 322 393 316 975 130 868 422 999 144 559 205 477 123 674 142 263 167 667 260 740 313 768 276 691 1000 39 485 86 833 695 959 700 711 450 930 490 525 385 919 591 859 267 827 323 898 511 721 699 871 808 836 183 811 368 904 374 572 451 823...
output:
74518
result:
ok single line: '74518'
Test #7:
score: 5
Accepted
time: 4ms
memory: 396792kb
input:
300 0 2234 808 3265 948 3662 705 4619 812 3062 764 3365 2179 4191 3260 4320 1500 4240 1195 3010 2758 3487 102 2883 588 3081 328 3048 581 3648 388 3413 2646 4620 3978 4325 3127 4731 3735 4662 2347 4653 2799 3786 2586 3922 887 1995 781 1251 1059 2769 2418 4185 3904 4614 2697 2811 332 3870 3202 4348 11...
output:
4251662
result:
ok single line: '4251662'
Test #8:
score: 5
Accepted
time: 16ms
memory: 397380kb
input:
1000 0 17333 13126 15796 6107 27305 12291 24273 9155 29688 12339 18672 11786 22712 2665 22600 12406 24462 12979 27845 1048 17423 3058 26591 7917 27372 9637 23237 5342 5729 3918 15760 7093 26842 11098 19943 1808 26777 4264 26356 7598 13116 12039 29403 21857 25851 12781 22534 8354 27116 13198 25910 21...
output:
231963320
result:
ok single line: '231963320'
Test #9:
score: 5
Accepted
time: 35ms
memory: 403988kb
input:
3000 0 35030 29189 133951 74841 132314 114542 124283 81714 142460 24811 119612 5598 133847 82950 115176 7788 117514 75338 114999 28606 138886 50947 119697 62186 93000 78480 147320 90986 130578 90787 101789 98035 140398 34867 130582 6569 106437 7705 111672 14421 51046 5709 75803 19872 138748 7703 689...
output:
4402448622
result:
ok single line: '4402448622'
Test #10:
score: 5
Accepted
time: 68ms
memory: 415584kb
input:
5000 0 175205 119957 824222 445611 813274 86703 866578 248777 529352 30137 889485 294998 720961 88921 818675 174279 755203 77316 247890 131263 960765 203910 918035 89295 515523 137964 599612 68449 904811 492622 820327 509373 536632 321380 568121 359136 781871 316487 749871 555165 740747 545400 85254...
output:
130442494872
result:
ok single line: '130442494872'
Test #11:
score: 5
Accepted
time: 12ms
memory: 397252kb
input:
100 0 9328 2078 27815 34792 44319 34792 44319 34792 44319 34792 44319 34792 44319 34792 10405 41653 23189 34792 44319 34792 44319 34792 44319 34792 44319 34792 44319 34792 44319 34792 44319 47040 27694 29243 34792 44319 34792 44319 34792 44319 34792 44319 34792 44319 34792 44319 34792 44319 34792 44...
output:
1094540
result:
ok single line: '1094540'
Test #12:
score: 5
Accepted
time: 31ms
memory: 396004kb
input:
300 0 85190 67258 78151 5185 54563 22280 90328 74731 97764 50188 59032 50188 59032 50188 59032 50188 59032 50188 59032 50188 59032 50188 59032 50188 59032 50188 59032 50188 59032 50188 59032 50188 59032 50188 79042 84085 61880 75247 43617 69314 61511 76595 23795 50188 59032 50188 59032 50188 59032 5...
output:
24786984
result:
ok single line: '24786984'
Test #13:
score: 5
Accepted
time: 97ms
memory: 399044kb
input:
1000 0 116737 99614 299799 16585 299799 16585 299799 16585 299799 16585 299799 16585 299799 16585 183113 299799 16585 299799 16585 299799 16585 299799 16585 299799 16585 299799 16585 299799 16585 299799 173328 219238 299799 16585 299799 16585 299799 16585 299799 16585 299799 16585 299799 16585 29979...
output:
230422468
result:
ok single line: '230422468'
Test #14:
score: 5
Accepted
time: 157ms
memory: 402640kb
input:
3000 0 69924 59900 328514 294576 471175 422240 483388 146219 227756 107406 205844 487816 205844 487816 205844 487816 205844 487816 205844 487816 205844 487816 205844 487816 205844 487816 464327 242084 495883 14183 95703 34180 233203 174720 347022 128840 205844 487816 205844 487816 205844 487816 2058...
output:
3278917928
result:
ok single line: '3278917928'
Test #15:
score: 5
Accepted
time: 199ms
memory: 404620kb
input:
3000 0 99031 72755 93415 407285 697174 407285 697174 407285 697174 407285 697174 407285 697174 407285 697174 407285 697174 407285 697174 407285 697174 407285 697174 407285 697174 407285 49962 735651 18426 407285 697174 407285 697174 407285 697174 407285 697174 407285 697174 407285 697174 407285 6971...
output:
7072866772
result:
ok single line: '7072866772'
Test #16:
score: 5
Accepted
time: 177ms
memory: 405964kb
input:
4000 0 545127 103548 343211 181812 538161 207763 599389 466969 699193 415656 628200 271732 600225 532496 694227 277545 653690 156993 315025 114419 481586 79744 481586 79744 481586 79744 481586 79744 481586 79744 481586 79744 481586 79744 481586 79744 481586 79744 481586 79744 481586 79744 481586 797...
output:
22202684064
result:
ok single line: '22202684064'
Test #17:
score: 5
Accepted
time: 216ms
memory: 406396kb
input:
4000 0 57015 24880 170970 499132 170970 499132 170970 499132 170970 499132 170970 499132 170970 499132 170970 499132 170970 499132 170970 499132 170970 499132 170970 499132 170970 499132 170970 499132 170970 499132 170970 499132 451121 259990 170970 499132 170970 499132 170970 499132 170970 499132 1...
output:
6857985928
result:
ok single line: '6857985928'
Test #18:
score: 5
Accepted
time: 193ms
memory: 413928kb
input:
5000 0 922557 630537 769780 535891 877167 798109 885080 205094 328232 687924 328232 687924 328232 687924 328232 687924 328232 687924 328232 687924 328232 687924 278621 93974 178966 170411 988959 821072 853418 328232 687924 328232 687924 328232 687924 328232 687924 328232 687924 328232 687924 328232 ...
output:
54987742762
result:
ok single line: '54987742762'
Test #19:
score: 5
Accepted
time: 205ms
memory: 412904kb
input:
5000 0 63845 48703 452242 156419 229001 302963 880985 302963 880985 302963 880985 302963 880985 302963 880985 302963 880985 302963 880985 302963 880985 302963 880985 302963 880985 105919 901373 157751 438977 135422 302963 880985 302963 880985 302963 880985 302963 880985 302963 880985 302963 880985 3...
output:
23321970656
result:
ok single line: '23321970656'
Test #20:
score: 5
Accepted
time: 240ms
memory: 412880kb
input:
5000 0 547684 99661 627638 618126 455753 618126 455753 618126 455753 618126 455753 618126 455753 618126 455753 618126 455753 618126 455753 618126 455753 618126 455753 44843 301326 618126 455753 618126 455753 618126 455753 618126 455753 618126 455753 618126 455753 618126 455753 618126 455753 618126 4...
output:
9218936312
result:
ok single line: '9218936312'