Submission #685163

#TimeUsernameProblemLanguageResultExecution timeMemory
685163etheningGroup Photo (JOI21_ho_t3)C++17
100 / 100
772 ms196896 KiB
#include "bits/stdc++.h" #include <vector> using namespace std; using ll = long long; const int INF = 1e9; int n; int a[5005]; vector<vector<int>> p1, p2; void compute_p(vector<vector<int>>& p) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { p[i][j] = p[i][j] + p[i][j - 1] + p[i - 1][j] - p[i - 1][j - 1]; } } } int get_p(vector<vector<int>>& p, int l1, int r1, int l2, int r2) { return p[r1][r2] - p[l1 - 1][r2] - p[r1][l2 - 1] + p[l1 - 1][l2 - 1]; } int main() { cin.tie(0)->sync_with_stdio(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } p1 = vector<vector<int>>(n + 5, vector<int>(n + 5, 0)); p2 = vector<vector<int>>(n + 5, vector<int>(n + 5, 0)); vector<int> dp(n + 5, INF); for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { if (a[i] > a[j]) { ++p1[a[i]][a[j]]; } else if (a[i] < a[j]) { ++p2[a[i]][a[j]]; } } } // for (int i = 1; i <= n; i++) { // for (int j = 1; j <= n; j++) { // cout << p1[i][j] << " \n"[j == n]; // } // } // cout << "\n"; // for (int i = 1; i <= n; i++) { // for (int j = 1; j <= n; j++) { // cout << p2[i][j] << " \n"[j == n]; // } // } compute_p(p1); compute_p(p2); // cout << "===\n"; // for (int i = 1; i <= n; i++) { // for (int j = 1; j <= n; j++) { // cout << p1[i][j] << " \n"[j == n]; // } // } // cout << "\n"; // for (int i = 1; i <= n; i++) { // for (int j = 1; j <= n; j++) { // cout << p2[i][j] << " \n"[j == n]; // } // } dp[0] = 0; for (int i = 1; i <= n; i++) { { int cost2 = get_p(p2, 1, i, 1, i); dp[i] = min(dp[i], cost2); } for (int j = 1; j < i; j++) { int cost1 = get_p(p1, j + 1, i, 1, j); int cost2 = get_p(p2, j + 1, i, j + 1, i); dp[i] = min(dp[i], cost1 + cost2 + dp[j]); } } 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...