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>
#define pb push_back
#define sp " "
#define sl "\n"
#define fi first
#define se second
#define mp make_pair
#define sz(a) ((int)a.size())
#define all(a) a.begin(),a.end()
#define rev(a) reverse(a.begin(),a.end())
#define PI 3.141592
#define fo(i,n) for (int i=0;i<n;i++)
#define fofo(i,n,j,m) for (int i=0;i<n;i++) for (int j=0;j<m;j++)
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
const int N=5e3+5,mod=1e9+7;
int n,a[N];
ll dp[N];
int st[N];
int g[N];
vi aux;
int tam;
void add(int u)
{
while (u<=tam) {
st[u]++;
u += u&(-u);
}
return;
}
int get(int u)
{
int r = 0;
while (u>0) {
r += st[u];
u -= u&(-u);
}
return r;
}
int main() {
cin.tie(0); cout.tie(0);
ios_base::sync_with_stdio();
cin >> n;
for (int i=0;i<n;i++) cin >> a[i];
dp[0] = dp[1] = 0;
for (int i=2;i<=n;i++) dp[i] = 1e15;
for (int i=0;i<n;i++) g[a[i]] = i;
for (int i=n;i>=1;i--) {
int p1,p2;
p2 = get(g[i]-1);
}
for (int i=2;i<=n;i++) {
aux.clear();
for (int j=0;j<n;j++) if (a[j]<=i) aux.push_back(a[j]);
tam = i;
int p=0;
long long res = 0;
for (int u : aux) g[u] = ++p;
for (int j=0;j<=n;j++) st[j] = 0;
for (int j=i;j>=1;j--) {
int k = get(g[j]-1);
res += i-g[j]-k;
dp[i] = min(dp[i],dp[j-1]+res);
add(g[j]);
}
//cout << dp[i] << " ";
}
cout << dp[n];
return 0;
}
// Recuerda limites
// Recuerda casos especiales
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:56:7: warning: unused variable 'p1' [-Wunused-variable]
56 | int p1,p2;
| ^~
Main.cpp:56:10: warning: variable 'p2' set but not used [-Wunused-but-set-variable]
56 | int p1,p2;
| ^~
# | 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... |