acmicpc

[BAEKJOON] 1244. 스위치 켜고 끄기

코드와이 2021. 2. 6. 23:31

 

문제링크

www.acmicpc.net/problem/1244

 

1244번: 스위치 켜고 끄기

첫째 줄에는 스위치 개수가 주어진다. 스위치 개수는 100 이하인 양의 정수이다. 둘째 줄에는 각 스위치의 상태가 주어진다. 켜져 있으면 1, 꺼져있으면 0이라고 표시하고 사이에 빈칸이 하나씩

www.acmicpc.net

package acmicpc;

import java.util.Scanner;

public class 스위치켜고끄기 {
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		int T = sc.nextInt();
		
		int[] arr = new int[T+1];
		arr[0] = -1;
		
		for (int i = 1 ; i < T+1 ; i++) {
			arr[i] = sc.nextInt();
		}
		
		int n = sc.nextInt();
		
		for( int l = 0 ; l < n ; l++) {
			int a = sc.nextInt();
			int m = sc.nextInt();
			
			if ( a == 1) {
				int t = m;
				while (m < T + 1) {
					
					arr[m] = (arr[m] + 1) % 2;
					
					m += t;
					
				}
			}
			else {
			
				int w2 = m;
				while (true) {
					
					if ( m - 1 > 0 && w2 + 1 <= T && arr[m - 1] == arr[w2 + 1]) {
						m -= 1;
						w2 += 1;
					} else break;
					
				}
				
				for(int i = m ; i <= w2 ; i++) {
					arr[i] = (arr[i] + 1) % 2;
				}
			}
		}
		
		for (int i = 1 ; i <= T ; i++) {
			System.out.print(arr[i] + " ");
			if (i % 20 == 0) System.out.println();
		}
		
	}
}