๐Ÿ•ธ Algorithm/๐Ÿ•ธ ๋ฐฑ์ค€ BaekJoon

[๋ฐฑ์ค€][java][10811][๋ฐ”๊ตฌ๋‹ˆ ๋’ค์ง‘๊ธฐ]

๋‰ด์ด NUEY 2023. 11. 12. 21:52
๋ฐ˜์‘ํ˜•

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {
	// 10811	๋ฐ”๊ตฌ๋‹ˆ ๋’ค์ง‘๊ธฐ
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int[] arr = new int[sc.nextInt()];
		int m = sc.nextInt();
		
	    for (int i = 0; i < arr.length; i++) {
			arr[i] = i+1;
		}
		
		for (int i = 0; i < m; i++) {
			List<Integer> lst = new ArrayList<Integer>();
			
			int a = sc.nextInt();
			int b = sc.nextInt();

			for (int j = 0; j < (b - a + 1); j++) {
				lst.add(j, arr[a - 1+ j]);
			}
			Collections.reverse(lst);
			
			for (int j = 0; j < lst.size(); j++) {
				arr[a - 1 + j] = lst.get(j);
			}
		}
		sc.close();
		
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + " ");
		}
	}
}
  • 12345๋ฅผ arr ๋ฐฐ์—ด์— ๋จผ์ € ๋‹ด์•„์ค๋‹ˆ๋‹ค.
  • ์—ญ์ˆœํ•  ๋ฒ”์œ„๋ฅผ List์— ๋‹ด์•„ ์ค€ ํ›„, Collection.reverse()ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์—ญ์ˆœ์œผ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
  • ํ•ด๋‹น ๋ฒ”์œ„ ์•ˆ์— list์˜ ๊ฐ’์„ ๋‹ด์•„์ค๋‹ˆ๋‹ค.
๋ฐ˜์‘ํ˜•