Submission #1090883

#TimeUsernameProblemLanguageResultExecution timeMemory
1090883yogesh_saneGroup Photo (JOI21_ho_t3)C++17
44 / 100
5072 ms2908 KiB
#include <iostream> #include <vector> #include <algorithm> #include <numeric> #include <queue> using namespace std; int n; vector<int>a, idx; vector<int>p; int const inf = 1e9; vector<int>dp; vector<vector<int>>dpcost; int cost(int pos, int val){ if(dpcost[pos][val] != -1) return dpcost[pos][val]; //cout << "cost(" << pos << ',' << val << ")\n"; iota(p.begin(), p.end(), 0); reverse(p.begin()+pos, p.begin()+val+1); int inv = 0; for(int i = pos; i <= val; i++){ for(int j = i+1; j <= n; j++){ if((p[i] < p[j] && idx[i] > idx[j]) || (p[i] > p[j] && idx[i] < idx[j])) inv++; } } return dpcost[pos][val] = inv; } int solve(int k){ if(k > n) return 0; if(dp[k] != -1) return dp[k]; //cout << "solve(" << k <<")" << endl; if(k > n) return 0; int ans = inf; for(int i = k; i <= n; i++){ ans = min(ans, cost(k, i) + solve(i+1)); } return dp[k] = ans; } int main(){ cin >> n; a.resize(n+1); p.resize(n+1); idx.resize(n+1); dp.resize(n+1, -1); dpcost.resize(n+1, vector<int>(n+1, -1)); for(int i = 1; i <= n; i++){ cin >> a[i]; idx[a[i]] = i; } cout << solve(1) << '\n'; 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...