1744번: 수 묶기
길이가 N인 수열이 주어졌을 때, 그 수열의 합을 구하려고 한다. 하지만, 그냥 그 수열의 합을 모두 더해서 구하는 것이 아니라, 수열의 두 수를 묶으려고 한다. 어떤 수를 묶으려고 할 때, 위치에
www.acmicpc.net
- 풀이
1. 먼저 수를 오름차순 or 내림차순으로 정렬한다. (작업을 하는 이유 : 수를 묶어주기 편해짐)
2. 양수는 양수끼리, 음수는 음수끼리 묶어주면 합이 커진다. 또한, 음수는 0이랑도 곱해주고 양수는 곱해줄 때 1을 곱하지 않는다.
- 참고 반례
--------------------------------------------------------------------
case1. 양수기준 - (양수가 짝수개일때) 내림차순으로 정렬했을때 큰값 2개씩 곱해서 답을 도출하는가
input : 4 9 4 2 7
output : 71
--------------------------------------------------------------------
case2. 양수기준 - (양수가 홀수개일때) 내림차순으로 정렬했을 때 큰값 2개씩 곱한 후 마지막에 숫자를 더해서 답을 도출하는가
input : 7 9 7 4 2 8 8 7
output : 158
--------------------------------------------------------------------
case3. 음수기준 - 오름차순으로 정렬했을때 작은값 2개씩 묶어서 답을 도출하는가 (이유 : 음수는 작을수록 두 수를 곱했을때 커지므로)
input : 4 -9 -11 -2 -4
output : 107
--------------------------------------------------------------------
case4. 0이 존재하면서 음수의 개수가 홀수개 일때 마지막 음수를 0으로 처리하는지
input : 8 -10 -5 0 0 0 -11 -9 -2
output : 155
--------------------------------------------------------------------
case5. 1이 존재할때 1은 다른수와 곱하지 않고 바로 더하는지 (이유 : 1보다 큰 수 x와 1이 주어진다면 두수를 곱하는것보다 따로 더해주는게 더 크다)
input : 5 5 4 1 1 1
output : 23
- 코드
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.InputMismatchException;
public class Main {
static int N;
static long answer;
static int[] array;
public static void main(String[] args) throws Exception {
SetData();
System.out.println(answer);
}
// 데이터
private static void SetData() throws Exception {
InputReader in = new InputReader(System.in);
N = in.nextInt();
array = new int[N];
answer = 0;
for (int i = 0; i < N; i++)
array[i] = in.nextInt();
Arrays.sort(array);
SaveMaxValue();
}
private static void SaveMaxValue() {
for(int i = N - 2; i >= 0; i-=2) {
if(array[i] <= 0 || array[i + 1] <= 0) break;
if(array[i] > 1 && array[i + 1] > 1) {
array[i] *= array[i+1];
array[i+1] = 0;
}
}
for(int i = 1; i < N; i+=2) {
if(array[i] > 0 || array[i-1] > 0) break;
if(array[i] <= 0 && array[i-1] <= 0) {
array[i] *= array[i-1];
array[i-1] = 0;
}
}
for(int i = 0; i < N; i++)
answer += array[i];
}
}
class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
'algorithm' 카테고리의 다른 글
[BOJ/JAVA] 백준 20183번 : 골목 대장 호석 (0) | 2021.04.02 |
---|---|
[BOJ/JAVA] 백준 20164번 : 홀수 홀릭 호석 (0) | 2021.04.01 |
[BOJ/JAVA] 백준 1715번 : 카드 정렬하기 (0) | 2021.03.30 |
[BOJ/JAVA] 백준 4179번 : 불! (0) | 2021.03.26 |
[BOJ/JAVA] 백준 14888번 : 연산자 끼워넣기 (0) | 2021.03.26 |