Submission #529544

#TimeUsernameProblemLanguageResultExecution timeMemory
529544abc864197532Group Photo (JOI21_ho_t3)C++17
100 / 100
706 ms196564 KiB
#include <bits/stdc++.h> using namespace std; #define lli long long int #define pb push_back #define eb emplace_back #define pii pair<int,int> #define X first #define Y second #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() void abc() {cout << endl;} template <typename T, typename ...U> void abc(T i, U ...j) { cout << i << ' ', abc(j...); } template <typename T> void printv(T l, T r) { for (; l != r; ++l) cout << *l << " \n"[l + 1 == r]; } #ifdef Doludu #define test(x...) abc("[" + string(#x) + "]", x) #define owo freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout) #else #define test(x...) void(0) #define owo ios::sync_with_stdio(false), cin.tie(0) #endif const int N = 200000; int main () { owo; int n; cin >> n; vector <int> p(n); for (int i = 0; i < n; ++i) cin >> p[i], --p[i]; vector <int> dp(n + 1, 1 << 30); vector <vector <int>> cost(n + 1, vector <int> (n + 1, 0)); vector <vector <int>> inversion(n + 1, vector <int> (n + 1, 0)); for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { if (p[i] > p[j]) inversion[p[j]][p[i] + 1]++; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { inversion[i][j + 1] += inversion[i][j]; } } vector <int> q(n); for (int r = 1; r <= n; ++r) { int t = 0; for (int i = 0; i < n; ++i) if (p[i] < r) q[p[i]] = t++; for (int l = r - 1; ~l; --l) { cost[l][r] = cost[l + 1][r] + r - q[l] - 1 - inversion[l][r]; } } dp[0] = 0; for (int i = 1; i <= n; ++i) { for (int j = 0; j < i; ++j) { // max at postion j dp[i] = min(dp[i], dp[j] + cost[j][i]); } } cout << dp[n] << endl; }
#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...