Submission #1273630

#TimeUsernameProblemLanguageResultExecution timeMemory
1273630Jakub_WozniakGroup Photo (JOI21_ho_t3)C++20
100 / 100
335 ms159000 KiB
#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
const int maxn = 5009;
int dp[maxn];
int inw[maxn][maxn];
int C[maxn][maxn];
bool czyinw[maxn][maxn];
int a[maxn];


int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int N;
    cin >> N;
    for(int i = 1; i <= N ; i++)cin >> a[i];
    
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1 ; j < i; j++)
        {
            if(a[i] < a[j])
            {
                czyinw[a[i]][a[j]] = 1; 
                czyinw[a[j]][a[i]] = 1; 
            }
        }
    }

    dp[0] = 0;
    for(int i = 1 ; i <= N ; i++)
    {
        dp[i] = i*i;

        inw[i][i] =0 ;
        for(int j = i-1 ; j >= 1 ; j--)
        {
            inw[i][j] = inw[i][j+1];
            if(czyinw[j][i])inw[i][j]++;
        }
        for(int j = 1 ; j <= i ; j++)inw[i][j] += inw[i-1][j];

        C[i][i] = 0;
        for(int j = i-1 ; j >= 1 ; j--)
        {
            C[i][j] = 0; 
            if(czyinw[j][i])
            {
                C[i][j+1] += 1;
            }
        }
        for(int j = 1 ; j <= i ; j++)
        {
            C[i][j] = C[i][j-1] + C[i][j];
        }
        for(int j = 1; j <= i ;j++)C[i][j] += C[i-1][j];


        for(int j = 0 ; j < i ; j++)
        {

            dp[i] = min(dp[i] , dp[j] + min(inw[i][j+1], (i-j)*(i-j-1)/2 - inw[i][j+1]) + C[i][j+1]);
        }
    }
    cout << dp[N] << '\n';
    
    return 0;

}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...