한줄 후기 : 어렵게 생각하지 말자..
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개나 3개를 골라 곱한 값이 Max인 값을 출력하면 된다.
처음에 dp 인가? 하다가 그냥 sort 해서 큰 수 곱하면 되지 했다가 틀렸다. 간과하고 있던건 음수를 두개 곱해도 양수다..
그래서 sort한 후 양수 2개 vs 3개 max 값 찾고, 음수 2개 vs 음수 2개 *양수 1개 max 값 찾아 비교 한 후 더 큰값을 골랐다.
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
int main() {
int n;
cin >> n;
int arr[10001];
for (int i = 0; i<n; i++) {
scanf("%d", &arr[i]);
}
sort(arr, arr + n);
if (arr[n - 1] <= 0) printf("%d\n", arr[0] * arr[1]);
else {
int plus=1;
int minus=1;
if(arr[n-3]*arr[n-2]*arr[n-1] > arr[n-2]*arr[n-1])
plus = arr[n-3]*arr[n-2]*arr[n-1];
else
plus = arr[n-2] * arr[n-1];
minus = max(arr[0] * arr[1], arr[0] * arr[1] * arr[n - 1]);
if (plus > minus) printf("%d\n", plus);
else printf("%d\n", minus);
}
}
'DEV > PS' 카테고리의 다른 글
[10026] 적록색약 , c++ (0) | 2019.11.15 |
---|---|
[17521] ACM-ICPC 2019 C번 - byte coin , c++ (2) | 2019.10.09 |
[1463] 1로 만들기, c++ (0) | 2019.09.17 |
[7785] 회사에 있는 사람 (2) | 2019.09.08 |
[16466] 콘서트, c++ (0) | 2019.09.04 |