Submission #996475

#TimeUsernameProblemLanguageResultExecution timeMemory
996475amine_arouaGroup Photo (JOI21_ho_t3)C++17
0 / 100
1 ms348 KiB
#include <bits/stdc++.h> //#pragma GCC optimize("O3") //#pragma GCC optimize("unroll-loops") using namespace std; #define int long long #define pb push_back #define nl '\n' #define fore(i, y) for(int i = 0; i < y; i++) #define forr(i, x, y) for(int i = x;i<=y;i++) #define forn(i, y, x) for(int i = y; i >= x; i--) const int INF = 1e18; signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n ; cin>>n; vector<int> h(n + 1) , dp(n + 1 , INF) , pos(n + 1); forr(i , 1 , n) { cin>>h[i]; pos[h[i]] = i; } dp[0] = 0; vector<vector<int>> prevInv(n + 1 , vector<int>(n + 1 , 0)); vector<vector<int>> cost = prevInv; forr(i , 1 ,n) { forr(j , 1 , n) { prevInv[i][j] = prevInv[i][j - 1] + (pos[j] > pos[i]); } } forr(i , 1 , n) { forr(j , i , n) { cost[i][j]+=prevInv[i - 1][j]; if(i < j) { cost[i][j]+= cost[i][j - 1]; cost[i][j]+= (j - i); cost[i][j]-= (prevInv[j][j - 1] - prevInv[j][i - 1]); } } } forr(i , 1 , n) forr(j , 1 , i) dp[i] = min(dp[i] , dp[j - 1] + cost[j][i]); cout<<dp[n]<<nl; }
#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...