baekjoon 20251204
This commit is contained in:
parent
bebf5a9cae
commit
96b18191a8
35
code_study/Baekjoon/python/2473.py
Normal file
35
code_study/Baekjoon/python/2473.py
Normal file
@ -0,0 +1,35 @@
|
||||
import sys
|
||||
input = sys.stdin.readline
|
||||
|
||||
N = int(input())
|
||||
value = sorted(list(map(int, input().split())))
|
||||
|
||||
def twoPointer(fixIndex) :
|
||||
absSum = 30**9
|
||||
fixNum = value[fixIndex]
|
||||
L, vL, R, vR = fixIndex+1, value[fixIndex+1], len(value)-1, value[len(value)-1]
|
||||
|
||||
while L != R :
|
||||
temp = value[L] + value[R] + fixNum
|
||||
|
||||
if absSum > abs(temp) :
|
||||
absSum = abs(temp)
|
||||
vL = value[L]
|
||||
vR = value[R]
|
||||
|
||||
if temp > 0 :
|
||||
R -= 1
|
||||
else :
|
||||
L += 1
|
||||
|
||||
return absSum, (fixNum, vL, vR)
|
||||
|
||||
resultValue = 30**9
|
||||
resultPair = (0,0,0)
|
||||
for i in range(len(value)-2) :
|
||||
a, b = twoPointer(i)
|
||||
if resultValue > a :
|
||||
resultPair = b
|
||||
resultValue = a
|
||||
|
||||
print(" ".join(map(str, resultPair)))
|
||||
30
code_study/Baekjoon/swift/2342_1.swift
Normal file
30
code_study/Baekjoon/swift/2342_1.swift
Normal file
@ -0,0 +1,30 @@
|
||||
if let input = readLine(),
|
||||
let seq = input.split(separator: " ").compactMap({Int($0)}) as? [Int]
|
||||
{
|
||||
let force: [[Int]] = [
|
||||
[1,2,2,2,2],
|
||||
[2,1,3,4,3],
|
||||
[2,3,1,3,4],
|
||||
[2,4,3,1,3],
|
||||
[2,3,4,3,1]
|
||||
]
|
||||
|
||||
var dp: [[[Int]]] = Array(repeating: Array(repeating: Array(repeating: 0, count: 5), count: 5), count: seq.count-1)
|
||||
|
||||
func solve(_ step: Int, _ l: Int, _ r: Int) -> Int {
|
||||
if step == seq.count-1 { return 0 }
|
||||
|
||||
if dp[step][l][r] != 0 { return dp[step][l][r] }
|
||||
|
||||
let target = seq[step]
|
||||
|
||||
let moveLeftCost = solve(step + 1, target, r) + force[l][target]
|
||||
let moveRightCost = solve(step + 1, l, target) + force[r][target]
|
||||
|
||||
dp[step][l][r] = min(moveLeftCost, moveRightCost)
|
||||
|
||||
return dp[step][l][r]
|
||||
}
|
||||
|
||||
print(solve(0, 0, 0))
|
||||
}
|
||||
0
code_study/Baekjoon/swift/2342_2.swift
Normal file
0
code_study/Baekjoon/swift/2342_2.swift
Normal file
38
code_study/Baekjoon/ts/2342_1.ts
Normal file
38
code_study/Baekjoon/ts/2342_1.ts
Normal file
@ -0,0 +1,38 @@
|
||||
const inst: number[] = require("fs").readFileSync(0).toString().trim().split(" ").map(Number);
|
||||
const steps: number = inst.length - 1;
|
||||
|
||||
const force: number[][] = [
|
||||
[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]
|
||||
];
|
||||
|
||||
let dp: number[][][] = Array.from( {length: 2}, (_, i) =>
|
||||
Array.from({length: 5}, () =>
|
||||
Array.from({length: 5}, () =>
|
||||
i===1 ? 0 : Infinity
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
let currentIsZero: boolean = true;
|
||||
|
||||
for(let i=steps-1; i >= 0; i--) {
|
||||
const current: number = currentIsZero ? 0 : 1;
|
||||
const next: number = currentIsZero ? 1 : 0;
|
||||
const target: number = inst[i];
|
||||
|
||||
for(let l=0; l<5; l++) {
|
||||
for(let r=0; r<5; r++) {
|
||||
const moveLeft: number = dp[next][target][r] + force[l][target];
|
||||
const moveRight: number = dp[next][l][target] + force[r][target];
|
||||
dp[current][l][r] = Math.min(moveLeft, moveRight);
|
||||
}
|
||||
}
|
||||
|
||||
currentIsZero = !currentIsZero;
|
||||
}
|
||||
|
||||
console.log(dp[currentIsZero ? 1 : 0][0][0]);
|
||||
Loading…
x
Reference in New Issue
Block a user