somdaa / algorithmstudy

0 stars 0 forks source link

week-02. Stack-Tower #6

Open ewqsaz123 opened 6 years ago

ewqsaz123 commented 6 years ago

https://www.acmicpc.net/problem/2493

levi-yo commented 6 years ago

package algorism;

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

public class StackTower {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Stack<Integer> stack=new Stack<Integer>(); //탑을 담을 스택
    Stack<Integer> stack1=new Stack<Integer>(); //탑의 인덱스번호를 담는 스택
    Scanner scan=new Scanner(System.in);
    int n=scan.nextInt();
    StringBuffer sb=new StringBuffer();
    stack.push(scan.nextInt());
    stack1.push(1);
    sb.append(0+" ");
    for(int i=1;i<n;i++){
        int tNum=scan.nextInt();
        while(!stack.isEmpty()){
            if(stack.peek()>tNum){
                sb.append(stack1.peek()+" ");
                break;
            }else{
                stack.pop();
                stack1.pop();
                if(stack.isEmpty()){
                    sb.append(0+" ");
                }
            }
        }
        stack.push(tNum);
        stack1.push(i+1);
    }
    System.out.println(sb);
}

}