티스토리 뷰

728x90

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

 

14753번: MultiMax

There are n cards, each with an integer on it where two or more cards can have the same integer. From these cards, we want to select two or three cards such that the product of numbers on the selected cards is maximum. For example, assume that there are 6

www.acmicpc.net

 

반복문으로 모든 값들을 비교하여 최댓값을 구하면 시간 초과가 발생하기 때문에

정렬 후 특정 경우에 대한 값들을 취합하여 비교하면 됩니다.

 

2개를 고를 때

1. list[0] * list[1] : 음수 * 음수가 최댓값일 경우

3. list[-2] * list[-1] : 양수 * 양수가 최댓값일 경우

 

3개를 고를 때

1. list[0] * list[1] * list[-1] : 음수 * 음수 * 양수가 최댓값

2. list[-3] * list[-2] * list[-1] : 양수 * 양수 * 양수가 최댓값

 

1
2
3
4
5
6
7
import sys
= int(sys.stdin.readline())
lst = list(map(int, sys.stdin.readline().split()))
lst.sort()
lst2 = [lst[0]*lst[1], lst[-2]*lst[-1]]
lst3 = [lst[-3]*lst[-2]*lst[-1], lst[0]*lst[1]*lst[-1]]
print(max(max(lst2), max(lst3)))
cs

 

댓글