New Year Chaos


Problem Statement :


It is New Year's Day and people are in line for the Wonderland rollercoaster ride. Each person wears a sticker indicating their initial position in the queue from 1 to n. Any person can bribe the person directly in front of them to swap positions, but they still wear their original sticker. One person can bribe at most two others.

Determine the minimum number of bribes that took place to get to a given queue order. Print the number of bribes, or, if anyone has bribed more than two people, print Too chaotic.


Function Description

Complete the function minimumBribes in the editor below.

minimumBribes has the following parameter(s):

int q[n]: the positions of the people after all bribes
Returns

No value is returned. Print the minimum number of bribes necessary or Too chaotic if someone has bribed more than 2  people.


Input Format

The first line contains an integer t, the number of test cases.

Each of the next t pairs of lines are as follows:
- The first line contains an integer t, the number of people in the queue
- The second line has n space-separated integers describing the final state of the queue.

Constraints


1  <=  t  <=  10 
1  <=  n  <=  10^5



Sample Input

STDIN                 Function
-----                       --------
2                         t = 2
5                         n = 5
2 1 5 3 4            q = [2, 1, 5, 3, 4]
5                         n = 5
2 5 1 3 4            q = [2, 5, 1, 3, 4]



Sample Output

3
Too chaotic



Solution :



title-img


                            Solution in C :

In   C  :






#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    int f,T,tmp,i,j,k,ans,c; 
    scanf("%d",&T);
    for(int a0 = 0; a0 < T; a0++){
        int n;
        ans=c=f=0;
        scanf("%d",&n);
        int *q = malloc(sizeof(int) * n);
        for(int q_i = 0; q_i < n; q_i++){
           scanf("%d",&q[q_i]);
        }
for(i=0;i<n-1;i++)
    {
    
    for(j=i+1;q[i]>q[j]&&j<n;j++)
        {
        ans++;
        c++;
       // printf("i=%d,ans=%d,c=%d\n",i,ans,c);
       
        if(c>2)
            {
         f=1;
            break;
        }
    }
    c=0;
    if(f==1)
        break;
}
        if(f==1)
            printf("Too chaotic\n");
        else
           {
            ans=0;
            for(i=1;i<n;i++)
                {
                k=q[i];
                for(j=i-1;j>=0&&k<q[j];j--)
                    {q[j+1]=q[j];
                    ans++;}
                q[j+1]=k;
            }
        printf("%d\n",ans);
        }
        
    }
    return 0;
}
                        


                        Solution in C++ :

In   C++  :







#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;


int main(){
    int T;
    cin >> T;
    for(int a0 = 0; a0 < T; a0++){
        int n;
        cin >> n;
        vector<int> q(n);
        for(int q_i = 0;q_i < n;q_i++){
           cin >> q[q_i];
        }
        int ans = 0;
        for (int i = n - 1; i >= 0; i--){
            if (ans == -1)
                break;
            int k = i;
            while (q[k] != i + 1)
                k--;
            if (i - k > 2){
                ans = -1;
                break;
            } else {
                while (k != i){
                    swap(q[k], q[k + 1]);
                    k++;
                    ans++;
                }
            }
        }
        if (ans == -1)
            puts("Too chaotic");
        else
            cout << ans << "\n";
    }
    return 0;
}
                    


                        Solution in Java :

In   Java  :






import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        for(int a0 = 0; a0 < T; a0++){
            int n = in.nextInt();
            int q[] = new int[n];
            for(int q_i=0; q_i < n; q_i++){
                q[q_i] = in.nextInt();
            }
            
            int bribes = 0;
            for (int i = 1; i <= n; ++i) {
                 if (q[i - 1] - i > 2) {
                     System.out.println("Too chaotic");
                     bribes = -1;
                     break;
                 }
            }
            
            if (bribes == 0)
            {
                for (int i = 0; i < n; ++i) {
                    for (int j = i + 1; j < i + 100 && j < n; ++j) {
                        if (q[j] < q[i]) {
                            bribes++;
                        }
                    }
                }
                System.out.println(bribes);
            }
        }
    }
}
                    


                        Solution in Python : 
                            
In   Python3  :






#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the minimumBribes function below.
def minimumBribes(q):
    m = 0
    Q = [i-1 for i in q]
    for i,j in enumerate(Q):
        if j-i > 2:
            print('Too chaotic')
            return
        for k in range(max(j-1, 0), i):
            if Q[k] > j:
                m += 1
    print(m)

if __name__ == '__main__':
    t = int(input())

    for t_itr in range(t):
        n = int(input())

        q = list(map(int, input().rstrip().split()))

        minimumBribes(q)
                    


View More Similar Problems

Waiter

You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in from top to bottom in answers. In the next iteration, do the

View Solution →

Queue using Two Stacks

A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que

View Solution →

Castle on the Grid

You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):

View Solution →

Down to Zero II

You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.

View Solution →

Truck Tour

Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr

View Solution →

Queries with Fixed Length

Consider an -integer sequence, . We perform a query on by using an integer, , to calculate the result of the following expression: In other words, if we let , then you need to calculate . Given and queries, return a list of answers to each query. Example The first query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . The secon

View Solution →