제출 #593151

#제출 시각아이디문제언어결과실행 시간메모리
593151piOOEGroup Photo (JOI21_ho_t3)C++17
100 / 100
157 ms196480 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> a(n), reva(n); vector<vector<int>> pl(n, vector<int>(n)); for (int i = 0; i < n; ++i) { cin >> a[i]; --a[i]; reva[a[i]] = i; } for (int i = 0; i < n; ++i) { pl[i][i] = 1; for (int j = i + 1; j < n; ++j) { pl[i][j] = pl[i][j - 1] + (reva[j] < reva[i]); } } vector<vector<int>> dp(n, vector<int>(n + 1, 1000'000'000)); dp[n - 1][1] = 0; for (int x = n - 2; x >= 0; --x) { for (int cnt = 1; cnt + 1 + x <= n; ++cnt) { dp[x][1] = min(dp[x][1], dp[x + 1][cnt] + abs(pl[x][n - 1] - 1)); } for (int cnt = 2; x + cnt <= n; ++cnt) { dp[x][cnt] = dp[x + 1][cnt - 1] + (pl[x][n - 1] - pl[x][x + cnt - 1]) + abs(pl[x][x + cnt - 1] - cnt); } } int ans = n * n; for (int i = 1; i <= n; ++i) { ans = min(ans, dp[0][i]); } cout << ans; 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...