1675B - Make It Increasing - CodeForces Solution


greedy implementation *900

Please click on ads to support us..

Python Code:

for s in[*open(0)][2::2]:
 *a,=map(int,s.split());r=1;p=3e9
 while a and p:
  q=a.pop()
  while q>=p:r+=1;q>>=1
  p=q
 print(r*(a==[])-1)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long 
#define pb push_back
#define forn(i,x,n) for(int i=x; i<n; i++)

void solve() {

  ll n;
  cin>>n;
  ll arr[n];
  forn(i,0,n) cin>>arr[i];
  int cnt = 0;
  
  for(int i=n-1; i>0; i--){

    if(arr[i-1] >= arr[i]){

      if(arr[i] == 0){
        cout<<"-1\n";
        return;
      }
      while(arr[i-1] >= arr[i]){
        arr[i-1] /= 2;
        cnt++;
      }
    }
  }
  cout<<cnt<<"\n";
}

int main(){
  int t;
  cin>>t;
  while(t--){
    solve();
  }
}


Comments

Submit
0 Comments
More Questions

931A - Friends Meeting
1594A - Consecutive Sum Riddle
1466A - Bovine Dilemma
454A - Little Pony and Crystal Mine
2A - Winner
1622B - Berland Music
1139B - Chocolates
1371A - Magical Sticks
1253A - Single Push
706B - Interesting drink
1265A - Beautiful String
214A - System of Equations
287A - IQ Test
1108A - Two distinct points
1064A - Make a triangle
1245C - Constanze's Machine
1005A - Tanya and Stairways
1663F - In Every Generation
1108B - Divisors of Two Integers
1175A - From Hero to Zero
1141A - Game 23
1401B - Ternary Sequence
598A - Tricky Sum
519A - A and B and Chess
725B - Food on the Plane
154B - Colliders
127B - Canvas Frames
107B - Basketball Team
245A - System Administrator
698A - Vacations