제출 #1194168

#제출 시각아이디문제언어결과실행 시간메모리
1194168VMaksimoski008Group Photo (JOI21_ho_t3)C++20
100 / 100
439 ms98392 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; struct fenwick { int n; vector<int> tree; fenwick(int _n) : n(_n+10), tree(n+50) {} void update(int p) { for(p++; p<n; p+=p&-p) tree[p]++; } int query(int p) { int ans = 0; for(p++; p; p-=p&-p) ans += tree[p]; return ans; } }; signed main() { int n; cin >> n; vector<int> a(n+1), dp(n+1, 1e8), pos(n+1); for(int i=1; i<=n; i++) cin >> a[i]; for(int i=1; i<=n; i++) pos[a[i]] = i; int cost[n+1][n+1]; memset(cost, 0, sizeof(cost)); fenwick fwt(n); for(int l=1; l<=n; l++) { fenwick fwt2(n); for(int r=l; r<=n; r++) { cost[l][r] = cost[l][r-1]; cost[l][r] += fwt.query(n) - fwt.query(pos[r]); cost[l][r] += fwt2.query(pos[r]); fwt2.update(pos[r]); } fwt.update(pos[l]); } dp[0] = 0; for(int i=1; i<=n; i++) for(int j=1; j<=i; j++) dp[i] = min(dp[i], dp[j-1] + cost[j][i]); cout << dp[n] << '\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...