https://www.acmicpc.net/problem/2229
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public int getMaxScoreDif(int[] scores) {
int[] dp = new int[scores.length];
for(int i = 2; i < scores.length; i++) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for(int j = i; j >= 1; j--) {
max = Math.max(max, scores[j]);
min = Math.min(min, scores[j]);
dp[i] = Math.max(dp[i], max - min + dp[j - 1]);
}
}
return dp[scores.length - 1];
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int people_num = Integer.parseInt(br.readLine());
int[] scores = new int[people_num + 1];
String[] score = br.readLine().split(" ");
br.close();
for(int i = 0; i < people_num; i++) {
scores[i + 1] = Integer.parseInt(score[i]);
}
Main m = new Main();
bw.write(m.getMaxScoreDif(scores) + "\n");
bw.flush();
bw.close();
}
}
짜여진 조에서 가장 높은 점수와 가장 낮은 점수를 저장할 max, min 변수를 생성하고 각각 정수에서의 최소값과 정수에서의 최대값으로 초기화합니다.
현재 점수부터 이전 점수들을 하나씩 조에 추가하면서 이전 dp 값들을 이용하여 현재 점수에서의 조가 잘 짜여진 정도의 최댓값을 구합니다.
현재 짜여진 조에서의 가장 높은 점수와 가장 낮은 점수를 찾습니다.
현재 점수에 해당하는 순서의 dp 배열에
현재 해당 dp 배열에 저장되어있는 값,
현재 조에서 조가 잘 짜여진 정도 + 현재 조가 짜여지기 전까지의 조가 잘 짜여진 정도의 최댓값(dp[j - 1])
중 더 큰 값을 현재 점수에 해당하는 순서의 dp 배열의 값으로 취합니다.
2번 과정을 마지막 점수까지 진행합니다.