This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define debug(x) cout << '[' << #x << " is: " << x << "] " << endl;
#define inF freopen("cardgame.in" , "r" , stdin);
#define outF freopen("cardgame.out" , "w" , stdout);
#define imod(a , n) (a % n + n ) % n
#define sor(v) sort(v.begin() , v.end());
#define print(v) for(auto f : v ) cout << f << " " ;
#define rsor(v) sort(v.rbegin() , v.rend());
#define rev(v) reverse(v.begin() , v.end());
#define scan(v)for(auto &it : v)cin >> it ;
#define ll long long
#define logar(x , y) log(x) / log(y)
#define __sum(n) n * (n + 1) / 2
#define __lcm(a , b) a / __gcd(a , b) * b
#define pii pair<int , int >
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);
#define int ll
const int N = 3e5 + 5 , M = N * 4 , MX = 2e4 + 40;
const ll MOD = 998244353 , oo = 1e9 + 9 , OO = 1e18 , mod = MOD ;
const double pi = acos(-1) , eps = 1e-17 ;
int di[] = {1 , -1 , 0 , 0};
int dj[] = {0 , 0 , 1 , -1};
struct data{
int a , b , c ;
};
int32_t main()
{
// inF;
// inF;outF;
fastio;
//Welcome back without <TAWJIHI>
int n;
cin >> n ;
vector<int> v(n);
for(auto &it : v)cin >> it ;
int mn = 1e9 ;
// asc or desc
int asc = 0 , past = v[0] - 1 , cnt = 0;
for(int i = 0 ; i < n ; i ++){
int curr = v[i] + cnt ;
if(past >= curr)cnt += past - curr + 1 , asc += past - curr + 1 , curr += past - curr + 1 ;
past = curr ;
}
mn = min(mn , asc);
past = v.back() - 1 ;
int desc = 0 ;
cnt = 0 ;
for(int i = n - 1 ; i >= 0 ; i --){
int curr = v[i] + cnt ;
if(past >= curr)cnt += past - curr + 1 , desc += past - curr +1 , curr += past - curr + 1 ;
past = curr ;
}
mn = min(mn , desc);
for(int i = 1 ; i < n - 1 ; i ++){
int j = 1 , c2 = 0 , c1 = 0 , past , past2;
past = v[0];
int ans = 0 ;
while(j < i){
int curr = v[j] + c1 ;
if(curr <= past)
c1 += past - curr + 1 , ans += past - curr + 1 ;
past = v[j] + c1;
j ++ ;
}
past2 = v.back();
j = n - 2 ;
while(j >= i){
int curr = v[j] + c2 ;
if(curr <= past2)c2+= past2 - curr + 1 , ans += past2 - curr + 1 ;
past2 = v[j] + c2 ;
j -- ;
}
if(past >= past2)ans += past - past2 + 1 , c1 += past - past2 + 1 ;
ans -= min(c1 , c2);
mn = min(mn , ans);
}
cout << mn ;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |