-
Notifications
You must be signed in to change notification settings - Fork 7
/
new-year-chaos.cpp
76 lines (72 loc) · 980 Bytes
/
new-year-chaos.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//https://www.hackerrank.com/contests/hourrank-4/challenges/new-year-chaos
#include<iostream>
using namespace std;
int check(int arr[],int x,int n)
{
for(int i=x;i>=0;i--)
{
if(arr[i]==x)
{
return i;
}
}
return 0;
}
int main()
{
int t,n;
int arr[100000];
int brr[100000];
cin>>t;
arr[0]=0;
while(t--)
{
int bribe=0;
cin>>n;
int x;
for(int i=0;i<=n;i++)
{
brr[i]=0;
}
for(int i=1;i<=n;i++)
cin>>arr[i];
int flag=1;
int z=0;
int j=n;
int i=n;
for(int i=n;i>0;i--)
{
int y=1;
while(y)
{
if(arr[j]!=i)
{
x=check(arr,j,n);
int temp=arr[x+1];
arr[x+1]=arr[x];
arr[x]=temp;
bribe+=1;
brr[j]+=1;
if(brr[j]>2)
{
cout<<"Too chaotic"<<endl;
z=1;
i=-1;
break;
}
if(z==1)
break;
}
if(z==1) break;
if(arr[j]==i)
{
y=0;
j-=1;
}
}
}
if(z==0)
cout<<bribe<<endl;
}
return 0;
}