[Gold IV] Title: A[j]-A[i]+A[l]-A[k], Time: 1192 ms, Memory: 39140 KB -BaekjoonHub

This commit is contained in:
SSUM
2026-02-10 18:59:43 +09:00
parent 1316e383f8
commit 610838a3ca
2 changed files with 80 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
import sys
data = sys.stdin.buffer.read()
p = 0
L = len(data)
def readint():
global p
while p < L and data[p] <= 32:
p += 1
num = 0
while p < L and data[p] > 32:
num = num * 10 + (data[p] - 48)
p += 1
return num
N = readint()
a0 = readint()
a1 = readint()
NEG = -10**30
minA = a0
best1 = a1 - minA
minA = a1 if a1 < minA else minA
best2 = NEG
ans = NEG
for t in range(2, N):
x = readint()
if t >= 3:
v = best2 + x
if v > ans:
ans = v
v2 = best1 - x
if v2 > best2:
best2 = v2
v1 = x - minA
if v1 > best1:
best1 = v1
if x < minA:
minA = x
sys.stdout.write(str(ans) + "\n")

View File

@@ -0,0 +1,30 @@
# [Gold IV] A[j]-A[i]+A[l]-A[k] - 15487
[문제 링크](https://www.acmicpc.net/problem/15487)
### 성능 요약
메모리: 39140 KB, 시간: 1192 ms
### 분류
다이나믹 프로그래밍
### 제출 일자
2026년 2월 10일 18:58:20
### 문제 설명
<p>크기가 N인 배열 A가 주어졌을 때, i < j < k < l을 만족하는 (i, j, k, l) 중에서 A[j]-A[i]+A[l]-A[k]의 최댓값을 구하는 프로그램을 작성하시오. </p>
### 입력
<p>첫째 줄에 배열 A의 크기 N(4 ≤ N ≤ 1,000,000)이 주어진다.</p>
<p>둘째 줄에는 배열 A에 들어있는 수가 순서대로 주어진다. 배열에 들어있는 수는 1,000,000보다 작거나 같은 자연수이다.</p>
### 출력
<p>첫째 줄에 i < j < k < l을 만족하는 (i, j, k, l) 중에서 A[j]-A[i]+A[l]-A[k]의 최댓값을 출력한다.</p>