Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
- {
- List<int> currentQueueArray = q.ToList();
- /* List<int> fixedQueue = new List<int>();
- fixedQueue.AddRange(currentQueueArray);
- fixedQueue.Sort();*/
- bool wasTooChaotic = false;
- int totalDifference = 0;
- int maxVal = q.Length;
- int queueLength = maxVal;
- int startCheck = maxVal;
- int difference;
- int startIndexSearchLoop = startCheck;
- for (int i = 0; i < queueLength; i++)
- {
- //Check of opeenvolgende getallen geskipped kunnen worden
- for (; startCheck > 0; startCheck--)
- {
- if (!(startCheck - 1 < 0))
- {
- if (currentQueueArray[startCheck - 1] != maxVal)
- {
- maxVal = startCheck;
- break;
- }
- }
- if (startCheck - 1 == 0)
- {
- Console.WriteLine(totalDifference);
- //Console.WriteLine("ordered");
- return;
- }
- maxVal--;
- }
- for (startIndexSearchLoop = startCheck - 1; startIndexSearchLoop >= 0; startIndexSearchLoop--)
- {
- if (currentQueueArray[startIndexSearchLoop] == maxVal)
- {
- startCheck--;
- break;
- }
- }
- int indexItShouldBeAt = maxVal - 1;
- difference = indexItShouldBeAt - startIndexSearchLoop;
- if (difference > 2)
- {
- Console.WriteLine("Too chaotic");
- wasTooChaotic = true;
- return;
- }
- if (difference > 0)
- {
- totalDifference += difference;
- currentQueueArray.RemoveAt(startIndexSearchLoop);
- maxVal--;
- }
- }
- Console.WriteLine(totalDifference);
- }
- 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);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement