Submission #42298

#TimeUsernameProblemLanguageResultExecution timeMemory
42298minhtung0404즐거운 채소 기르기 (JOI14_growing)C++14
100 / 100
103 ms29556 KiB
#include<bits/stdc++.h> const long long N = 3e5 +5; const long long inf = 1e9; using namespace std; typedef pair <long long, long long> ii; vector <ii> mv; long long n, h, res, bit[N], cnt, pos[N], dp[N]; bool check[N]; void update(long long i){ while (i <= n){ bit[i]++; i += i&(-i); } } long long get(long long i){ long long ans = 0, z = i; while (i > 0){ ans += bit[i]; i -= i&(-i); } return z - ans; } int main(){ ios::sync_with_stdio(false); cin >> n; for (long long i = 1; i <= n; i++){ cin >> h; mv.push_back(ii(h, i)); } sort(mv.begin(), mv.end()); mv.push_back(ii(-inf, -inf)); for (long long i = 0; i < n; i++){ long long cnt = i, z = mv[cnt].first, minn = inf, sum = 0; while (mv[cnt].first == z) { pos[cnt] = get(mv[cnt].second); cnt++; } dp[cnt] = 0; for (long long j = cnt-1; j >= i; j--){ dp[j] = dp[j+1] + n-i-pos[j]-cnt+j+1; } minn = dp[i]; for (long long j = i; j < cnt; j++){ sum += pos[j]-1-j+i; minn = min (minn, dp[j+1] + sum); update(mv[j].second); } res += minn; i = cnt-1; } cout << res; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...