-
Notifications
You must be signed in to change notification settings - Fork 0
/
NewYearChaos.cs
64 lines (47 loc) · 1.49 KB
/
NewYearChaos.cs
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
using System;
class Solution {
// Complete the minimumBribes function below.
static void MinimumBribes(int[] bastards)
{
var moveCount = 0;
for (int i = 0; i < bastards.Length; i++)
{
var index = bastards.Length - (i + 1);
if (bastards[index] <= index) continue;
var partialMoveCount = FixPlace(bastards, index);
if (partialMoveCount > 2)
{
Console.WriteLine("Too chaotic");
return;
}
moveCount += partialMoveCount;
i -= partialMoveCount;
}
Console.WriteLine(moveCount);
}
static int FixPlace(int[] array, int index)
{
var indexPlus = index + 1;
if (array[index] == indexPlus)
{
return 0;
}
Swap(array, index, index = indexPlus);
return FixPlace(array, index) + 1;
}
static void Swap(int[] array, int index1, int index2)
{
var oneTemp = array[index1];
array[index1] = array[index2];
array[index2] = oneTemp;
}
static void Main(string[] args) {
int t = Convert.ToInt32(Console.ReadLine());
for (int tItr = 0; tItr < t; tItr++) {
int n = Convert.ToInt32(Console.ReadLine());
int[] q = Array.ConvertAll(Console.ReadLine().Split(' '), qTemp => Convert.ToInt32(qTemp))
;
MinimumBribes(q);
}
}
}