728x90
1. 링크
https://www.acmicpc.net/problem/3273
2. 난이도(solved.ac 참고)
실버3
3. 풀이
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://www.acmicpc.net/problem/3273 | |
import sys | |
input = sys.stdin.readline | |
n = int(input()) | |
a = list(map(int, input().split())) | |
x = int(input()) | |
count = 0 | |
first = 0 | |
last = len(a)-1 | |
a.sort() | |
while first < last: | |
if a[first] + a[last] > x: | |
last -= 1 | |
elif a[first] + a[last] < x: | |
first += 1 | |
else: | |
count += 1 | |
last -= 1 | |
first += 1 | |
print(count) |
처음에 조합을 이용해서, aC2를 한 두 개의 합이 x일 경우 count를 +1 해줬는데, 그렇게 하니까 시간초과가 떴다.
그래서, sort 후에 투 포인터를 사용해서 합보다 작으면 last 값을 -1, 크면 first 값을 +1, 같으면 last값 감소와 first값 증가를 동시에 해줬다.
'CS > 알고리즘' 카테고리의 다른 글
[Python] BOJ(백준) 11659, 11660번 - 구간 합 구하기(4), (5) (0) | 2021.09.25 |
---|---|
[Python] BOJ(백준) 1912번 - 연속합 (0) | 2021.09.25 |
[Python] BOJ(백준) 2981번 - 검문 (0) | 2021.09.21 |
[Python] BOJ(백준) 17129번 - 윌리암슨수액빨이딱따구리가 정보섬에 올라온 이유 (0) | 2021.09.19 |
[Python] BOJ(백준) 1759번 - 암호 만들기 (0) | 2021.09.17 |