1635B - Avoid Local Maximums - CodeForces Solution


greedy *800

Please click on ads to support us..

Python Code:

for i in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))+[0]
    count=0
    for i in range(1,n-1):
        if a[i-1]<a[i]>a[i+1]:
            a[i+1]=max(a[i],a[i+2])
            count+=1
    print(count)
    print(*a[:-1])

C++ Code:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
using namespace std;

#define read freopen("0_input.txt","r",stdin)
#define write freopen("0_output.txt","w",stdout)
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;
#define lld long long int
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define mXs 1e6
#define test long long int ct;cin >> ct;while (ct--)
const double pi = acos(-1.0);

void solve(){
    lld n,i,j,ans=0,minn,maxx;
    cin>>n;
    lld ara[n];
    for(i=0;i<n;i++){
        cin>>ara[i];
    }
    for(i=1;i<n-1;i++){
        if(ara[i]>ara[i-1] && ara[i]>ara[i+1]){
             if(i + 2 < n) 
                ara[i + 1] = max(ara[i], ara[i + 2]); 
            else 
                ara[i + 1] = ara[i];
            ans++;
        }
    }
    cout<<ans<<endl;
    for(i=0;i<n;i++){
        cout<<ara[i]<<" ";
    }
    cout<<endl;
}
int main()
{
    cin.tie(NULL);
    //read;
    //write;
    ios_base::sync_with_stdio(false);
    test{
        solve();
    }
    return 0;
}

 	  					  				 			 		 			 	 	


Comments

Submit
0 Comments
More Questions

1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols
1213A - Chips Moving
490A - Team Olympiad
233A - Perfect Permutation
1360A - Minimal Square
467A - George and Accommodation
893C - Rumor
227B - Effective Approach
1534B - Histogram Ugliness
1611B - Team Composition Programmers and Mathematicians
110A - Nearly Lucky Number
1220B - Multiplication Table
1644A - Doors and Keys
1644B - Anti-Fibonacci Permutation
1610A - Anti Light's Cell Guessing
349B - Color the Fence
144A - Arrival of the General
1106A - Lunar New Year and Cross Counting
58A - Chat room
230A - Dragons