๐Ÿ—๏ธ Algorithm/๐ŸŸฉ ๋ฐฑ์ค€

๐ŸŸฉ [๋ฐฑ์ค€] [Java] [Silver3] 1966๋ฒˆ_ํ”„๋ฆฐํ„ฐ ํ

Dbswnstjd 2024. 1. 12. 16:54

๋ฌธ์ œ

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

 

1966๋ฒˆ: ํ”„๋ฆฐํ„ฐ ํ

์—ฌ๋Ÿฌ๋ถ„๋„ ์•Œ๋‹ค์‹œํ”ผ ์—ฌ๋Ÿฌ๋ถ„์˜ ํ”„๋ฆฐํ„ฐ ๊ธฐ๊ธฐ๋Š” ์—ฌ๋Ÿฌ๋ถ„์ด ์ธ์‡„ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฌธ์„œ๋ฅผ ์ธ์‡„ ๋ช…๋ น์„ ๋ฐ›์€ ‘์ˆœ์„œ๋Œ€๋กœ’, ์ฆ‰ ๋จผ์ € ์š”์ฒญ๋œ ๊ฒƒ์„ ๋จผ์ € ์ธ์‡„ํ•œ๋‹ค. ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฌธ์„œ๊ฐ€ ์Œ“์ธ๋‹ค๋ฉด Queue ์ž๋ฃŒ๊ตฌ์กฐ์—

www.acmicpc.net

ํ’€์ด

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class ํ”„๋ฆฐํ„ฐํ {
    static int t; // ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜
    static int n; // ๋ฌธ์„œ์˜ ๊ฐœ์ˆ˜
    static int m; // ๊ถ๊ธˆํ•œ ๋ฌธ์„œ์˜ ์ดˆ๊ธฐ ์œ„์น˜
    static Queue<Integer> queue; // ๋ฌธ์„œ์˜ ์ค‘์š”๋„
    static Queue<Integer> indexQueue; // ๋ฌธ์„œ์˜ ์ธ๋ฑ์Šค
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(br.readLine());
        for(int i=0; i<t; i++){
            StringTokenizer st = new StringTokenizer(br.readLine());
            n = Integer.parseInt(st.nextToken());
            m = Integer.parseInt(st.nextToken());

            queue = new LinkedList<>();
            indexQueue = new LinkedList<>();

            st = new StringTokenizer(br.readLine());
            for(int j=0; j<n; j++){
                queue.offer(Integer.parseInt(st.nextToken()));
                indexQueue.offer(j);
            }
            solve();
        }
    }
    public static void solve(){
        int count = 1;

        while (!queue.isEmpty()) {
            int max  = Collections.max(queue);
            int cur = queue.poll();
            int curIndex = indexQueue.poll();

            if(cur == max){
                if (curIndex == m) {
                    System.out.println(count);
                    break;
                }
                count++;
            }
            else{
                queue.offer(cur);
                indexQueue.offer(curIndex);
            }
        }
    }

}