Submission #995195

#TimeUsernameProblemLanguageResultExecution timeMemory
995195gmroh06Group Photo (JOI21_ho_t3)C++14
100 / 100
159 ms199776 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; inline void fastio() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); } ll n, arr[5050], p[5050], dp[5050], sum[5050][5050]; int main() { fastio(); cin >> n; for (ll i = 1; i <= n; i++) { cin >> arr[i]; p[arr[i]] = i; } for (ll i = 1; i <= n; i++) { for (ll j = 1; j <= n; j++) { sum[i][j] += sum[i - 1][j] + (arr[i] <= j); } } memset(dp, 0x3f, sizeof(dp)); *dp = 0; for (ll i = 1; i <= n; i++) { for (ll j = i, tmp = 0; j > 0; j--) { tmp += (i - j) + (sum[p[j]][n] - sum[p[j]][i]) - (sum[p[j]][i] - sum[p[j]][j]); dp[i] = min(dp[i], dp[j - 1] + tmp); } } cout << dp[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...