#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define rep(n) rep1(i,n)
#define rep1(i,n) rep2(i,0,n)
#define rep2(i,a,b) for(int i=a; i<(b); ++i)
#define rep3(i,a,b) for(int i=a; i>=(b); --i)
#define pii pair<int,int>
#define pb push_back
#define all(a) a.begin(),a.end()
#define sz(a) ((int)a.size())
#define pow2(x) (1ll<<(x))
#define inf 1000'000'000'000'000'000
#ifdef i_am_noob
#define bug(...) cerr << "#" << __LINE__ << ": " << #__VA_ARGS__ << "- ",_do(__VA_ARGS__)
template<typename T> void _do(T x){cerr << x << endl;}
template<typename T, typename ... S> void _do(T x, S ...y){cerr << x << ", ";_do(y...);}
#else
#define bug(...) 49
#endif
const int maxn=5005;
int n,a[maxn],dp[maxn],pos[maxn];
int bit[maxn];
void modify(int p, int x){for(int i=p+1; i<maxn; i+=i&-i) bit[i]+=x;}
int query(int r){
int res=0;
for(int i=r+1; i; i-=i&-i) res+=bit[i];
return res;
}
signed main(){
ios_base::sync_with_stdio(0),cin.tie(0);
cin >> n;
rep(n) cin >> a[i];
rep(n) a[i]--;
rep2(i,2,n+1){
vector<int> vec;
rep1(j,n) if(a[j]<i) vec.pb(j);
rep1(j,sz(vec)) pos[a[vec[j]]]=j;
//rep1(j,sz(vec)) bug(vec[j]);
memset(bit,0,sizeof bit);
int cur=0,sum=0;
dp[i]=inf;
rep3(j,i-1,0){
cur+=i-j-1-query(pos[j]);
sum+=pos[j]+1;
dp[i]=min(dp[i],dp[j]+cur+(i*(i+1)/2-j*(j+1)/2-sum));
modify(pos[j],1);
}
bug(dp[i]);
}
cout << dp[n] << endl;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:21:18: warning: statement has no effect [-Wunused-value]
21 | #define bug(...) 49
| ^~
Main.cpp:55:9: note: in expansion of macro 'bug'
55 | bug(dp[i]);
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
316 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
316 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
316 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
2 ms |
332 KB |
Output is correct |
28 |
Correct |
2 ms |
332 KB |
Output is correct |
29 |
Correct |
2 ms |
312 KB |
Output is correct |
30 |
Correct |
2 ms |
372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
316 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
316 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
2 ms |
332 KB |
Output is correct |
28 |
Correct |
2 ms |
332 KB |
Output is correct |
29 |
Correct |
2 ms |
312 KB |
Output is correct |
30 |
Correct |
2 ms |
372 KB |
Output is correct |
31 |
Correct |
12 ms |
332 KB |
Output is correct |
32 |
Correct |
13 ms |
400 KB |
Output is correct |
33 |
Correct |
13 ms |
388 KB |
Output is correct |
34 |
Correct |
13 ms |
316 KB |
Output is correct |
35 |
Correct |
14 ms |
500 KB |
Output is correct |
36 |
Correct |
15 ms |
404 KB |
Output is correct |
37 |
Correct |
16 ms |
460 KB |
Output is correct |
38 |
Correct |
13 ms |
332 KB |
Output is correct |
39 |
Correct |
13 ms |
404 KB |
Output is correct |
40 |
Correct |
14 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
316 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
316 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
2 ms |
332 KB |
Output is correct |
28 |
Correct |
2 ms |
332 KB |
Output is correct |
29 |
Correct |
2 ms |
312 KB |
Output is correct |
30 |
Correct |
2 ms |
372 KB |
Output is correct |
31 |
Correct |
12 ms |
332 KB |
Output is correct |
32 |
Correct |
13 ms |
400 KB |
Output is correct |
33 |
Correct |
13 ms |
388 KB |
Output is correct |
34 |
Correct |
13 ms |
316 KB |
Output is correct |
35 |
Correct |
14 ms |
500 KB |
Output is correct |
36 |
Correct |
15 ms |
404 KB |
Output is correct |
37 |
Correct |
16 ms |
460 KB |
Output is correct |
38 |
Correct |
13 ms |
332 KB |
Output is correct |
39 |
Correct |
13 ms |
404 KB |
Output is correct |
40 |
Correct |
14 ms |
332 KB |
Output is correct |
41 |
Correct |
476 ms |
656 KB |
Output is correct |
42 |
Correct |
458 ms |
700 KB |
Output is correct |
43 |
Correct |
457 ms |
748 KB |
Output is correct |
44 |
Correct |
457 ms |
580 KB |
Output is correct |
45 |
Correct |
480 ms |
892 KB |
Output is correct |
46 |
Correct |
467 ms |
452 KB |
Output is correct |
47 |
Correct |
481 ms |
744 KB |
Output is correct |
48 |
Correct |
480 ms |
580 KB |
Output is correct |
49 |
Correct |
466 ms |
572 KB |
Output is correct |
50 |
Correct |
486 ms |
524 KB |
Output is correct |
51 |
Correct |
462 ms |
576 KB |
Output is correct |
52 |
Correct |
470 ms |
580 KB |
Output is correct |
53 |
Correct |
461 ms |
580 KB |
Output is correct |
54 |
Correct |
462 ms |
684 KB |
Output is correct |
55 |
Correct |
452 ms |
572 KB |
Output is correct |