제출 #1171251

#제출 시각아이디문제언어결과실행 시간메모리
1171251nguyenkhangninh99Group Photo (JOI21_ho_t3)C++20
100 / 100
389 ms632 KiB
#include <bits/stdc++.h> using namespace std; #define int long long const int maxn = 5e3 + 5; int dp[maxn], pos[maxn]; struct FenwickTree{ vector<int> tree; void init(int n){ tree.assign(n + 2, 0); } void update(int v, int val) { for (; v < tree.size(); v += v & (-v)) tree[v] += val; } int get(int v) { int res = 0; for (; v; v -= v & (-v)) res += tree[v]; return res; } } bit; void solve(){ int n; cin >> n; for(int i = 1; i <= n; i++){ int x; cin >> x; pos[x] = i; dp[i] = 1e9; } bit.init(n + 1); for(int i = 1; i <= n; i++){ int cost = 0; for(int j = i; j <= n; j++){ cost += pos[j] - i - ((j - i) - bit.get(pos[j])); //pos[j] - i. đi từ pos[j] tới i + 1. đảm bảo pos[j] > i + 1 dp[j] = min(dp[j], dp[i - 1] + cost); bit.update(pos[j], 1); } bit.init(n + 1); for(int j = i + 1; j <= n; j++) if(pos[j] < pos[i]) pos[j]++; //phần tử giá trị từ [1, i + 1] phải được xếp vào i + 1 vị trí đầu để dùng dp[i + 1] tính } cout << dp[n] << '\n'; } signed main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); }
#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...