#include<iostream>
#include<stdio.h>
#include<stdint.h>
#include<vector>
#include<algorithm>
#include<utility>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<deque>
#include<string>
#include<assert.h>
#include<math.h>
#include<chrono>
#include<random>
#include<bitset>
using namespace std;
const int64_t INF = (int64_t) 1e9 + 5;
const int64_t mINF = (int64_t) 4 * 1e4;
const int64_t MOD = (int64_t) 1e9 + 7;
const int nbit = 30;
const int ndig = 10;
const int nchar = 26;
const int p1 = 31;
const int p2 = 53;
const int D = 4;
int dr[D] = {0, 1, 0, -1};
int dc[D] = {1, 0, -1, 0};
// 0 right // 1 down // 2 left // 3 up
struct Solution
{
int n;
Solution() {}
void solve()
{
cin >> n;
vector<int> pos(n, -1);
for(int i = 1; i <= n; i++)
{
int x;
cin >> x;
pos[x - 1] = i;
}
vector<vector<int> > cost(n, vector<int>(n, 0));
vector<int> bit(n + 1, 0);
for(int i = 0; i < n; i++)
{
update(pos[i], 1, bit);
}
for(int j = 0; j < n; j++)
{
update(pos[j], -1, bit);
for(int i = j; i >= 0; i--)
{
if(i != j) cost[i][j] += cost[i + 1][j];
cost[i][j] += sum(pos[i] - 1, bit);
}
}
for(int i = 0; i < n; i++)
{
vector<int> bit2(n + 1, 0);
int x = 0;
for(int j = i; j < n; j++)
{
x += sum(pos[j], bit2);
cost[i][j] += x;
update(pos[j], 1, bit2);
}
}
vector<int> dp(n + 1, INF);
dp[0] = 0;
for(int i = 1; i <= n; i++)
{
for(int j = i; j <= n; j++)
{
dp[j] = min(dp[j], dp[i - 1] + cost[i - 1][j - 1]);
}
}
cout << dp[n] << "\n";
}
int sum(int i, vector<int>& bit)
{
int ans = 0;
while(i > 0)
{
ans += bit[i];
i -= (i & (-i));
}
return ans;
}
void update(int i, int val, vector<int>& bit)
{
while(i < (int) bit.size())
{
bit[i] += val;
i += (i & (-i));
}
}
int modsub(int t1, int t2)
{
int64_t res = t1 - t2;
if(res < 0) res += MOD;
return res;
}
int modadd(int t1, int t2)
{
int64_t res = t1 + t2;
if(res >= MOD) res -= MOD;
return res;
}
int modmul(int t1, int t2)
{
int64_t res = 1LL * t1 * t2;
return (res % MOD);
}
bool BIT(int mask, int j)
{
return (mask & MASK(j));
}
int64_t MASK(int j)
{
return (1LL << j);
}
int64_t Abs(int64_t t1)
{
if(t1 < 0) return -t1;
return t1;
}
};
void __startup()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
}
int main()
{
__startup();
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
Solution().solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
316 KB |
Output is correct |
12 |
Correct |
0 ms |
320 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
276 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
316 KB |
Output is correct |
12 |
Correct |
0 ms |
320 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
276 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
448 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
452 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
444 KB |
Output is correct |
28 |
Correct |
1 ms |
448 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
316 KB |
Output is correct |
12 |
Correct |
0 ms |
320 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
276 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
448 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
452 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
444 KB |
Output is correct |
28 |
Correct |
1 ms |
448 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
9 ms |
2748 KB |
Output is correct |
32 |
Correct |
10 ms |
2772 KB |
Output is correct |
33 |
Correct |
9 ms |
2772 KB |
Output is correct |
34 |
Correct |
9 ms |
2748 KB |
Output is correct |
35 |
Correct |
9 ms |
2864 KB |
Output is correct |
36 |
Correct |
9 ms |
2772 KB |
Output is correct |
37 |
Correct |
11 ms |
2772 KB |
Output is correct |
38 |
Correct |
9 ms |
2748 KB |
Output is correct |
39 |
Correct |
9 ms |
2756 KB |
Output is correct |
40 |
Correct |
9 ms |
2760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
316 KB |
Output is correct |
12 |
Correct |
0 ms |
320 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
276 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
448 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
452 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
444 KB |
Output is correct |
28 |
Correct |
1 ms |
448 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
9 ms |
2748 KB |
Output is correct |
32 |
Correct |
10 ms |
2772 KB |
Output is correct |
33 |
Correct |
9 ms |
2772 KB |
Output is correct |
34 |
Correct |
9 ms |
2748 KB |
Output is correct |
35 |
Correct |
9 ms |
2864 KB |
Output is correct |
36 |
Correct |
9 ms |
2772 KB |
Output is correct |
37 |
Correct |
11 ms |
2772 KB |
Output is correct |
38 |
Correct |
9 ms |
2748 KB |
Output is correct |
39 |
Correct |
9 ms |
2756 KB |
Output is correct |
40 |
Correct |
9 ms |
2760 KB |
Output is correct |
41 |
Correct |
541 ms |
98284 KB |
Output is correct |
42 |
Correct |
507 ms |
98324 KB |
Output is correct |
43 |
Correct |
506 ms |
98340 KB |
Output is correct |
44 |
Correct |
538 ms |
98380 KB |
Output is correct |
45 |
Correct |
532 ms |
98408 KB |
Output is correct |
46 |
Correct |
507 ms |
98380 KB |
Output is correct |
47 |
Correct |
543 ms |
98484 KB |
Output is correct |
48 |
Correct |
533 ms |
98432 KB |
Output is correct |
49 |
Correct |
518 ms |
98388 KB |
Output is correct |
50 |
Correct |
529 ms |
98428 KB |
Output is correct |
51 |
Correct |
519 ms |
98532 KB |
Output is correct |
52 |
Correct |
550 ms |
98380 KB |
Output is correct |
53 |
Correct |
525 ms |
98428 KB |
Output is correct |
54 |
Correct |
539 ms |
98408 KB |
Output is correct |
55 |
Correct |
509 ms |
98408 KB |
Output is correct |