# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
388755 | CaroLinda | Group Photo (JOI21_ho_t3) | C++14 | 84 ms | 528 KiB |
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 lp(i,a,b) for(int i =a ; i < b ; i++)
#define ll long long
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
#define sz(x) (int)(x.size())
#define pii pair<int,int>
#define mk make_pair
#define mkt make_tuple
#define tiii tuple<int,int,int>
const int MAXN = 5010 ;
using namespace std ;
int N ;
int H[MAXN] , pos[MAXN] , art_pos[MAXN] ;
int maior_esq[MAXN] ;
int dp[MAXN] ;
int main()
{
scanf("%d", &N ) ;
lp(i,1,N+1)
{
scanf("%d", &H[i] ) ;
pos[H[i]] = i ;
}
vector<int> v = {1} ;
for(int i = 2 ; i <= N ; i++ )
{
int p = -1 ;
for(int j = 0 ; j < sz(v) ; j++ )
if( pos[ v[j] ] > pos[i] )
{
p = j ;
break ;
}
if(p == -1) v.pb(i) , p = sz(v)-1 ;
else v.insert( v.begin()+p , i ) ;
for(int j = p+1 ; j < sz(v) ; j++ ) maior_esq[ v[j] ]++ ;
for(int j = 0 ;j < sz(v) ; j++ ) art_pos[ v[j] ] = j ;
dp[i]= N*N ;
int cost = 0 ;
for(int g = i ; g > 0 ; g-- )
{
cost += i-1-art_pos[g]-maior_esq[g] ;
dp[i]=min(dp[i] , dp[g-1]+cost) ;
}
}
printf("%d\n" , dp[N] ) ;
}
Compilation message (stderr)
# | 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... |