-
Notifications
You must be signed in to change notification settings - Fork 0
Closed
Labels
Description
- 묶이는 수의 갯수는 두 개
아이디어
- 오름차순 정렬
- 음수, 0, 양수를 기준으로
- 양수) 최대한 큰 수 끼리 두 수를 서로 곱해 나감
양수값 주의 사항 - 1과 결합될 때는 곱하지말고 더해야 더 큰값이다. - 0 ) 0은 마지막에 음수가 하나 남는다면 0으로 곱해서 없앰
- 음수 )
음수 * 음수 = 양수이므로 최대한 짝수 개는 곱해줌
- 예시
-1 -2 -3 0 1 2 3
→
(-1 * 0) + (-2 * -3) + 1 + (2 * 3) = 13
어려운점 & 실수
-
모든 값을 넣고 기본 오름차순 정렬하면
-1 -3 -2 1 3 2 0→-3 -2 -1 0 1 2 3이 된다.
⇒ 이 상태에서 뒤에서부터 쭉 계산하면 (-3 * 0) + (-2 * -1) + 1 + (2 * 3)이 된다.
음수는 별개로 분리 계산 -
⭐️ 주의할 조건 ⭐️
"1과 2는 곱하는 것 보단 더하는게 더 크다"
⇒ 1의 경우에는 곱하지말고 더해야함!!!!
1 2⇒ 3
1 1⇒ 2
정답
public class N11502 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int T = Integer.parseInt(br.readLine());
while (T-- > 0) {
int N = Integer.parseInt(br.readLine());
int[] arr = new int[N];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
long total = 0;
int max = arr[N - 1];
for (int i = N - 2; i >= 0; i--) {
if (arr[i] < max) {
total += max - arr[i];
} else {
max = arr[i];
}
}
System.out.println(total);
}
}
}Reactions are currently unavailable