somdaa / algorithmstudy

0 stars 0 forks source link

week-02. Queue-PrinterQueue #7

Open levi-yo opened 6 years ago

levi-yo commented 6 years ago

https://www.acmicpc.net/problem/tag/%ED%81%90

ewqsaz123 commented 6 years ago

import java.util.Iterator; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner;

public class Solution_1966 {

/*
 * 알고리즘 분류. 큐
 */
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    int tc = sc.nextInt();

    for(int i=0; i<tc; i++) {
        int n = sc.nextInt();   //문서 수
        int m = sc.nextInt();   //구할 인덱스
        Queue<Integer> dq = new LinkedList<>();
        for(int j=0; j<n; j++) {
            dq.offer(sc.nextInt());
        }
        findSolution(n,m,dq);
    }
}
static void findSolution(int n, int m, Queue<Integer> dq) {
    Queue<Integer> q = new LinkedList<>();

    int cnt = 0;

    for(int i=0; i<n; i++) {
        q.offer(i);         //인덱스값 큐에 넣기 = 문서 나열
    }

    int output = -1;
    while(output != m) {
        int k = q.poll();
        int o = dq.poll();
        boolean b = false;

        Iterator<Integer> it = dq.iterator();
        while(it.hasNext()) {
            if(it.next() > o) {
                b = true;
                break;
            }
        }

        if(b) {
            q.offer(k);
            dq.offer(o);
            b = false;
        }else {
            output = k;
            cnt++;
        }

    }
    System.out.println(cnt);
}

}

levi-yo commented 6 years ago

package algorism;

import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; import java.util.Stack;

public class PrinterQueue {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Queue<Integer> q=new LinkedList<Integer>();
    Queue<Integer> q1=new LinkedList<Integer>();
    Stack s=new Stack<>();
    ArrayList list=new ArrayList<>();
    Scanner scan=new Scanner(System.in);
    int a=scan.nextInt();
    for(int i=0;i<a;i++){
        int N=scan.nextInt();
        int M=scan.nextInt();
        q.clear();
        q1.clear();
        s.clear();
        for(int j=0;j<N;j++){
            int num=scan.nextInt();
            q.offer(num);
            q1.offer(j);
            s.push(num);
        }
        Collections.sort(s);
        int pollNum=0;
        int count=0;
        while(true){
            if(q.peek()==s.peek()){
                q.poll();
                pollNum=q1.poll();
                s.pop();
                count++;
                if(pollNum==M){
                    break;
                }
            }else{
                q.offer(q.poll());
                q1.offer(q1.poll());
            }
        }
        list.add(count);
    }
    Iterator it=list.iterator();
    while(it.hasNext()) System.out.println(it.next());
}

}