Submission #408882

#TimeUsernameProblemLanguageResultExecution timeMemory
408882mat_vGroup Photo (JOI21_ho_t3)C++14
100 / 100
857 ms294596 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
#define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
#define mod 998244353
#define xx first
#define yy second
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define ll long long
#define pii pair<int,int>


using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>,rb_tree_tag, tree_order_statistics_node_update> ordered_set;/// find_by_order(x)(x+1th) , order_of_key() (strictly less)
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());


int n;
int niz[5005];
int dp[5005];
int ans[5005];
int poz[5005];
int pref[5005][5005];
int suff1[5005][5005];
int suff2[5005][5005];

int main()
{

    ios_base::sync_with_stdio(false); cin.tie(0);

    cin >> n;
    ff(i,1,n){
        cin >> niz[i];
        poz[niz[i]] = i;
    }
    ff(i,1,n){
        ff(j,0,n){
            if(niz[i] > j)pref[i][j]++;
            pref[i][j] += pref[i - 1][j];
        }
    }
    fb(i,n,1){
        ff(j,0,n){
            if(niz[i] >= j)suff1[i][j]++;
            suff1[i][j] += suff1[i + 1][j];
            if(niz[i] <= j)suff2[i][j]++;
            suff2[i][j] += suff2[i + 1][j];
        }
    }
    dp[n] = 0;
    fb(i,n-1,0){
        ans[0] = 0;
        dp[i] = 1e9;
        ff(j,i+1,n){
            ans[j-i] = ans[j-i-1];
            ans[j-i] += pref[poz[j] - 1][i];
            if(j != i+1){
                int ost = n - poz[j];
                ost -= suff1[poz[j]+1][j];
                ost -= suff2[poz[j]+1][i];
                ans[j-i] -= ost;
            }
            dp[i] = min(dp[i], ans[j-i] + dp[j]);
        }
    }
    cout << dp[0] << "\n";

    return 0;
}

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
Main.cpp:38:5: note: in expansion of macro 'ff'
   38 |     ff(i,1,n){
      |     ^~
Main.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
Main.cpp:42:5: note: in expansion of macro 'ff'
   42 |     ff(i,1,n){
      |     ^~
Main.cpp:6:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
Main.cpp:43:9: note: in expansion of macro 'ff'
   43 |         ff(j,0,n){
      |         ^~
Main.cpp:7:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    7 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
      |                           ^
Main.cpp:48:5: note: in expansion of macro 'fb'
   48 |     fb(i,n,1){
      |     ^~
Main.cpp:6:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
Main.cpp:49:9: note: in expansion of macro 'ff'
   49 |         ff(j,0,n){
      |         ^~
Main.cpp:7:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
    7 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
      |                           ^
Main.cpp:57:5: note: in expansion of macro 'fb'
   57 |     fb(i,n-1,0){
      |     ^~
Main.cpp:6:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
    6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
      |                           ^
Main.cpp:60:9: note: in expansion of macro 'ff'
   60 |         ff(j,i+1,n){
      |         ^~
#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...