제출 #625129

#제출 시각아이디문제언어결과실행 시간메모리
625129boris_mihovGrowing Vegetables is Fun 4 (JOI21_ho_t1)C++14
0 / 100
1 ms340 KiB
#include <algorithm> #include <iostream> #include <numeric> #include <vector> typedef long long llong; const int MAXN = 5000 + 10; const int INF = 1e9; int a[MAXN], n; llong prefix[MAXN]; llong suffix[MAXN]; llong prefVal[MAXN]; llong suffVal[MAXN]; void solve() { prefVal[1] = a[1]; suffVal[n] = a[n]; int last = a[1]; prefix[1] = suffix[n] = 0; for (int i = 2 ; i <= n ; ++i) { prefix[i] = prefix[i-1] + std::max(0, last + 1 - a[i]); last = std::max(last + 1, a[i]); prefVal[i] = last; } last = a[n]; llong ans = prefix[n]; for (int i = n-1 ; i >= 0 ; --i) { suffix[i] = suffix[i+1] + std::max(0, last + 1 - a[i]); last = std::max(last + 1, a[i]); suffVal[i] = last; } for (int i = 0 ; i <= n ; ++i) { ans = std::min(ans, std::max(prefix[i], suffix[i + 1]) + (prefVal[i] == suffVal[i + 1] && prefix[i] == suffix[i + 1])); } std::cout << ans << '\n'; } void read() { std::cin >> n; for (int i = 1 ; i <= n ; ++i) { std::cin >> a[i]; } } void fastIO() { std::ios_base :: sync_with_stdio(0); std::cout.tie(nullptr); std::cin.tie(nullptr); } int main() { fastIO(); read(); solve(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...