[๋ฐฑ์ค€] ์ฃผ๋ชฝ / 1940๋ฒˆ / ํŒŒ์ด์ฌ / Python
Algorithm/Baekjoon

[๋ฐฑ์ค€] ์ฃผ๋ชฝ / 1940๋ฒˆ / ํŒŒ์ด์ฌ / Python

728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’กsolutions )

๐Ÿ’ฌ ์ฒ˜์Œ์— ๋ฌธ์ œ ํ’€ ๋•Œ ๋ณ„ ์ƒ๊ฐ์—†์ด ํ’€์–ด์„œ,, ์ฃผ์–ด์ง„ ์žฌ๋ฃŒ๋“ค ์ค‘ ๋‘ ์žฌ๋ฃŒ์”ฉ ๋ฌถ์„ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ itertools์˜ combinations์„ ํ†ตํ•ด ๊ตฌํ–ˆ๋‹ค๊ฐ€ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋‹ค. ์ฃผ์–ด์ง„ ์žฌ๋ฃŒ ๊ฐœ์ˆ˜์˜ ๋ฒ”์œ„๊ฐ€ N(1 ≤ N ≤ 15,000)์ธ ๊ฒƒ์„ ๊ฐ์•ˆํ•˜๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚˜๋Š” ๊ฒŒ ๋‹น์—ฐํ•˜๋‹ค. ์ฐธ๊ณ ๋กœ ํŒŒ์ด์ฌ์˜ ์ดˆ๋‹น 2,000๋งŒ๋ฒˆ์˜ ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ํ•œ๋‹ค.

๐Ÿ’ฌ ๋‘ ๋ฒˆ์งธ ์‹œ๋„ -> ํ•ด๋‹น ๋ฌธ์ œ๋Š” ํˆฌํฌ์ธํ„ฐ ๋ฌธ์ œ ์œ ํ˜•์ด๋‹ค.  ๋จผ์ € ์ •๋ ฌ์„ ํ•œ ํ›„ ๋‘ ๊ฐ€์ง€ ์ธ๋ฑ์Šค ๊ฐ’(left๋Š” ๋งจ ์ฒ˜์Œ, right๋Š” ๋งจ ๋ ์žฌ๋ฃŒ๋ฅผ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•จ)์„ ์ •์˜ํ•œ๋‹ค.

๐Ÿ’ฌ left, right๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋‘ ์žฌ๋ฃŒ์˜ ํ•ฉ์ด m๊ณผ ๊ฐ™์œผ๋ฉด answer += 1, m๋ณด๋‹ค ์ž‘์œผ๋ฉด left += 1, m๋ณด๋‹ค ํฌ๋ฉด right -= 1์„ ํ•ด์ฃผ๋ฉฐ ์ธ๋ฑ์Šค๋ฅผ ์กฐ์ •ํ•˜์—ฌ ์žฌ๋ฃŒ์˜ ํฌ๊ธฐ๋ฅผ ์กฐ์ ˆํ•ด์ค€๋‹ค.

 

๐Ÿ‘จ‍๐Ÿ’ปcode )

import sys


input = sys.stdin.readline
n = int(input())
m = int(input())
n_list = list(map(int, input().split()))
n_list.sort()
answer = 0
left, right = 0, n-1
while left < right:
    if n_list[left] + n_list[right] == m:
        answer += 1
        left += 1
        right -= 1
    elif n_list[left] + n_list[right] < m:
        left += 1
    elif n_list[left] + n_list[right] > m:
        right -= 1
print(answer)

 

 

๐Ÿ“Œdescription )

 

1940๋ฒˆ: ์ฃผ๋ชฝ

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

www.acmicpc.net


๋ฌธ์ œ

์ฃผ๋ชฝ์€ ์ฒ ๊ธฐ๊ตฐ์„ ์–‘์„ฑํ•˜๊ธฐ ์œ„ํ•œ ํ”„๋กœ์ ํŠธ์— ๋‚˜์„ฐ๋‹ค. ๊ทธ๋ž˜์„œ ์•ผ์ฒ ๋Œ€์žฅ์„ ํ†ตํ•ด ์ฒ ๊ธฐ๊ตฐ์ด ์ž…์„ ๊ฐ‘์˜ท์„ ๋งŒ๋“ค๊ฒŒ ํ•˜์˜€๋‹ค. ์•ผ์ฒ ๋Œ€์žฅ์€ ์ฃผ๋ชฝ์˜ ๋ช…์— ๋”ฐ๋ฅด๊ธฐ ์œ„ํ•˜์—ฌ ์—ฐ๊ตฌ์— ์ฐฉ์ˆ˜ํ•˜๋˜ ์ค‘ ์•„๋ž˜์™€ ๊ฐ™์€ ์‚ฌ์‹ค์„ ๋ฐœ๊ฒฌํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

๊ฐ‘์˜ท์„ ๋งŒ๋“œ๋Š” ์žฌ๋ฃŒ๋“ค์€ ๊ฐ๊ฐ ๊ณ ์œ ํ•œ ๋ฒˆํ˜ธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๊ฐ‘์˜ท์€ ๋‘ ๊ฐœ์˜ ์žฌ๋ฃŒ๋กœ ๋งŒ๋“œ๋Š”๋ฐ ๋‘ ์žฌ๋ฃŒ์˜ ๊ณ ์œ ํ•œ ๋ฒˆํ˜ธ๋ฅผ ํ•ฉ์ณ์„œ M(1 ≤ M ≤ 10,000,000)์ด ๋˜๋ฉด ๊ฐ‘์˜ท์ด ๋งŒ๋“ค์–ด ์ง€๊ฒŒ ๋œ๋‹ค. ์•ผ์ฒ ๋Œ€์žฅ์€ ์ž์‹ ์ด ๋งŒ๋“ค๊ณ  ์žˆ๋Š” ์žฌ๋ฃŒ๋ฅผ ๊ฐ€์ง€๊ณ  ๊ฐ‘์˜ท์„ ๋ช‡ ๊ฐœ๋‚˜ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”์ง€ ๊ถ๊ธˆํ•ด์กŒ๋‹ค. ์ด๋Ÿฌํ•œ ๊ถ๊ธˆ์ฆ์„ ํ’€์–ด ์ฃผ๊ธฐ ์œ„ํ•˜์—ฌ N(1 ≤ N ≤ 15,000) ๊ฐœ์˜ ์žฌ๋ฃŒ์™€ M์ด ์ฃผ์–ด์กŒ์„ ๋•Œ ๋ช‡ ๊ฐœ์˜ ๊ฐ‘์˜ท์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

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

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๊ฐ‘์˜ท์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.


๋ฐ˜์‘ํ˜•