이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(v) v.begin() , v.end()
#define sz(a) (int)a.size()
const int inf = 1e18 + 7;
void solve(){
int n;
cin >> n;
int arr[n];
for(int i = 0;i<n;i++)cin >> arr[i];
int pre[n]={0} , suf[n]={0};
int preval[n] , sufval[n];
preval[0] = arr[0];
int sum = 0;
for(int i = 1;i<n;i++){
sum += max(0ll , arr[i-1] + 1 - arr[i]);
pre[i] = sum;
preval[i] = arr[i] + sum;
}
sufval[n-1] = arr[n-1];
sum = 0;
for(int i = n-2;i>=0;i--){
sum += max(0ll , arr[i+1] + 1 - arr[i]);
suf[i] = sum;
sufval[i] = arr[i] + sum;
}
int global_ans = inf;
for(int i = 0;i<n;i++){
int ans[3];
ans[0] = (i == 0 ? 0ll : pre[i-1]);
ans[1] = max(0ll , max(i == 0 ? 0ll : preval[i-1] + 1 , i == n-1 ? 0ll : sufval[i+1] + 1) - arr[i]);
ans[2] = (i == n-1 ? 0ll : suf[i+1]);
int total = max(ans[0] , ans[2]);
total += max(0ll , ans[1] - max(ans[0] , ans[2]));
global_ans = min(global_ans , total);
}
cout << global_ans << endl;
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int testcase = 1;cin >> testcase;
while(testcase--)solve();
cerr << 1000.0 * clock() / CLOCKS_PER_SEC << " ms" << endl;
}
/*
* dont get stuck on one approach , try different ones
* if you think the implementation is long & messy , change your approach
* think simple , dont overkill
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |