๊ธฐ๋ก๋ฐฉ

BOJ_3273 : ๋‘ ์ˆ˜์˜ ํ•ฉ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_3273 : ๋‘ ์ˆ˜์˜ ํ•ฉ

Soom_1n 2023. 1. 12. 10:00

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

3273๋ฒˆ: ๋‘ ์ˆ˜์˜ ํ•ฉ

n๊ฐœ์˜ ์„œ๋กœ ๋‹ค๋ฅธ ์–‘์˜ ์ •์ˆ˜ a1, a2, ..., an์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์ˆ˜์—ด์ด ์žˆ๋‹ค. ai์˜ ๊ฐ’์€ 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 1000000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ์ž์—ฐ์ˆ˜ x๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ai + aj = x (1 ≤ i < j ≤ n)์„ ๋งŒ์กฑํ•˜๋Š”

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • n๊ฐœ์˜ ์ˆ˜์—์„œ ๋‘ ์ˆ˜์˜ ํ•ฉ์ด x๊ฐ€ ๋˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
    • ์ˆ˜์—ด์„ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค.
    • ๋‘ ํฌ์ธํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ•˜๋‚˜๋Š” ๋งจ ์•ž, ํ•˜๋‚˜๋Š” ๋งจ ๋’ค๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด ํ•ฉ์ด x์ธ์ง€ ํ™•์ธํ•˜๋ฉฐ ์นด์šดํŠธํ•œ๋‹ค.
      • ํ•ฉ์ด x๋ณด๋‹ค ์ž‘์œผ๋ฉด ์•ž ํฌ์ธํ„ฐ๋ฅผ +1, ํฌ๋ฉด ๋’ค ํฌ์ธํ„ฐ๋ฅผ -1 ํ•œ๋‹ค.
        • ์•ž ํฌ์ธํ„ฐ๋ฅผ +1ํ•˜๋ฉด ํ•ฉ์ด ์ปค์ง„๋‹ค.
        • ๋’ค ํฌ์ธํ„ฐ๋ฅผ -1ํ•˜๋ฉด ํ•ฉ์ด ์ž‘์•„์ง„๋‹ค.
      • ์•ž ํฌ์ธํ„ฐ์™€ ๋’ค ํฌ์ธํ„ฐ๊ฐ€ ๋งŒ๋‚˜๋ฉด ์ข…๋ฃŒํ•œ๋‹ค.
    • ์นด์šดํŠธํ•œ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		int[] arr = new int[n];

		StringTokenizer st = new StringTokenizer(br.readLine());
		for (int i = 0; i < n; i++) {
			arr[i] = Integer.parseInt(st.nextToken());
		}
		int x = Integer.parseInt(br.readLine());

		Arrays.sort(arr);

		int answer = 0;
		int p1 = 0, p2 = n-1;
		while (p1 != p2) {
			int sum = arr[p1] + arr[p2];
			if (sum == x) {
				p1++;
				answer++;
			} else if (sum < x) {
				p1++;
			} else {
				p2--;
			}

		}
		System.out.println(answer);

	}
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • n๊ฐœ์˜ ์ˆ˜๋ฅผ arr๋ฐฐ์—ด์— ์ €์žฅํ•˜๊ณ , ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค.
  • ์ˆ˜์—ด์˜ ์•ž ํฌ์ธํ„ฐ p1๊ณผ ๋’ค ํฌ์ธํ„ฐ p2๋ฅผ 0๊ณผ n-1๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
  • p1๊ณผ p2๊ฐ€ ๋งŒ๋‚ ๋•Œ๊นŒ์ง€ ๊ณ„์‚ฐ์„ ๋ฐ˜๋ณตํ•œ๋‹ค.
    • sum์— arr[p1]๊ณผ arr[p2]์˜ ํ•ฉ์„ ์ €์žฅํ•œ๋‹ค.
    • sum == x ๋ผ๋ฉด, p1๊ณผ answer๋ฅผ 1์ฆ๊ฐ€์‹œํ‚จ๋‹ค.
    • sum < x ๋ผ๋ฉด, p1์„ 1 ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.
    • sum > x ๋ผ๋ฉด, p2๋ฅผ 1 ๊ฐ์†Œ์‹œํ‚จ๋‹ค.
  • ๋ฐ˜๋ณต์ด ์ข…๋ฃŒ๋˜๋ฉด answer๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ํˆฌ ํฌ์ธํ„ฐ๋ฅผ ์–ด๋–ป๊ฒŒ ์ด์šฉํ•ด์•ผ ํ•  ์ง€ ๋งŽ์ด ๊ณ ๋ฏผํ–ˆ์—ˆ๋‹ค.
    • ์ฒ˜์Œ์— p1๊ณผ p2๋ฅผ 0๊ณผ 1๋กœ ๋‘ฌ์„œ ๋งŽ์ด ํ—ค๋งค๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ, ํ•ฉ์ด ์ปค์ง€๊ณ  ์ž‘์•„์ง€๋„๋ก ํฌ์ธํ„ฐ๋ฅผ ์›€์ง์—ฌ์•ผํ•ด์„œ ๊ฐ์†Œ๋ฅผ ์–ด๋–ป๊ฒŒ ์‹œํ‚ฌ ์ˆ˜ ์žˆ์„๊นŒ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ p2๋ฅผ n-1๋กœ ๋‘๋Š” ๋ฐฉ๋ฒ•์„ ๋– ์˜ฌ๋ ธ๋‹ค.
  • sum == x์—์„œ p1๊ณผ p2๋ฅผ ๋™์‹œ์— ์ˆ˜์ •ํ•ด์„œ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๋กœ 1ํšŒ ํ‹€๋ ธ๋‹ค.
    • p1์„ ๋”ํ•˜๋˜ p2๋ฅผ ๋นผ๋˜ ํ•˜๋‚˜๋งŒ ์ˆ˜ํ–‰ํ•˜๋ฉด ํ•ด๊ฒฐ๋๋‹ค.

728x90

'CodingTest > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ_10757 : ํฐ ์ˆ˜ A+B  (0) 2023.01.12
BOJ_18108 : 1998๋…„์ƒ์ธ ๋‚ด๊ฐ€ ํƒœ๊ตญ์—์„œ๋Š” 2541๋…„์ƒ?!  (0) 2023.01.12
BOJ_2559 : ์ˆ˜์—ด  (0) 2023.01.12
BOJ_25304 : ์˜์ˆ˜์ฆ  (0) 2023.01.12
BOJ_2512 : ์˜ˆ์‚ฐ  (0) 2023.01.11