1668C - Make it Increasing - CodeForces Solution


brute force greedy math *1300

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n;
    cin>>n;
    long long ans=1e18;
    long long arr[n];
    for(int i=0;i<n;i++)cin>>arr[i];
    for(int i=0;i<n;i++){
        long long ans1=0;
        long long mx=0;
        for(int j=i-1;j>=0;j--){
            long long x=arr[j];
            if(x<mx){
                ans1++;
                mx=-arr[j];
            }
            else{
                long long y=abs(mx);
            long long z=(y/x)+1;
            ans1+=z;
            mx=-(z*x);
            }
            
        }
        mx=0;
        for(int j=i+1;j<n;j++){
           long long x=arr[j];
           if(x>mx){
               ans1++;
               mx=x;
           }
           else{
               long long z=mx/x +1;
               ans1+=z;
               mx=(z*x);
           }
        }
        ans=min(ans,ans1);
    }
    cout<<ans<<endl;
    return 0;
}


Comments

Submit
0 Comments
More Questions

1092B - Teams Forming
1166C - A Tale of Two Lands
544B - Sea and Islands
152B - Steps
1174D - Ehab and the Expected XOR Problem
1511A - Review Site
1316A - Grade Allocation
838A - Binary Blocks
1515D - Phoenix and Socks
1624D - Palindromes Coloring
1552F - Telepanting
1692G - 2Sort
1191A - Tokitsukaze and Enhancement
903A - Hungry Student Problem
52B - Right Triangles
1712A - Wonderful Permutation
1712D - Empty Graph
1712B - Woeful Permutation
1712C - Sort Zero
1028B - Unnatural Conditions
735B - Urbanization
746C - Tram
1278B - A and B
1353D - Constructing the Array
1269C - Long Beautiful Integer
1076A - Minimizing the String
913C - Party Lemonade
1313A - Fast Food Restaurant
681A - A Good Contest
1585F - Non-equal Neighbours