1638A - Reverse - CodeForces Solution


constructive algorithms greedy math *800

Please click on ads to support us..

Python Code:

for s in[*open(0)][2::2]:
 a=[0,*map(int,s.split()),0];i=1
 while a[i]==i:i+=1
 if a[i]:j=a.index(i)+1;a[i:j]=a[i:j][::-1]
 print(*a[1:-1])

C++ Code:

//make longer column than row if Possible
//dont print always store ans
//prime:1e9+7
//use unorder map if just hashing is required
//if you know the size of vector then use array<int,size>
//continuously division by a number is log(n) operation.
/*
GO FIRST BRUTE FORCE..start with some homogeneous,simple testcases
TRY to DIVIDE problem into simple cases according to given conditions then prove your solution by using some Example for each case
SEE BIGGER PICTURE:take more time to think logic than code
If regularly wrong submission occurs,then check code line by line
*/
//string conversion to int=stoi(string),O(n)
#include<bits/stdc++.h>
#include<iostream>
typedef long long ll;
#define all(a) a.begin(),a.end()
#define ss second
#define ff first
#define pb push_back
#define el endl
#define fio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
int main()
{
fio;
ll tt;
cin>>tt;
while (tt--)
{
ll n;
cin>>n;
vector<ll>a(n),b;
int id=0;
vector<int>m(n+1);
for (int i = 0; i < n; i++){cin>>a[i];m[a[i]]=i;}
b=a;
sort(all(b));
// if(a==b){for(auto i:a)cout<<i<<" ";cout<<'\n';continue;}
for (int j = 0; j < n; j++)
{
if(a[j]==j+1){cout<<j+1<<" ";continue;}
id=m[j+1]; 

for (int i = id; i>=j; i--)
{
    cout<<a[i]<<" ";
}
for (int i = id+1; i < n; i++)
{
    cout<<a[i]<<" ";
}
break;
}
cout<<'\n';

}
return 0;
}


Comments

Submit
0 Comments
More Questions

Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses