#include <bits/stdc++.h>
using namespace std;
const int maxn = 5005;
const int inf = 1e9;
int n;
int a[maxn], positions[maxn], toRem[maxn];
int dp[maxn];
struct FenwickTree
{
int tree[maxn];
void update(int pos, int val)
{
for (; pos <= n; pos += (pos & (-pos)))
{
tree[pos] += val;
}
}
int query(int pos)
{
int result = 0;
for (; pos >= 1; pos -= (pos & (-pos)))
{
result += tree[pos];
}
return result;
}
};
FenwickTree bit;
void fastIO()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main()
{
fastIO();
cin >> n;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
positions[a[i]] = i;
}
dp[n + 1] = 0;
for (int num = n; num >= 1; --num)
{
dp[num] = inf;
for (int to = num; to <= n; ++to)
{
vector<int> toClear;
for (int j = to + 1; j <= n; ++j)
{
bit.update(positions[j], +1);
toClear.push_back(positions[j]);
}
for (int i = num; i <= to; ++i)
{
toRem[i] = bit.query(positions[i] - 1);
}
for (auto pos : toClear)
{
bit.update(pos, -1);
}
toClear.clear();
int cost = 0, inv = 0;
vector<pair<int, int>> actualPos;
for (int i = num; i <= to; ++i)
{
int newPos = positions[i] - toRem[i];
//cout << "here for " << i << " -> " << newPos << endl;
actualPos.push_back({newPos, i});
}
sort(actualPos.begin(), actualPos.end(), greater<pair<int, int>>());
for (int i = 0; i < actualPos.size(); ++i)
{
cost += abs((to - i) - actualPos[i].first);
}
for (int i = 0; i < actualPos.size(); ++i)
{
inv += bit.query(actualPos[i].second - 1);
bit.update(actualPos[i].second, +1);
toClear.push_back(actualPos[i].second);
}
for (auto pos : toClear)
{
bit.update(pos, -1);
}
int cntNums = to - num + 1;
cost += (cntNums * (cntNums - 1)) / 2 - inv;
//cout << num << " " << to << " :: " << cost << endl;
dp[num] = min(dp[num], cost + dp[to + 1]);
}
//cout << num << " ::: " << dp[num] << endl;
}
cout << dp[1] << endl;
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:99:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
99 | for (int i = 0; i < actualPos.size(); ++i)
| ~~^~~~~~~~~~~~~~~~~~
Main.cpp:104:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | for (int i = 0; i < actualPos.size(); ++i)
| ~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
508 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
508 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
508 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
504 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
508 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
508 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
504 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
88 ms |
472 KB |
Output is correct |
22 |
Correct |
83 ms |
336 KB |
Output is correct |
23 |
Correct |
81 ms |
336 KB |
Output is correct |
24 |
Correct |
81 ms |
336 KB |
Output is correct |
25 |
Correct |
82 ms |
336 KB |
Output is correct |
26 |
Correct |
83 ms |
472 KB |
Output is correct |
27 |
Correct |
87 ms |
336 KB |
Output is correct |
28 |
Correct |
82 ms |
508 KB |
Output is correct |
29 |
Correct |
82 ms |
336 KB |
Output is correct |
30 |
Correct |
83 ms |
476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
508 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
508 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
504 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
88 ms |
472 KB |
Output is correct |
22 |
Correct |
83 ms |
336 KB |
Output is correct |
23 |
Correct |
81 ms |
336 KB |
Output is correct |
24 |
Correct |
81 ms |
336 KB |
Output is correct |
25 |
Correct |
82 ms |
336 KB |
Output is correct |
26 |
Correct |
83 ms |
472 KB |
Output is correct |
27 |
Correct |
87 ms |
336 KB |
Output is correct |
28 |
Correct |
82 ms |
508 KB |
Output is correct |
29 |
Correct |
82 ms |
336 KB |
Output is correct |
30 |
Correct |
83 ms |
476 KB |
Output is correct |
31 |
Execution timed out |
5048 ms |
496 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
508 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
504 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
508 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
504 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
88 ms |
472 KB |
Output is correct |
22 |
Correct |
83 ms |
336 KB |
Output is correct |
23 |
Correct |
81 ms |
336 KB |
Output is correct |
24 |
Correct |
81 ms |
336 KB |
Output is correct |
25 |
Correct |
82 ms |
336 KB |
Output is correct |
26 |
Correct |
83 ms |
472 KB |
Output is correct |
27 |
Correct |
87 ms |
336 KB |
Output is correct |
28 |
Correct |
82 ms |
508 KB |
Output is correct |
29 |
Correct |
82 ms |
336 KB |
Output is correct |
30 |
Correct |
83 ms |
476 KB |
Output is correct |
31 |
Execution timed out |
5048 ms |
496 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |