๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm

[๋ฐฑ์ค€] 1940๋ฒˆ: ์ฃผ๋ชฝ(์ˆ˜ํ•™, ์ •๋ ฌ)

by ์ฃผ๋ฐœ2 2020. 4. 23.
๋ฐ˜์‘ํ˜•

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

 

1940๋ฒˆ: ์ฃผ๋ชฝ

์ฒซ์งธ ์ค„์—๋Š” ์žฌ๋ฃŒ์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 15,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‘ ๋ฒˆ์งธ ์ค„์—๋Š” ๊ฐ‘์˜ท์„ ๋งŒ๋“œ๋Š”๋ฐ ํ•„์š”ํ•œ ์ˆ˜ M(1 ≤ M ≤ 10,000,000) ์ฃผ์–ด์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ ์…‹์งธ ์ค„์—๋Š” N๊ฐœ์˜ ์žฌ๋ฃŒ๋“ค์ด ๊ฐ€์ง„ ๊ณ ์œ ํ•œ ๋ฒˆํ˜ธ๋“ค์ด ๊ณต๋ฐฑ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค. ๊ณ ์œ ํ•œ ๋ฒˆํ˜ธ๋Š” 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.

www.acmicpc.net

์ฝ”๋“œ

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

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		
		int N = Integer.parseInt(bf.readLine());
		int M = Integer.parseInt(bf.readLine());
		int[] arr = new int[N];
		StringTokenizer st = new StringTokenizer(bf.readLine());
		for(int i=0; i<arr.length; i++)
			arr[i] = Integer.parseInt(st.nextToken());
		
		int count = 0;
		for(int i=0; i<arr.length-1; i++) {
			for(int j=i+1; j<arr.length; j++) {
				if(arr[i] + arr[j] == M) {
					count ++;
					break;
				}
			}
		}
		
		System.out.println(count);
		bf.close();
	}

}

ํ’€์ด

์‹œ๊ฐ„ ์ œํ•œ์ด 2์ดˆ์— N์˜ ๋ฒ”์œ„๊ฐ€ 15,000 ์ด์–ด์„œ O(N^2) ์™„์ „ํƒ์ƒ‰์œผ๋กœ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•ด์„œ ํ•ด๊ฒฐํ–ˆ๋‹ค.

for๋ฌธ์˜ ๋ฒ”์œ„๋งŒ ์‹ ๊ฒฝ์จ์ฃผ๊ณ , ๋ชจ๋“  ๋ฐฐ์—ด ๊ฐ’๋“ค์„ ํ™•์ธํ•˜๋ฉด์„œ ๋‘ ๊ฐœ์˜ ์žฌ๋ฃŒ ๊ณ ์œ ๋ฒˆํ˜ธ ํ•ฉ์ด M์ผ ๊ฒฝ์šฐ count ++ ํ•ด์ค€๋‹ค.

 

 

 

 

 

๋‹ค๋ฅธ ํ’€์ด

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
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));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

		int N = Integer.parseInt(br.readLine());
		int M = 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());
		}
		
		Arrays.sort(arr);
		
		int i=0, j=arr.length-1;
		int cnt = 0;
		while(i < j) {
			if(arr[i] + arr[j] == M) {
				i++;
				j--;
				cnt++;
			}
			else {
				if(arr[i] + arr[j] > M) {
					j--;
				}
				else {
					i++;
				}
			}
		}
		
		bw.write(String.valueOf(cnt));
		
		br.close();
		bw.close();
	}
}

์ •๋ ฌ์„ ํ•˜๊ณ , ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’๊ณผ ๊ฐ€์žฅ ํฐ ๊ฐ’์˜ ํ•ฉ์„ ๊ตฌํ•ด์„œ M๊ณผ ๋น„๊ต๋ฅผ ํ•˜๋ฉฐ ํ•ด๊ฒฐํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

๋‘ ๊ฐœ์˜ ํ•ฉ์ด M๊ณผ ๊ฐ™์œผ๋ฉด i ๊ฐ’์„ ์ฆ๊ฐ€ , j ๊ฐ’์„ ๊ฐ์†Œ(์ด๋ถ„ ํƒ์ƒ‰์˜ ๋Š๋‚Œ)์‹œํ‚ค๊ณ ,

๋‘ ๊ฐœ์˜ ํ•ฉ์ด M๋ณด๋‹ค ํด๊ฒฝ์šฐ -> ํ•ฉ์„ ์ค„์—ฌ์•ผ ํ•˜๋ฏ€๋กœ ํฐ๊ฐ’์„ ๊ฐ์†Œ(j--)

๋‘ ๊ฐœ์˜ ํ•ฉ์ด M๋ณด๋‹ค ์ž‘์„๊ฒฝ์šฐ -> ํ•ฉ์„ ์ฆ๊ฐ€์‹œ์ผœ์•ผ ํ•˜๋ฏ€๋กœ ์ž‘์€๊ฐ’์„ ์ฆ๊ฐ€(i++) ์‹œํ‚จ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€