Jun's Development Journey

[BOJ] 2531 회전초밥 본문

BOJ/Two Pointer

[BOJ] 2531 회전초밥

J_Jayce 2021. 2. 22. 13:21

문제

www.acmicpc.net/problem/2531

 

2531번: 회전 초밥

첫 번째 줄에는 회전 초밥 벨트에 놓인 접시의 수 N, 초밥의 가짓수 d, 연속해서 먹는 접시의 수 k, 쿠폰 번호 c가 각각 하나의 빈 칸을 사이에 두고 주어진다. 단, 2 ≤ N ≤ 30,000, 2 ≤ d ≤ 3,000, 2 ≤

www.acmicpc.net

 

풀이

1) 처음 풀이 코드(Two Pointer X)

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

public class Main {
	static int max = -1;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N,d,k,c, cnt,dishes[];
        boolean[] chk;
        //입력
        String op = br.readLine();
        StringTokenizer st = new StringTokenizer(op," ");
        N = Integer.parseInt(st.nextToken());
        d = Integer.parseInt(st.nextToken());
        k = Integer.parseInt(st.nextToken());
        c = Integer.parseInt(st.nextToken());
        chk = new boolean[d+1];
        dishes = new int[N*2];
        
        for(int i=0;i<N;i++) {
        	int num = Integer.parseInt(br.readLine());
        	dishes[i] = dishes[i+N] = num;
        }
        
        //계산
        for(int i=0;i<2*N-k;i++) {
        	Arrays.fill(chk, false);
        	cnt=0;
        	for(int j=0;j<k;j++) {
        		if(!chk[dishes[i+j]]) {
        			chk[dishes[i+j]] = true;
        			cnt++;
        		}
        		//System.out.print(dishes[i+j]+" ");
        	}
        	//System.out.println();
        	if(!chk[c])
        		cnt++;
        	max = cnt>max?cnt:max;
        }
        System.out.println(max);
    }
} 

2) 추가 풀이 방법 코드(Two Pointer O)

 

 

'BOJ > Two Pointer' 카테고리의 다른 글

[BOJ] 2470번 두 용액  (0) 2021.04.21
[BOJ] 17609 회문  (0) 2021.03.03
[BOJ] 6159 코스튬 파티  (0) 2021.02.23