-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewYearsChaos.cs
68 lines (51 loc) · 1.77 KB
/
newYearsChaos.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
65
66
67
68
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
class Solution {
static void minimumBribes(int[] q) {
var totalSwaps = 0;
for (var value = q.Length; value > 0; value--) {
var currentValueIndex = -1;
var originalValueIndex = value - 1;
for (var position = 0; position < 3; position++) {
if (q[originalValueIndex - position] == value) {
currentValueIndex = originalValueIndex - position;
break;
}
}
if (currentValueIndex == -1) {
Console.WriteLine("Too chaotic");
return;
}
var diff = originalValueIndex - currentValueIndex;
totalSwaps += diff;
//shifting array
var valueToShift = value;
for (int j = 0; j < diff + 1; j++) {
var valueToBeReplaced = q[originalValueIndex - j];
q[originalValueIndex - j] = valueToShift;
valueToShift = valueToBeReplaced;
}
}
Console.WriteLine(totalSwaps);
}
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);
}
}
}