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>
using namespace std;
#define int long long
#define INF (int)1e18
#define f first
#define s second
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
const int N = 5005;
int m, f[N][2];
void upd(int x, int j, int v = 1){
for (int i = x; i <= m; i += i & (-i))
f[i][j] += v;
}
int query(int x, int j){
int res = 0;
for (int i = x; i; i -= i & (-i))
res += f[i][j];
return res;
}
void Solve()
{
int n; cin >> n;
m = n;
vector <int> a(n + 1), b(n + 1);
for (int i = 1; i <= n; i++){
cin >> a[i]; b[a[i]] = i;
}
vector<vector<int>> c(n + 1, vector<int>(n + 1));
for (int i = 1; i <= n; i++){
for (int i = 1; i <= m; i++) f[i][0] = f[i][1] = 0;
int val = 0;
for (int j = i; j >= 1; j--){
upd(b[j], 0);
}
for (int j = i; j >= 1; j--){
val += query(m, 0) - query(b[j], 0);
val -= query(b[j], 1);
// upd(b[j], 0, -1);
upd(b[j], 1);
c[j][i] = val;
// cout << j << " " << i << " " << val << "\n";
}
}
vector <int> dp(n + 1, INF);
dp[0] = 0;
for (int i = 1; i <= n; i++){
for (int j = 0; j < i; j++){
dp[i] = min(dp[i], dp[j] + c[j + 1][i]);
}
}
cout << dp[n] << "\n";
}
int32_t main()
{
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// freopen("in", "r", stdin);
// freopen("out", "w", stdout);
// cin >> t;
for(int i = 1; i <= t; i++)
{
//cout << "Case #" << i << ": ";
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\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... |