#include<bits/stdc++.h>
using namespace std;
#define ll long long
#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 |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2552 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2648 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 |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2552 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2648 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 |
2396 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 |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2552 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2648 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 |
2396 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 |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
5 ms |
3416 KB |
Output is correct |
22 |
Correct |
5 ms |
3416 KB |
Output is correct |
23 |
Correct |
5 ms |
3416 KB |
Output is correct |
24 |
Correct |
5 ms |
3416 KB |
Output is correct |
25 |
Correct |
5 ms |
3416 KB |
Output is correct |
26 |
Correct |
5 ms |
3416 KB |
Output is correct |
27 |
Correct |
5 ms |
3420 KB |
Output is correct |
28 |
Correct |
5 ms |
3416 KB |
Output is correct |
29 |
Correct |
6 ms |
3416 KB |
Output is correct |
30 |
Correct |
5 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2552 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2648 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 |
2396 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 |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
5 ms |
3416 KB |
Output is correct |
22 |
Correct |
5 ms |
3416 KB |
Output is correct |
23 |
Correct |
5 ms |
3416 KB |
Output is correct |
24 |
Correct |
5 ms |
3416 KB |
Output is correct |
25 |
Correct |
5 ms |
3416 KB |
Output is correct |
26 |
Correct |
5 ms |
3416 KB |
Output is correct |
27 |
Correct |
5 ms |
3420 KB |
Output is correct |
28 |
Correct |
5 ms |
3416 KB |
Output is correct |
29 |
Correct |
6 ms |
3416 KB |
Output is correct |
30 |
Correct |
5 ms |
3420 KB |
Output is correct |
31 |
Correct |
87 ms |
11604 KB |
Output is correct |
32 |
Correct |
91 ms |
11584 KB |
Output is correct |
33 |
Correct |
89 ms |
11724 KB |
Output is correct |
34 |
Correct |
89 ms |
11600 KB |
Output is correct |
35 |
Correct |
91 ms |
11600 KB |
Output is correct |
36 |
Correct |
89 ms |
11692 KB |
Output is correct |
37 |
Correct |
89 ms |
11616 KB |
Output is correct |
38 |
Correct |
92 ms |
11636 KB |
Output is correct |
39 |
Correct |
89 ms |
11596 KB |
Output is correct |
40 |
Correct |
88 ms |
11600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2552 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2648 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 |
2396 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 |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
5 ms |
3416 KB |
Output is correct |
22 |
Correct |
5 ms |
3416 KB |
Output is correct |
23 |
Correct |
5 ms |
3416 KB |
Output is correct |
24 |
Correct |
5 ms |
3416 KB |
Output is correct |
25 |
Correct |
5 ms |
3416 KB |
Output is correct |
26 |
Correct |
5 ms |
3416 KB |
Output is correct |
27 |
Correct |
5 ms |
3420 KB |
Output is correct |
28 |
Correct |
5 ms |
3416 KB |
Output is correct |
29 |
Correct |
6 ms |
3416 KB |
Output is correct |
30 |
Correct |
5 ms |
3420 KB |
Output is correct |
31 |
Correct |
87 ms |
11604 KB |
Output is correct |
32 |
Correct |
91 ms |
11584 KB |
Output is correct |
33 |
Correct |
89 ms |
11724 KB |
Output is correct |
34 |
Correct |
89 ms |
11600 KB |
Output is correct |
35 |
Correct |
91 ms |
11600 KB |
Output is correct |
36 |
Correct |
89 ms |
11692 KB |
Output is correct |
37 |
Correct |
89 ms |
11616 KB |
Output is correct |
38 |
Correct |
92 ms |
11636 KB |
Output is correct |
39 |
Correct |
89 ms |
11596 KB |
Output is correct |
40 |
Correct |
88 ms |
11600 KB |
Output is correct |
41 |
Correct |
4722 ms |
144152 KB |
Output is correct |
42 |
Correct |
4717 ms |
144368 KB |
Output is correct |
43 |
Correct |
4720 ms |
144284 KB |
Output is correct |
44 |
Correct |
4687 ms |
144356 KB |
Output is correct |
45 |
Correct |
4722 ms |
144084 KB |
Output is correct |
46 |
Correct |
4741 ms |
144272 KB |
Output is correct |
47 |
Correct |
4711 ms |
144276 KB |
Output is correct |
48 |
Correct |
4679 ms |
144388 KB |
Output is correct |
49 |
Correct |
4740 ms |
144376 KB |
Output is correct |
50 |
Correct |
4748 ms |
144344 KB |
Output is correct |
51 |
Correct |
4734 ms |
144052 KB |
Output is correct |
52 |
Correct |
4722 ms |
144080 KB |
Output is correct |
53 |
Correct |
4696 ms |
144032 KB |
Output is correct |
54 |
Correct |
4820 ms |
144196 KB |
Output is correct |
55 |
Correct |
4718 ms |
144240 KB |
Output is correct |