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>
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
using namespace std;
#define int long long
#define pb push_back
#define nl '\n'
#define fore(i, y) for(int i = 0; i < y; i++)
#define forr(i, x, y) for(int i = x;i<=y;i++)
#define forn(i, y, x) for(int i = y; i >= x; i--)
const int INF = 1e18;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n ;
cin>>n;
vector<int> h(n + 1) , dp(n + 1 , INF) , pos(n + 1);
forr(i , 1 , n)
{
cin>>h[i];
pos[h[i]] = i;
}
dp[0] = 0;
vector<vector<int>> prevInv(n + 1 , vector<int>(n + 1 , 0));
vector<vector<int>> cost = prevInv;
forr(i , 1 ,n)
{
forr(j , 1 , n)
{
prevInv[i][j] = prevInv[i][j - 1] + (pos[j] > pos[i]);
}
}
forr(i , 1 , n)
{
forr(j , i , n)
{
cost[i][j]+=prevInv[i - 1][j];
if(i < j)
{
cost[i][j]+= cost[i][j - 1];
cost[i][j]+= (j - i);
cost[i][j]-= (prevInv[j][j - 1] - prevInv[j][i - 1]);
}
}
}
forr(i , 1 , n)
forr(j , 1 , i)
dp[i] = min(dp[i] , dp[j - 1] + cost[j][i]);
cout<<dp[n]<<nl;
}
# | 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... |