# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1116419 | ntdaccode | Cat Exercise (JOI23_ho_t4) | C++17 | 91 ms | 53428 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |