이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
const int N = 5e3 + 5;
const ll mod = 1e12 + 7;
ll n, m, a[N], b[N], x[N], dp[N], inv[N][N], tong;
void sol()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
b[a[i]] = i;
}
for (int i = 1; i <= n; i++)
{
tong = 0;
for (int j = i - 1; j > 0; j--)
{
if (b[i] < b[j])
++tong;
inv[i][j] = inv[i - 1][j] + tong;
}
}
for (int i = 2; i <= n; i++)
{
dp[i] = mod;
for (int j = i - 1; j >= 0; j--)
{
dp[i] = min(dp[i], dp[j] + inv[i][1] - inv[j][1] + (i - j) * (i - j - 1) / 2 - inv[i][j + 1] * 2);
}
//cout << dp[i] << " ";
}
cout << dp[n];
}
int main()
{
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
int test = 1;
//cin >> test;
while (test-- > 0)
sol();
return 0;
}
/*
5
2 1 3 5 4
*/
// ctrl + F8 = compile
// ctrl + F9 = run
# | 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... |