# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1116419 | 2024-11-21T15:50:37 Z | ntdaccode | Cat Exercise (JOI23_ho_t4) | C++17 | 91 ms | 53428 KB |
#include<bits/stdc++.h> #define fori(i,a,b) for(int i = a;i <= b; i++) #define int long long #define pb push_back using namespace std; typedef pair<int,int> ii; typedef tuple<int,int,int> tp; const int M = 1e6 + 10; const int N = 1e3 + 10; const int mod = 1e9 + 7; int n,p[M]; int ma[M][20],lg[M]; int getmax(int l,int r) { int k = lg[r - l + 1]; int u = ma[l][k]; int v = ma[r - (1<<k) +1][k]; if(p[u] > p[v]) return u; return v; } int f[M],pos[M],cnt = 0; int solve(int l,int r) { //cout << l << " " << r << "\n"; if(l > r) return -1; cnt++; int cur = cnt; pos[cur] = getmax(l,r); //cout << l << " " << r << " " << pos[cur] << "\n"; int pre = solve(l,pos[cur] - 1); int nxt = solve(pos[cur] + 1,r); if(pre != -1) f[cur] = max(f[cur],f[pre] + abs(pos[cur] - pos[pre])); if(nxt != -1) f[cur] = max(f[cur],f[nxt] + abs(pos[cur] - pos[nxt])); //cout << l << " " << r << " " << f[cur] << "sa\n"; return cur; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if(fopen("1.inp","r")) { freopen("1.inp","r",stdin); freopen("1.out","w",stdout); } #define task "" if(fopen(task".inp","r")) { freopen(task".inp","r",stdin); freopen(task".out","w",stdout); } cin >> n ; for(int i = 1;i <= n; i++) cin >> p[i],ma[i][0] = i; for(int i = 1;i <= n - 1; i++) { int u,v; cin >> u >> v; } for(int i = 1;i <= 18; i++) { for(int j = 1;j + (1<<i) - 1 <= n; j++) { int u = ma[j][i - 1]; int v = ma[j + (1 << i-1)][i - 1]; if(p[u] > p[v]) ma[j][i] = u; else ma[j][i] = v; } } for(int i = 1;i <= n; i++) lg[i]= __lg(i); solve(1,n); cout << f[1] ; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 6480 KB | Output is correct |
2 | Correct | 2 ms | 6648 KB | Output is correct |
3 | Correct | 1 ms | 6480 KB | Output is correct |
4 | Correct | 1 ms | 6480 KB | Output is correct |
5 | Correct | 2 ms | 6648 KB | Output is correct |
6 | Correct | 1 ms | 6480 KB | Output is correct |
7 | Correct | 1 ms | 6480 KB | Output is correct |
8 | Correct | 1 ms | 6480 KB | Output is correct |
9 | Correct | 1 ms | 6480 KB | Output is correct |
10 | Correct | 1 ms | 6480 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 6480 KB | Output is correct |
2 | Correct | 2 ms | 6648 KB | Output is correct |
3 | Correct | 1 ms | 6480 KB | Output is correct |
4 | Correct | 1 ms | 6480 KB | Output is correct |
5 | Correct | 2 ms | 6648 KB | Output is correct |
6 | Correct | 1 ms | 6480 KB | Output is correct |
7 | Correct | 1 ms | 6480 KB | Output is correct |
8 | Correct | 1 ms | 6480 KB | Output is correct |
9 | Correct | 1 ms | 6480 KB | Output is correct |
10 | Correct | 1 ms | 6480 KB | Output is correct |
11 | Correct | 2 ms | 6648 KB | Output is correct |
12 | Correct | 1 ms | 6480 KB | Output is correct |
13 | Correct | 2 ms | 6648 KB | Output is correct |
14 | Correct | 2 ms | 6480 KB | Output is correct |
15 | Correct | 2 ms | 6712 KB | Output is correct |
16 | Correct | 1 ms | 6480 KB | Output is correct |
17 | Correct | 1 ms | 6480 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 6480 KB | Output is correct |
2 | Correct | 2 ms | 6648 KB | Output is correct |
3 | Correct | 1 ms | 6480 KB | Output is correct |
4 | Correct | 1 ms | 6480 KB | Output is correct |
5 | Correct | 2 ms | 6648 KB | Output is correct |
6 | Correct | 1 ms | 6480 KB | Output is correct |
7 | Correct | 1 ms | 6480 KB | Output is correct |
8 | Correct | 1 ms | 6480 KB | Output is correct |
9 | Correct | 1 ms | 6480 KB | Output is correct |
10 | Correct | 1 ms | 6480 KB | Output is correct |
11 | Correct | 2 ms | 6648 KB | Output is correct |
12 | Correct | 1 ms | 6480 KB | Output is correct |
13 | Correct | 2 ms | 6648 KB | Output is correct |
14 | Correct | 2 ms | 6480 KB | Output is correct |
15 | Correct | 2 ms | 6712 KB | Output is correct |
16 | Correct | 1 ms | 6480 KB | Output is correct |
17 | Correct | 1 ms | 6480 KB | Output is correct |
18 | Correct | 3 ms | 7160 KB | Output is correct |
19 | Correct | 3 ms | 6736 KB | Output is correct |
20 | Correct | 3 ms | 6992 KB | Output is correct |
21 | Correct | 3 ms | 6736 KB | Output is correct |
22 | Correct | 3 ms | 6736 KB | Output is correct |
23 | Correct | 3 ms | 6736 KB | Output is correct |
24 | Correct | 3 ms | 6736 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 6480 KB | Output is correct |
2 | Correct | 2 ms | 6648 KB | Output is correct |
3 | Correct | 1 ms | 6480 KB | Output is correct |
4 | Correct | 1 ms | 6480 KB | Output is correct |
5 | Correct | 2 ms | 6648 KB | Output is correct |
6 | Correct | 1 ms | 6480 KB | Output is correct |
7 | Correct | 1 ms | 6480 KB | Output is correct |
8 | Correct | 1 ms | 6480 KB | Output is correct |
9 | Correct | 1 ms | 6480 KB | Output is correct |
10 | Correct | 1 ms | 6480 KB | Output is correct |
11 | Correct | 2 ms | 6648 KB | Output is correct |
12 | Correct | 1 ms | 6480 KB | Output is correct |
13 | Correct | 2 ms | 6648 KB | Output is correct |
14 | Correct | 2 ms | 6480 KB | Output is correct |
15 | Correct | 2 ms | 6712 KB | Output is correct |
16 | Correct | 1 ms | 6480 KB | Output is correct |
17 | Correct | 1 ms | 6480 KB | Output is correct |
18 | Correct | 3 ms | 7160 KB | Output is correct |
19 | Correct | 3 ms | 6736 KB | Output is correct |
20 | Correct | 3 ms | 6992 KB | Output is correct |
21 | Correct | 3 ms | 6736 KB | Output is correct |
22 | Correct | 3 ms | 6736 KB | Output is correct |
23 | Correct | 3 ms | 6736 KB | Output is correct |
24 | Correct | 3 ms | 6736 KB | Output is correct |
25 | Correct | 2 ms | 6480 KB | Output is correct |
26 | Incorrect | 3 ms | 6736 KB | Output isn't correct |
27 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 6480 KB | Output is correct |
2 | Correct | 2 ms | 6648 KB | Output is correct |
3 | Correct | 1 ms | 6480 KB | Output is correct |
4 | Correct | 1 ms | 6480 KB | Output is correct |
5 | Correct | 2 ms | 6648 KB | Output is correct |
6 | Correct | 1 ms | 6480 KB | Output is correct |
7 | Correct | 1 ms | 6480 KB | Output is correct |
8 | Correct | 1 ms | 6480 KB | Output is correct |
9 | Correct | 1 ms | 6480 KB | Output is correct |
10 | Correct | 1 ms | 6480 KB | Output is correct |
11 | Correct | 2 ms | 6648 KB | Output is correct |
12 | Correct | 1 ms | 6480 KB | Output is correct |
13 | Correct | 2 ms | 6648 KB | Output is correct |
14 | Correct | 2 ms | 6480 KB | Output is correct |
15 | Correct | 2 ms | 6712 KB | Output is correct |
16 | Correct | 1 ms | 6480 KB | Output is correct |
17 | Correct | 1 ms | 6480 KB | Output is correct |
18 | Correct | 3 ms | 7160 KB | Output is correct |
19 | Correct | 3 ms | 6736 KB | Output is correct |
20 | Correct | 3 ms | 6992 KB | Output is correct |
21 | Correct | 3 ms | 6736 KB | Output is correct |
22 | Correct | 3 ms | 6736 KB | Output is correct |
23 | Correct | 3 ms | 6736 KB | Output is correct |
24 | Correct | 3 ms | 6736 KB | Output is correct |
25 | Correct | 78 ms | 53072 KB | Output is correct |
26 | Correct | 74 ms | 53324 KB | Output is correct |
27 | Correct | 73 ms | 53428 KB | Output is correct |
28 | Correct | 70 ms | 44872 KB | Output is correct |
29 | Correct | 72 ms | 44884 KB | Output is correct |
30 | Correct | 91 ms | 43848 KB | Output is correct |
31 | Correct | 75 ms | 43844 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 6480 KB | Output is correct |
2 | Incorrect | 1 ms | 6480 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 6480 KB | Output is correct |
2 | Correct | 2 ms | 6648 KB | Output is correct |
3 | Correct | 1 ms | 6480 KB | Output is correct |
4 | Correct | 1 ms | 6480 KB | Output is correct |
5 | Correct | 2 ms | 6648 KB | Output is correct |
6 | Correct | 1 ms | 6480 KB | Output is correct |
7 | Correct | 1 ms | 6480 KB | Output is correct |
8 | Correct | 1 ms | 6480 KB | Output is correct |
9 | Correct | 1 ms | 6480 KB | Output is correct |
10 | Correct | 1 ms | 6480 KB | Output is correct |
11 | Correct | 2 ms | 6648 KB | Output is correct |
12 | Correct | 1 ms | 6480 KB | Output is correct |
13 | Correct | 2 ms | 6648 KB | Output is correct |
14 | Correct | 2 ms | 6480 KB | Output is correct |
15 | Correct | 2 ms | 6712 KB | Output is correct |
16 | Correct | 1 ms | 6480 KB | Output is correct |
17 | Correct | 1 ms | 6480 KB | Output is correct |
18 | Correct | 3 ms | 7160 KB | Output is correct |
19 | Correct | 3 ms | 6736 KB | Output is correct |
20 | Correct | 3 ms | 6992 KB | Output is correct |
21 | Correct | 3 ms | 6736 KB | Output is correct |
22 | Correct | 3 ms | 6736 KB | Output is correct |
23 | Correct | 3 ms | 6736 KB | Output is correct |
24 | Correct | 3 ms | 6736 KB | Output is correct |
25 | Correct | 2 ms | 6480 KB | Output is correct |
26 | Incorrect | 3 ms | 6736 KB | Output isn't correct |
27 | Halted | 0 ms | 0 KB | - |