이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
long long n , a [5003] , bs [5003][5003] , wr [5003] , dp [5003][5003];
int f ( int x , int y )
{
if ( x == n + 1 ) return 0;
if ( dp [x][y] != -1 ) return dp [x][y];
int ans = 1e9 , pos = x , posx = wr [y] + bs [y][ x - 1 ] , howmany = ( bs [y][ y - 1 ] - bs [y][ x - 1 ] );
if ( x != y ) ans = min ( ans , f ( y , y ) );
if ( y != n + 1 ) ans = min ( ans , f ( x , y + 1 ) + posx - pos - howmany );
return dp [x][y] = ans;
}
int main ()
{
memset ( dp , -1 , sizeof dp );
cin >> n;
for ( int i = 1 ; i <= n ; i ++ )
{
cin >> a [i];
wr [ a [i] ] = i;
}
for ( int i = 1 ; i <= n ; i ++ )
{
for ( int j = i ; j <= n ; j ++ ) bs [ a [i] ][ a [j] ] ++;
for ( int j = 1 ; j <= n ; j ++ ) bs [ a [i] ][j] += bs [ a [i] ][ j - 1 ];
}
cout << f ( 1 , 1 );
}
# | 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... |