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;
typedef long long ll;
const ll INF = 1e18+7;
const ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ll,ii>
#define ull unsigned ll
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///Training
int main(void){
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n;
cin>>n;
ll arr[n];
ll pre[n+1][n+1][3];
ll pos[n+1];
f(i,0,n){
cin>>arr[i];
pos[arr[i]] = i;
}
f(i,0,n+1){
f(j,0,n+1){
pre[i][j][0] = pre[i][j][1] = pre[i][j][2] = 0;
}
}
f(i,1,n+1){
f(j,i+1,n+1){
pre[i][j][1] = pre[i][j-1][1] + (pos[i] > pos[j]);
}
}
f(j,1,n+1){
f(i,1,j+1){
pre[j][i][0] = pre[j][i-1][0] + (pos[i] < pos[j]);
pre[j][i][2] = pre[j][i-1][2] + (pos[i] > pos[j]);
}
}
ll dp[n+1] = {0};
for(ll i = n-1;i >= 0;i--){
dp[i] = 1e18;
ll sum = 0,sumi = 0;
f(j,i+1,n+1){
sum += pre[j][j][0] - pre[j][i][0];
sumi += pre[j][n][1] - pre[j][j][1];
sumi -= pre[j][j][2] - pre[j][i][2];
dp[i] = min(dp[i],dp[j] + sum + sumi);
}
}
cout<<dp[0]<<"\n";
}
# | 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... |