Submission #1022983

#TimeUsernameProblemLanguageResultExecution timeMemory
1022983snpmrnhlolGroup Photo (JOI21_ho_t3)C++17
100 / 100
457 ms856 KiB
#include<bits/stdc++.h> using namespace std; const int N = 5e3; const int inf = 2e9; int v[N],pos[N]; int dp[N]; int fen[N][3]; int n; void add(int pos,int x,int id){ for(int i = pos;i < n;i|=(i + 1)){ fen[i][id]+=x; } } int get(int pos,int id){ int r = 0; for(int i = pos;i >= 0;i&=(i + 1),i--){ r+=fen[i][id]; } return r; } int main(){ cin>>n; for(int i = 0;i < n;i++){ cin>>v[i]; v[i]--; pos[v[i]] = i; dp[i] = inf; } int sum2 = 0; for(int i = n - 1;i >= 0;i--){ sum2+=get(n - 1 - pos[i],0); dp[i] = min(dp[i],sum2); //cout<<sum2<<' '; add(n - 1 - pos[i],1,0); add(pos[i],1,2); int sum = 0; for(int j = i - 1;j >= 0;j--){ sum+=get(pos[j],2); sum+=get(n - 1 - pos[j],1); add(n - 1 - pos[j],1,1); //cout<<j<<' '<<i<<' '<<sum<<'\n'; dp[j] = min(dp[j], dp[i] + sum); } for(int j = i - 1;j >= 0;j--){ add(n - 1 - pos[j],-1,1); } //cout<<dp[i]<<'\n'; } cout<<dp[0]; 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...