#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 5005;
int dp[MAXN];
int32_t main() {
int n;
cin >> n;
vector<int> v(n + 1);
for (int i = 1; i <= n; i++) {
cin >> v[i];
}
dp[0] = 0;
for (int k = 1; k <= n; k++) {
//cout << "K is " << k << "\n";
dp[k] = INF;
vector<int> nv;
vector<int> po(k + 1);
for (int i = 1; i <= n; i++) {
if (v[i] <= k) {
po[v[i]] = nv.size();
nv.push_back(v[i]);
}
}
priority_queue<int> neg;
int pos = 0;
int calc = 0;
int t = 0;
for (int i = k; i >= 1; i--) {
calc -= ((int)neg.size());
calc += pos;
calc += abs(po[i] - k + 1);
dp[k] = min(dp[k], dp[i - 1] + calc);
while ((!neg.empty()) && (neg.top() == t)) {
//cout << "removed " << neg.top() << "\n";
neg.pop();
pos++;
}
if ((po[i] - k + 1) < 0) {
neg.push(po[i] - k + 1 + t);
//cout << "added from " << i << " it " << po[i] - k + 1 + t << "\n";
} else pos++;
t--;
}
//cout << dp[k] << "\n";
}
cout << dp[n] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
440 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
440 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
440 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
440 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
440 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |