Competitive/Collaborative Programming Class

ICPC Computer Programming Contest Prep

Problem Solving in Computer Science

Spring 2015 -- CSC 2700 Section 01

[Isaac's Home Page ]  [Mailing List ]  [Class Page ]  [Normal ]  

week08/i/Rails.java

/*
 *  Author: Son Pham
 *    Date: March 5, 2013
 * Purpose: Solves this problem using a straightforward approach.
 * Problem: 514 - Rails
 * Comment: Think linked list. This code sure is ugly though...
 */

import java.util.*;

public class Rails {

    public static void main(String[] args) {
        
        Scanner reader = new Scanner(System.in);
        int block = reader.nextInt();
        reader.nextLine();
        while(block != 0){
            boolean yes = true;
            int n = block;
            String permutation = reader.nextLine();
            String[] carts = permutation.split("\\s+");
            int[] station = new int[1000];
            int stationIndex = 0;
            int a = 1;
            int cartnum = Integer.parseInt(carts[0]);
            if(cartnum == 0){
                System.out.println();
                block = reader.nextInt();
                reader.nextLine();
                continue;
            }
            for(int i = 0; i < carts.length; i++){ 
                if(i > 0)
                    cartnum = Integer.parseInt(carts[i]);
                //System.out.println("a = "+a+" cart = "+cartnum);
                if(cartnum < a){
                    if(station.length > 0){
                        if(cartnum == station[stationIndex-1]){
                            //System.out.print("pop "+station[stationIndex-1]);
                            stationIndex--;
                        }
                        else{
                            yes = false;
                            break;
                        }
                    }
                    else{
                        yes = false;
                        break;
                    }
                }
                else if(cartnum > a){
                    for(; a < cartnum; a++){
                        station[stationIndex] = a;
                        //System.out.println("push "+a+" to cart"+stationIndex);
                        stationIndex++;
                    }
                    a++;
                }
                else
                    a++;
            }
            if(yes)
                System.out.println("Yes");
            else
                System.out.println("No");
        }
        
    }
}

The statements and opinions included in these pages are those of only. Any statements and opinions included in these pages are not those of Louisiana State University or the LSU Board of Supervisors.
© 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015