#include <bits/stdc++.h>
#define pb push_back
#define fst first
#define snd second
#define fore(i,a,b) for(ll i=a,ggdem=b;i<ggdem;++i)
#define SZ(x) ((int)x.size())
#define ALL(x) x.begin(),x.end()
#define mset(a,v) memset((a),(v),sizeof(a))
#define FIN ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define imp(v) for(auto messi:v)cout<<messi<<" "; cout<<"\n"
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
const ll MAXN=2e5+5;
const ll K=18;
ll st[K][1ll<<K];
ll a[MAXN];
ll n;
ll oper(ll i, ll j){
//if(i==-1)return j;
//if(j==-1)return i;
if(a[i]>a[j])return i;
return j;
}
void st_init(){
fore(i,0,n)st[0][i]=i;
fore(k,1,K)fore(i,0,n-(1ll<<k)+1){
//if(i+(1ll<<k-1)>=n)break;
st[k][i]=oper(st[k-1][i],st[k-1][i+(1ll<<(k-1))]);
}
}
ll st_query(ll s, ll e){
if(e-s==0)return -1;
ll k=63-__builtin_clzll(e-s);
return oper(st[k][s],st[k][e-(1ll<<k)]);
}
ll dp[MAXN];
ll f(ll l, ll r){
ll k=st_query(l,r);
ll &res=dp[k];
if(res!=-1)return res;
if(r-l==0)return res=1;
ll k1=st_query(l,k),k2=st_query(k+1,r);
res=0;
if(k-l)res=max(res,abs(k1-k)+f(l,k));
if(r-k-1)res=max(res,abs(k2-k)+f(k+1,r));
//cout<<l<<","<<r<<" "<<k<<": "<<res<<"\n";
return res;
}
int main(){FIN;
cin>>n;
fore(i,0,n)cin>>a[i],a[i]--;
st_init();
mset(dp,-1);
cout<<f(0,n)<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
12892 KB |
Output is correct |
5 |
Correct |
2 ms |
12764 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
1 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
12892 KB |
Output is correct |
9 |
Correct |
2 ms |
12892 KB |
Output is correct |
10 |
Correct |
2 ms |
12892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
12892 KB |
Output is correct |
5 |
Correct |
2 ms |
12764 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
1 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
12892 KB |
Output is correct |
9 |
Correct |
2 ms |
12892 KB |
Output is correct |
10 |
Correct |
2 ms |
12892 KB |
Output is correct |
11 |
Correct |
3 ms |
21084 KB |
Output is correct |
12 |
Correct |
3 ms |
21084 KB |
Output is correct |
13 |
Correct |
3 ms |
21084 KB |
Output is correct |
14 |
Correct |
3 ms |
21084 KB |
Output is correct |
15 |
Correct |
3 ms |
21080 KB |
Output is correct |
16 |
Correct |
3 ms |
21084 KB |
Output is correct |
17 |
Correct |
3 ms |
21084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
12892 KB |
Output is correct |
5 |
Correct |
2 ms |
12764 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
1 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
12892 KB |
Output is correct |
9 |
Correct |
2 ms |
12892 KB |
Output is correct |
10 |
Correct |
2 ms |
12892 KB |
Output is correct |
11 |
Correct |
3 ms |
21084 KB |
Output is correct |
12 |
Correct |
3 ms |
21084 KB |
Output is correct |
13 |
Correct |
3 ms |
21084 KB |
Output is correct |
14 |
Correct |
3 ms |
21084 KB |
Output is correct |
15 |
Correct |
3 ms |
21080 KB |
Output is correct |
16 |
Correct |
3 ms |
21084 KB |
Output is correct |
17 |
Correct |
3 ms |
21084 KB |
Output is correct |
18 |
Correct |
4 ms |
29740 KB |
Output is correct |
19 |
Correct |
4 ms |
29532 KB |
Output is correct |
20 |
Correct |
5 ms |
29508 KB |
Output is correct |
21 |
Correct |
4 ms |
29528 KB |
Output is correct |
22 |
Correct |
4 ms |
29272 KB |
Output is correct |
23 |
Correct |
4 ms |
29276 KB |
Output is correct |
24 |
Correct |
4 ms |
29276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
12892 KB |
Output is correct |
5 |
Correct |
2 ms |
12764 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
1 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
12892 KB |
Output is correct |
9 |
Correct |
2 ms |
12892 KB |
Output is correct |
10 |
Correct |
2 ms |
12892 KB |
Output is correct |
11 |
Correct |
3 ms |
21084 KB |
Output is correct |
12 |
Correct |
3 ms |
21084 KB |
Output is correct |
13 |
Correct |
3 ms |
21084 KB |
Output is correct |
14 |
Correct |
3 ms |
21084 KB |
Output is correct |
15 |
Correct |
3 ms |
21080 KB |
Output is correct |
16 |
Correct |
3 ms |
21084 KB |
Output is correct |
17 |
Correct |
3 ms |
21084 KB |
Output is correct |
18 |
Correct |
4 ms |
29740 KB |
Output is correct |
19 |
Correct |
4 ms |
29532 KB |
Output is correct |
20 |
Correct |
5 ms |
29508 KB |
Output is correct |
21 |
Correct |
4 ms |
29528 KB |
Output is correct |
22 |
Correct |
4 ms |
29272 KB |
Output is correct |
23 |
Correct |
4 ms |
29276 KB |
Output is correct |
24 |
Correct |
4 ms |
29276 KB |
Output is correct |
25 |
Correct |
1 ms |
8796 KB |
Output is correct |
26 |
Incorrect |
4 ms |
29356 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
12892 KB |
Output is correct |
5 |
Correct |
2 ms |
12764 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
1 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
12892 KB |
Output is correct |
9 |
Correct |
2 ms |
12892 KB |
Output is correct |
10 |
Correct |
2 ms |
12892 KB |
Output is correct |
11 |
Correct |
3 ms |
21084 KB |
Output is correct |
12 |
Correct |
3 ms |
21084 KB |
Output is correct |
13 |
Correct |
3 ms |
21084 KB |
Output is correct |
14 |
Correct |
3 ms |
21084 KB |
Output is correct |
15 |
Correct |
3 ms |
21080 KB |
Output is correct |
16 |
Correct |
3 ms |
21084 KB |
Output is correct |
17 |
Correct |
3 ms |
21084 KB |
Output is correct |
18 |
Correct |
4 ms |
29740 KB |
Output is correct |
19 |
Correct |
4 ms |
29532 KB |
Output is correct |
20 |
Correct |
5 ms |
29508 KB |
Output is correct |
21 |
Correct |
4 ms |
29528 KB |
Output is correct |
22 |
Correct |
4 ms |
29272 KB |
Output is correct |
23 |
Correct |
4 ms |
29276 KB |
Output is correct |
24 |
Correct |
4 ms |
29276 KB |
Output is correct |
25 |
Correct |
31 ms |
53316 KB |
Output is correct |
26 |
Correct |
33 ms |
52280 KB |
Output is correct |
27 |
Correct |
29 ms |
52308 KB |
Output is correct |
28 |
Correct |
25 ms |
40780 KB |
Output is correct |
29 |
Correct |
25 ms |
40784 KB |
Output is correct |
30 |
Correct |
25 ms |
40796 KB |
Output is correct |
31 |
Correct |
25 ms |
40796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Incorrect |
2 ms |
19036 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
1 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
12892 KB |
Output is correct |
5 |
Correct |
2 ms |
12764 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
1 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
12892 KB |
Output is correct |
9 |
Correct |
2 ms |
12892 KB |
Output is correct |
10 |
Correct |
2 ms |
12892 KB |
Output is correct |
11 |
Correct |
3 ms |
21084 KB |
Output is correct |
12 |
Correct |
3 ms |
21084 KB |
Output is correct |
13 |
Correct |
3 ms |
21084 KB |
Output is correct |
14 |
Correct |
3 ms |
21084 KB |
Output is correct |
15 |
Correct |
3 ms |
21080 KB |
Output is correct |
16 |
Correct |
3 ms |
21084 KB |
Output is correct |
17 |
Correct |
3 ms |
21084 KB |
Output is correct |
18 |
Correct |
4 ms |
29740 KB |
Output is correct |
19 |
Correct |
4 ms |
29532 KB |
Output is correct |
20 |
Correct |
5 ms |
29508 KB |
Output is correct |
21 |
Correct |
4 ms |
29528 KB |
Output is correct |
22 |
Correct |
4 ms |
29272 KB |
Output is correct |
23 |
Correct |
4 ms |
29276 KB |
Output is correct |
24 |
Correct |
4 ms |
29276 KB |
Output is correct |
25 |
Correct |
1 ms |
8796 KB |
Output is correct |
26 |
Incorrect |
4 ms |
29356 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |