Submission #1331298

#TimeUsernameProblemLanguageResultExecution timeMemory
1331298Valters07Group Photo (JOI21_ho_t3)C++20
44 / 100
5094 ms2136 KiB
#include <bits/stdc++.h>
#define fio ios_base::sync_with_stdio(0);cin.tie(0);
#define ll long long
#define pb push_back
#define fi first
#define se second
#define en exit(0);
using namespace std;
const int N = 5005;
const int INF = 1e9 + 5;
int arr[N], dp[N], inv[N][N], to_front[N][N];
// dp[i] - min number of swaps to have [1;i] satisfy the condition and consist of numbers in [1;i]
// inv[a][b] - the number of reverse inversions of only the numbers [a;b]
// to_front[a][b] - the number of numbers > b in front of numbers in [a;b]
int main()
{
    fio
//    ifstream cin("in.in");
    int n;
    cin >> n;
    for(int i = 1;i <= n;i++)
        cin >> arr[i];
    // calculate inv[a][b]
    for(int a = 1;a <= n;a++)
        for(int b = a;b <= n;b++)
            for(int i = 1;i <= n;i++)
                for(int j = i + 1;j <= n;j++)
                    if(a <= arr[i] && arr[i] <= b && a <= arr[j] && arr[j] <= b && arr[i] < arr[j])
                        inv[a][b]++;
    // calculate to_front[a][b]
    for(int a = 1;a <= n;a++)
        for(int b = a;b <= n;b++)
            for(int i = 1;i <= n;i++)
                for(int j = i + 1;j <= n;j++)
                    if(a <= arr[j] && arr[j] <= b && arr[i] > b)
                        to_front[a][b]++;
    // calculate dp[i]
    for(int i = 1;i <= n;i++)
        dp[i] = INF;
    for(int i = 1;i <= n;i++)
        for(int j = 1;j <= i;j++)
            dp[i] = min(dp[i], dp[j - 1] + to_front[j][i] + inv[j][i]);
    cout << dp[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...