847H - Load Testing - CodeForces Solution


greedy *1600

Please click on ads to support us..

C++ Code:

#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#define ceri(a,l,r) {for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#include "bits/stdc++.h"
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll long long
#define llinf 100000000000000000LL
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define all(a) a.begin(),a.end()
#define sz(s) (ll)(s.size())
#define ios ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
#define mod 1000000007
using namespace std;
/*using namespace __gnu_pbds;
typedef tree<int,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
inline int add(int A, int B) { if(A + B >= mod) return A + B - mod; return A + B; }
inline int sub(int A, int B) { if(A - B < 0) return A - B + mod; return A - B; }
inline int mul(int A, int B) { return (ll)A * B % mod; }
inline int pot(int A, int B){
	int ret = 1, bs = A;
	for(; B ; B >>= 1){
		if(B & 1) ret = mul(ret, bs);
		bs = mul(bs, bs);
	}
	return ret%mod;
}*/
#define maxn 100005
ll n;
ll a[maxn];
ll inc(ll l){
    while(l<n-1&&a[l+1]>a[l])l++;
    return l;
}
ll dec(ll r){
    while(r>0&&a[r]<a[r-1])r--;
    return r;
}
void tc(){
    cin >> n;
    for(int i = 0;i<n;i++) cin >> a[i];
    ll l=0,r=n-1;
    l=inc(l);
    r=dec(r);
    ll ans=0;
    while(l<r){
        if(a[l]>a[r]){
			ans+=a[r]+1-a[r-1];
			a[r-1]=a[r]+1;
			r=dec(r);
		}
		else{
			ans+=a[l]+1-a[l+1];
			a[l+1]=a[l]+1;
			l=inc(l);
		}
	}
    cout<<ans<<endl;
}
int main(){
	ios
    int t; t = 1;
    //cin >> t;
    while(t--){
        tc();
    }
	return 0;
}


Comments

Submit
0 Comments
More Questions

1110A - Parity
1215B - The Number of Products
604C - Alternative Thinking
1204C - Anna Svyatoslav and Maps
322A - Ciel and Dancing
1689B - Mystic Permutation
1711B - Party
1702D - Not a Cheap String
1714F - Build a Tree and That Is It
1703F - Yet Another Problem About Pairs Satisfying an Inequality
610A - Pasha and Stick
1200A - Hotelier
1091A - New Year and the Christmas Ornament
1352B - Same Parity Summands
1102A - Integer Sequence Dividing
630B - Moore's Law
1004A - Sonya and Hotels
1680B - Robots
1690A - Print a Pedestal (Codeforces logo)
1295A - Display The Number
1077A - Frog Jumping
1714G - Path Prefixes
1369C - RationalLee
289B - Polo the Penguin and Matrix
1716A - 2-3 Moves
1670B - Dorms War
1716B - Permutation Chain
987A - Infinity Gauntlet
1676G - White-Black Balanced Subtrees
1716D - Chip Move