Created
August 15, 2018 16:46
-
-
Save vanduc1102/be96fc8549eda2e01d58d9c6cf845728 to your computer and use it in GitHub Desktop.
New Year Chaos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
process.stdin.resume(); | |
process.stdin.setEncoding('utf-8'); | |
let inputString = ''; | |
let currentLine = 0; | |
process.stdin.on('data', inputStdin => { | |
inputString += inputStdin; | |
}); | |
process.stdin.on('end', _ => { | |
inputString = inputString.replace(/\s*$/, '') | |
.split('\n') | |
.map(str => str.replace(/\s*$/, '')); | |
main(); | |
}); | |
function readLine() { | |
return inputString[currentLine++]; | |
} | |
// Complete the minimumBribes function below. | |
function minimumBribes(q) { | |
let n = q.length; | |
let counter = 0; | |
let tracks = Array.from({ length:q.length }, () => 0); | |
let swapped = 0; | |
do{ | |
swapped = 0; | |
for(let i = 0 ; i < n - 1 ; i++ ){ | |
let ticketNo = q[i] - 1; | |
if(tracks[ticketNo] < 2 && q[i] > q[i+1] ){ | |
let tempx = q[i+1]; | |
q[i+1] = q[i]; | |
q[i] = tempx; | |
counter ++; | |
tracks[ticketNo] ++; | |
swapped++; | |
} | |
} | |
}while( swapped != 0 ); | |
for(let i = 0 ; i < n ; i++){ | |
if(q[i] != i+1){ | |
counter = "Too chaotic"; | |
break; | |
} | |
} | |
console.log(counter); | |
} | |
function main() { | |
const t = parseInt(readLine(), 10); | |
for (let tItr = 0; tItr < t; tItr++) { | |
const n = parseInt(readLine(), 10); | |
const q = readLine().split(' ').map(qTemp => parseInt(qTemp, 10)); | |
minimumBribes(q); | |
} | |
// console.log(3); | |
//console.log("Too chaotic") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment