algo

오늘의 알고리즘 재활

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

실버 5

스택을 사용해 보자.

import java.util.*;
import java.io.*;

public class Main {

    public static void main(String[] args) throws Exception{

       // System.setIn(new FileInputStream("input.txt"));
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        

        StringTokenizer st=new StringTokenizer(br.readLine()," ");
        int N=Integer.parseInt(st.nextToken());
        int M=Integer.parseInt(st.nextToken());
        int[] stackpos=new int[200001];
        List<Deque<Integer>> stackList=new ArrayList<>();

        for(int i=0;i<M;i++) {
            stackList.add(new ArrayDeque<>());
            br.readLine();
            st=new StringTokenizer(br.readLine()," ");
            while(st.hasMoreTokens()) {
                int num=Integer.parseInt(st.nextToken());
                stackpos[num]=i;
                stackList.get(i).push(num);
            }
        }

        // System.out.println(Arrays.toString(stackpos));
        int cursor=1;
        boolean flags=true;
        while(cursor<=N) {
           // System.out.println("peeked one"+stackList.get(stackpos[cursor]).peek());
            if(cursor==stackList.get(stackpos[cursor]).peek()) {
             //   System.out.println(cursor);
                stackList.get(stackpos[cursor]).pop();
                cursor++;
            } else {
               // System.out.println(cursor);
                flags=false;
                break;
            }
        }

        if(flags) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }


    }
}

results matching ""

    No results matching ""