This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
//ako ovaj vaso daso misli da me pobedjuje.....
using namespace std;
#define int long long
#define f first
#define s second
const int N = 5010;
int dp[N], a[N], n, cost[N][N], fenw[N], pos[N];
//cost[i][j] -> prvih i je reseno i ja pocinjem od j
//prvi broj je i + 1
void add(int v, int val) {
for (int i = v;i < N;i += (i & -i))
fenw[i] += val;
}
int get(int v) {
int res = 0;
for (int i = v;i > 0;i -= (i & -i))
res += fenw[i];
return res;
}
void solve() {
for (int i = 1;i <= n;i++) {
int s = 0;
vector<int> p(n + 1);
for (int j = n;j >= 1;j--) {
if (a[j] < i)
s++;
else
p[a[j]] = pos[a[j]] + s;
}
for (int j = i;j <= n;j++) {
cost[i][j] = cost[i][j - 1] + p[j] - i - get(p[j]);
add(1, 1);
add(p[j] + 1, -1);
}
for (auto &i : fenw)
i = 0;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt = 1;
//cin >> tt;
while (tt--) {
cin >> n;
for (int i = 1;i <= n;i++) {
dp[i] = 1e9;
cin >> a[i];
pos[a[i]] = i;
}
solve();
for (int i = 1;i <= n;i++) {
for (int j = i - 1;j >= 0;j--)
dp[i] = min(dp[i], dp[j] + cost[j + 1][i]);
}
cout << dp[n];
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |