#include<bits/stdc++.h>
using namespace std;
#define ll int
#define fi first
#define se second
#define p(x,y) pair<ll,ll>(x,y)
#define BIT(i,x) ((x>>i)&1)
#define MASK(x) (1<<x)
#define ld long double
#define __builtin_popcount __builtin_popcountll
#define pll pair<ll,ll>
template<class T1,class T2>
bool maximize(T1 &x,const T2 &y)
{
if(x<y)
{
x=y;
return 1;
}
return 0;
}
template<class T1,class T2>
bool minimize(T1 &x,const T2 &y)
{
if(x>y)
{
x=y;
return 1;
}
return 0;
}
void online()
{
std::ios_base::sync_with_stdio(0);
cin.tie(0);
#ifdef thuc
freopen("input.INP","r",stdin);
freopen("output.OUT","w",stdout);
#endif
}
const int N=5e3+10,M=1e9+7;
struct Node
{
ll l,r,vl;
}Tree[22*N];
ll cnt;
vector<ll> Head;
ll n,a[N],pos[N];
ll make(ll x,ll y,ll z)
{
Tree[++cnt].l=x;
Tree[cnt].r=y;
Tree[cnt].vl=z;
return cnt;
}
ll build(ll l,ll r)
{
if(l==r) return make(0,0,0);
return make(build(l,(l+r)/2),build((l+r)/2+1,r),0);
}
ll update(ll i,ll l,ll r,ll pos,ll vl)
{
if(r<pos||pos<l) return i;
if(l==r) return make(0,0,vl);
ll x=update(Tree[i].l,l,(l+r)/2,pos,vl);
ll y=update(Tree[i].r,(l+r)/2+1,r,pos,vl);
return make(x,y,Tree[x].vl+Tree[y].vl);
}
ll query(ll i,ll l,ll r,ll l1,ll r1)
{
if(r1<l||r<l1) return 0;
if(l1<=l&&r<=r1) return Tree[i].vl;
return query(Tree[i].l,l,(l+r)/2,l1,r1)+query(Tree[i].r,(l+r)/2+1,r,l1,r1);
}
ll query(ll l,ll r,ll l1,ll r1)
{
if(l>r) return 0;
return query(Head[r],1,n,l1,r1)-query(Head[l-1],1,n,l1,r1);
}
ll c[N][N],f[N];
int main()
{
online();
cin>>n;
Head.push_back(build(1,n));
for(int i=1;i<=n;i++)
{
cin>>a[i];
pos[a[i]]=i;
Head.push_back(update(Head.back(),1,n,a[i],1));
}
memset(f,0x3f,sizeof f);
f[0]=0;
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
c[i][j]=c[i][j-1];
c[i][j]+=query(1,pos[j]-1,i,n);
c[i][j]-=query(pos[j],n,i,j-1);
minimize(f[j],f[i-1]+c[i][j]);
}
}
cout<<f[n];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2596 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2596 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
1 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2648 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2596 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
1 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2648 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
6 ms |
3416 KB |
Output is correct |
22 |
Correct |
5 ms |
3420 KB |
Output is correct |
23 |
Correct |
5 ms |
3416 KB |
Output is correct |
24 |
Correct |
5 ms |
3416 KB |
Output is correct |
25 |
Correct |
6 ms |
3416 KB |
Output is correct |
26 |
Correct |
5 ms |
3420 KB |
Output is correct |
27 |
Correct |
5 ms |
3416 KB |
Output is correct |
28 |
Correct |
5 ms |
3416 KB |
Output is correct |
29 |
Correct |
5 ms |
3416 KB |
Output is correct |
30 |
Correct |
5 ms |
3424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2596 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
1 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2648 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
6 ms |
3416 KB |
Output is correct |
22 |
Correct |
5 ms |
3420 KB |
Output is correct |
23 |
Correct |
5 ms |
3416 KB |
Output is correct |
24 |
Correct |
5 ms |
3416 KB |
Output is correct |
25 |
Correct |
6 ms |
3416 KB |
Output is correct |
26 |
Correct |
5 ms |
3420 KB |
Output is correct |
27 |
Correct |
5 ms |
3416 KB |
Output is correct |
28 |
Correct |
5 ms |
3416 KB |
Output is correct |
29 |
Correct |
5 ms |
3416 KB |
Output is correct |
30 |
Correct |
5 ms |
3424 KB |
Output is correct |
31 |
Correct |
90 ms |
9552 KB |
Output is correct |
32 |
Correct |
90 ms |
9552 KB |
Output is correct |
33 |
Correct |
90 ms |
9528 KB |
Output is correct |
34 |
Correct |
92 ms |
9560 KB |
Output is correct |
35 |
Correct |
90 ms |
9760 KB |
Output is correct |
36 |
Correct |
90 ms |
9556 KB |
Output is correct |
37 |
Correct |
90 ms |
9552 KB |
Output is correct |
38 |
Correct |
91 ms |
9552 KB |
Output is correct |
39 |
Correct |
98 ms |
9552 KB |
Output is correct |
40 |
Correct |
90 ms |
9552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2596 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
1 ms |
2392 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2648 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
6 ms |
3416 KB |
Output is correct |
22 |
Correct |
5 ms |
3420 KB |
Output is correct |
23 |
Correct |
5 ms |
3416 KB |
Output is correct |
24 |
Correct |
5 ms |
3416 KB |
Output is correct |
25 |
Correct |
6 ms |
3416 KB |
Output is correct |
26 |
Correct |
5 ms |
3420 KB |
Output is correct |
27 |
Correct |
5 ms |
3416 KB |
Output is correct |
28 |
Correct |
5 ms |
3416 KB |
Output is correct |
29 |
Correct |
5 ms |
3416 KB |
Output is correct |
30 |
Correct |
5 ms |
3424 KB |
Output is correct |
31 |
Correct |
90 ms |
9552 KB |
Output is correct |
32 |
Correct |
90 ms |
9552 KB |
Output is correct |
33 |
Correct |
90 ms |
9528 KB |
Output is correct |
34 |
Correct |
92 ms |
9560 KB |
Output is correct |
35 |
Correct |
90 ms |
9760 KB |
Output is correct |
36 |
Correct |
90 ms |
9556 KB |
Output is correct |
37 |
Correct |
90 ms |
9552 KB |
Output is correct |
38 |
Correct |
91 ms |
9552 KB |
Output is correct |
39 |
Correct |
98 ms |
9552 KB |
Output is correct |
40 |
Correct |
90 ms |
9552 KB |
Output is correct |
41 |
Correct |
4728 ms |
76992 KB |
Output is correct |
42 |
Correct |
4623 ms |
77108 KB |
Output is correct |
43 |
Correct |
4619 ms |
77072 KB |
Output is correct |
44 |
Correct |
4665 ms |
77344 KB |
Output is correct |
45 |
Correct |
4735 ms |
77516 KB |
Output is correct |
46 |
Correct |
4614 ms |
76996 KB |
Output is correct |
47 |
Correct |
4664 ms |
76968 KB |
Output is correct |
48 |
Correct |
4637 ms |
77076 KB |
Output is correct |
49 |
Correct |
4674 ms |
76996 KB |
Output is correct |
50 |
Correct |
4687 ms |
77012 KB |
Output is correct |
51 |
Correct |
4671 ms |
77084 KB |
Output is correct |
52 |
Correct |
4686 ms |
76964 KB |
Output is correct |
53 |
Correct |
4638 ms |
76960 KB |
Output is correct |
54 |
Correct |
4748 ms |
76992 KB |
Output is correct |
55 |
Correct |
4673 ms |
77020 KB |
Output is correct |