Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. using System.CodeDom.Compiler;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System.Globalization;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Runtime.Serialization;
  11. using System.Text.RegularExpressions;
  12. using System.Text;
  13. using System;
  14.  
  15. class Solution {
  16.  
  17. static void minimumBribes(int[] q)
  18. {
  19. List<int> currentQueueArray = q.ToList();
  20.  
  21. /* List<int> fixedQueue = new List<int>();
  22. fixedQueue.AddRange(currentQueueArray);
  23. fixedQueue.Sort();*/
  24. bool wasTooChaotic = false;
  25. int totalDifference = 0;
  26. int maxVal = q.Length;
  27. int queueLength = maxVal;
  28. int startCheck = maxVal;
  29. int difference;
  30. int startIndexSearchLoop = startCheck;
  31. for (int i = 0; i < queueLength; i++)
  32. {
  33. //Check of opeenvolgende getallen geskipped kunnen worden
  34. for (; startCheck > 0; startCheck--)
  35. {
  36. if (!(startCheck - 1 < 0))
  37. {
  38. if (currentQueueArray[startCheck - 1] != maxVal)
  39. {
  40. maxVal = startCheck;
  41. break;
  42. }
  43. }
  44. if (startCheck - 1 == 0)
  45. {
  46. Console.WriteLine(totalDifference);
  47. //Console.WriteLine("ordered");
  48. return;
  49. }
  50. maxVal--;
  51. }
  52.  
  53. for (startIndexSearchLoop = startCheck - 1; startIndexSearchLoop >= 0; startIndexSearchLoop--)
  54. {
  55. if (currentQueueArray[startIndexSearchLoop] == maxVal)
  56. {
  57. startCheck--;
  58. break;
  59. }
  60. }
  61.  
  62. int indexItShouldBeAt = maxVal - 1;
  63. difference = indexItShouldBeAt - startIndexSearchLoop;
  64. if (difference > 2)
  65. {
  66. Console.WriteLine("Too chaotic");
  67. wasTooChaotic = true;
  68. return;
  69. }
  70. if (difference > 0)
  71. {
  72. totalDifference += difference;
  73. currentQueueArray.RemoveAt(startIndexSearchLoop);
  74. maxVal--;
  75. }
  76.  
  77. }
  78. Console.WriteLine(totalDifference);
  79. }
  80.  
  81. static void Main(string[] args) {
  82. int t = Convert.ToInt32(Console.ReadLine());
  83.  
  84. for (int tItr = 0; tItr < t; tItr++) {
  85. int n = Convert.ToInt32(Console.ReadLine());
  86.  
  87. int[] q = Array.ConvertAll(Console.ReadLine().Split(' '), qTemp => Convert.ToInt32(qTemp))
  88. ;
  89. minimumBribes(q);
  90. }
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement