Submission #532748

#TimeUsernameProblemLanguageResultExecution timeMemory
532748Rafi22Group Photo (JOI21_ho_t3)C++14
100 / 100
288 ms67652 KiB
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define st first #define nd second #define pb push_back #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() #define ll long long ll mod=1000000007; int inf=1000000007; ll infl=1000000000000000007; int a[5007]; int ile[5007][5007]; int DP[5007]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; int inv=0; for(int i=1;i<=n;i++) { for(int j=1;j<i;j++) { if(a[j]>a[i]) { inv++; ile[min(a[i],a[j])][max(a[i],a[j])]++; } } } for(int k=0;k<n;k++) { for(int i=1;i+k<=n;i++) { int j=i+k; ile[i][j]+=ile[i+1][j]+ile[i][j-1]-ile[i+1][j-1]; } } for(int i=1;i<=n;i++) { DP[i]=inf; for(int j=i-1;j>=0;j--) { DP[i]=min(DP[i],DP[j]+(i-j)*(i-j-1)/2-2*ile[j+1][i]); } } cout<<inv+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...