baekjoon 20251231

This commit is contained in:
songyc macbook 2025-12-31 22:01:12 +09:00
parent 2a8d73edc8
commit fbbbaeb8e6
3 changed files with 95 additions and 0 deletions

View File

@ -0,0 +1,52 @@
import java.util.*;
public class _2342 {
static int INF = Integer.MAX_VALUE;
static int[][] move_energy = {
{0,2,2,2,2},
{0,1,3,4,3},
{0,3,1,3,4},
{0,4,3,1,3},
{0,3,4,3,1}
};
static int[][][] dp = new int[2][5][5];
static int curr = 0, next = 1;
static void swap() {
int temp = curr;
curr = next;
next = temp;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] inst = Arrays.stream(sc.nextLine().split(" "))
.mapToInt(Integer::parseInt)
.toArray();
sc.close();
for(int i=0; i<2; i++) {
for(int l=0; l<5; l++) {
for(int r=0; r<5; r++) {
if(i==0) dp[i][l][r] = INF;
else dp[i][l][r] = 0;
}
}
}
for(int i=inst.length-2; i>=0; i--) {
int target = inst[i];
for(int L=0; L<5; L++) {
for(int R=0; R<5; R++) {
int moveLeft = dp[next][target][R] + move_energy[L][target];
int moveRight = dp[next][L][target] + move_energy[R][target];
dp[curr][L][R] = Math.min(moveLeft, moveRight);
}
}
swap();
}
System.out.println(dp[next][0][0]);
}
}

View File

@ -0,0 +1,42 @@
import java.util.*;
public class _2342_1 {
static int[][][] dp;
static int[] inst;
static int inst_num;
static int[][] move_energy = {
{1,2,2,2,2},
{0,1,3,4,3},
{0,3,1,3,4},
{0,4,3,1,3},
{0,3,4,3,1}
};
static int solve(int L, int R, int step) {
if(step == inst_num) return 0;
if(dp[step][L][R] == 0) {
int target = inst[step];
int moveLeft = solve(target, R, step+1) + move_energy[L][target];
int moveRight = solve(L, target, step+1) + move_energy[R][target];
dp[step][L][R] = Math.min(moveLeft, moveRight);
}
return dp[step][L][R];
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
inst = Arrays.stream(sc.nextLine().split(" "))
.mapToInt(Integer::parseInt)
.toArray();
sc.close();
inst_num = inst.length-1;
dp = new int[inst_num][5][5];
System.out.println(solve(0, 0, 0));
}
}

View File

@ -0,0 +1 @@
print(max([sum(list(map(int, input().split()))) for _ in range(2)]))