Submission #1104681

#TimeUsernameProblemLanguageResultExecution timeMemory
1104681Champ_NamanGroup Photo (JOI21_ho_t3)C++17
100 / 100
692 ms392016 KiB
#include<bits/stdc++.h> using namespace std; #define int long long #define nl '\n' int pre1[5001][5001]; int pre2[5001][5001]; inline void solve(){ int n; cin>>n; int a[n+1]; for(int i=1; i<=n; i++) cin>>a[i]; for(int i=1; i<=n; i++){ for(int j=i+1; j<=n; j++){ pre1[a[j]][a[i]]++; pre2[a[j]][a[i]]++; } } for(int i=1; i<=n; i++){ for(int j=1; j<=n; j++){ pre1[i][j] += pre1[i][j-1]; } } for(int j=1; j<=n; j++){ for(int i=1; i<=n; i++){ pre2[i][j] += pre2[i-1][j]; } } int dp[n+1]; dp[0] = 0; for(int i=1; i<=n; i++){ dp[i] = 1e18; int add = 0; for(int j=i; j>0; j--){ add += pre1[j][n] - pre1[j][i]; add += pre2[i][j] - pre2[j][j]; dp[i] = min(dp[i], dp[j-1] + add); } } cout<<dp[n]<<nl; } signed main(){ ios_base::sync_with_stdio(0); cin.tie(NULL);cout.tie(NULL); int t = 1; //cin>>t; while(t--) solve(); 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...