Thursday, 13 August 2015

STPAR - Street Parade



For sure, the love mobiles will roll again on this summer's street parade. Each year, the organizers decide on a fixed order for the decorated trucks. Experience taught them to keep free a side street to be able to bring the trucks into order.

The side street is so narrow that no two cars can pass each other. Thus, the love mobile that enters the side street last must necessarily leave the side street first. Because the trucks and the ravers move up closely, a truck cannot drive back and re-enter the side street or the approach street.

You are given the order in which the love mobiles arrive. Write a program that decides if the love mobiles can be brought into the order that the organizers want them to be.

Input

There are several test cases. The first line of each test case contains a single number n, the number of love mobiles. The second line contains the numbers 1 to n in an arbitrary order. All the numbers are separated by single spaces. These numbers indicate the order in which the trucks arrive in the approach street. No more than 1000 love mobiles participate in the street parade. Input ends with number 0.

Output

For each test case your program has to output a line containing a single word yes if the love mobiles can be re-ordered with the help of the side street, and a single word no in the opposite case.

Example

Sample input:
5
5 1 2 4 3 
0

Sample output:
yes

Illustration

The sample input reflects the following situation:
The five trucks can be re-ordered in the following way:
    

TEST CASES  ( Try these )
5
4 1 5 3 2
5
3 1 2 5 4
5
5 3 2 1 4
10
1 2 10 5 4 3 7 6 8 9
10
1 2 10 5 4 3 9 8 7 6
5
3 5 2 4 1
5
1 2 4 3 5
4
4 2 3 1
no
yes
yes
yes
yes
no
yes
no


LOGIC TO SOLVE THIS

On first  we will make declarations .
Now when we have  got input from user ,in the way the love mobiles have arrived.
Store it in an array-list

next copy it to a new araylist  by making a clone of it ( Do not go for copying array-list )
for eg:
{

ArrayList<Integer> carno=new ArrayList<Integer>(5);
ArrayList<Integer> copy_carno= carno;

}
As the reference variable copy_carno now points the same object and it will be problem .
so make clone of it
Refer to code given below.

and now sort the array list you have just cloned (copy_carno )
using Collection.sort(copy_carno);

Now comes the logic part :

Pick up first element from arraylist ( in which input is stored  : carno )
compare it with the cloned ( copy_carno) arraylist and get its index from (copy_carno)

So from sorted arraylist( copu_carno) we get to know  that at what is its position in given love mobiles 
if  its position is 0 then we will comapare it with the elements inserted in stack (stac) [refer code below].
Since for first run theres no element in stack
insert it into YOUR final  arraylist ( propercar )  [refer code below]. 

we have inserted a max value at its  [zeroth postion]. (INITIALLY).  [refer code below].

And  now remove the element from the sorted arraylist (copy_carno) .

 Iterate again fro second element  ,if its index is now other than 1 insert it into stack (stac) .
Because if is not positioned 0 in list (copy_carno) then it must be greater than other element.
now compare it with stack elements, 
if it is greater than them
      pop elements from stack till its greater than the stack  elements 
else 
     store it in stack
       

if elemnet is indexed zero comapre again with stack elemnts 

  if it is greater than them
       pop elements from stack till its greater than the stack  elements 
  else
       add it to final araylist ( proper_carno). 

and remove element again  from  (copy_carno)


Iterate all elements and at last pop all elemnts from stack and inster it into proper_carno 

Print the final list and if its not sorted answer is  NO.



JAVA CODE 


package com.spoj;
import java.util.Collections;
import java.util.ArrayList;
import java.util.Scanner;

public class street_parade {

 public static void main(String[] args) {
  // TODO Auto-generated method stub

  Scanner sc=new Scanner(System.in);
  System.out.println("enter no. of elemnts to b inserteed");
  int n=sc.nextInt();
  
  ArrayList<Integer> carno=new ArrayList<Integer>(n);       //input from user goes here
  ArrayList<Integer> sortcarno=new ArrayList<Integer>(n);    //sorted input 
  ArrayList<Integer> propercar=new ArrayList<Integer>(n);    //for  programming purpose 
  Integer[] stac=new Integer[n];  //array
  stac[0]=Integer.MAX_VALUE;  //for  1-time comparison 
  int s=0;
  Integer temp;
  int index;
  
  
  for(int k=0;k<n;k++)  // input taken
  {
   temp=sc.nextInt();
   carno.add(temp);  
  }
 
  sortcarno= (ArrayList<Integer>) carno.clone();   // list is copied
  Collections.sort(sortcarno);        // sorted the input list in sortcarno
  
  for(int k=0;k<n;k++)   // logic for program
  {
   temp=carno.get(k);
   index=sortcarno.indexOf(temp);   
   
   if(index==0)
   {
    if(temp<stac[s])
     propercar.add(temp);
    else
    {
     while(temp>stac[s])
      propercar.add(stac[s--]);
     propercar.add(temp);
    }
   }
   else
   { 
    if(temp<stac[s])
     stac[++s]=temp;
    else
    {
     while(temp>stac[s])
      propercar.add(stac[s--]);
     stac[++s]=temp;
    }
   }
   
   sortcarno.remove(temp);  
  }
  
  while(s>=1)
    propercar.add(stac[s--]);  
  
  System.out.println();
  System.out.println(propercar);
  
  sc.close();
 }

}

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. package spoj.stack;

    import javafx.application.Application;
    import javafx.stage.Stage;

    import java.util.Scanner;
    import java.util.Stack;

    public class stpar {

    public static void main(String[] args)
    {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[] arr = new int[n];
    for(int i=0;i stack = new Stack<>();
    int a[] = new int[arr.length];
    stack.push(arr[0]);
    int counter = 0;

    for(int i=1;i<arr.length;i++)
    {
    while (!stack.isEmpty() && stack.peek() < arr[i])
    {
    // System.out.println(stack.pop());
    a[counter++] = stack.pop();
    }
    stack.push(arr[i]);
    }

    while (!stack.isEmpty())
    {
    a[counter++] = stack.pop();
    }

    int checker = 1;

    for(int i=0;i<a.length-1;i++)
    {

    if(a[i]+1 != a[i+1])
    {
    checker = 0;
    break;
    }

    }

    if(checker == 1)
    System.out.println("yes");
    else
    System.out.println("no");



    }


    }





    //i did this and all above testcases are correct wrt my code. idk where i am doing mistake plz help me

    ReplyDelete