티스토리 뷰
도착시간 기준으로 방문했을때의 최소값을 기록하여 확인 가능하다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Collections; | |
import java.util.LinkedList; | |
import java.util.Scanner; | |
/** | |
* 정올, 1491 | |
* 자동차경주대회 | |
* | |
* @author whitebeard-k | |
* | |
*/ | |
public class Problem1491 { | |
static int CAR_DIST; | |
static int N; | |
static int[] dist; | |
static int[] time; | |
static int[] dpTime; | |
static int[] dpStation; | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
CAR_DIST = sc.nextInt(); | |
N = sc.nextInt(); | |
int distSum = 0; | |
dist = new int[N + 2]; | |
for (int i = 1; i <= N + 1; i++) { | |
dist[i] = sc.nextInt(); | |
distSum += dist[i]; | |
} | |
time = new int[N + 2]; | |
for (int i = 1; i <= N; i++) | |
time[i] = sc.nextInt(); | |
dpTime = new int[N + 2]; | |
dpStation = new int[N + 2]; | |
sc.close(); | |
if (CAR_DIST >= distSum) { | |
System.out.println(0); | |
return; | |
} | |
for (int i = 1; i < dist.length; i++) { | |
int currentDist = CAR_DIST - dist[i]; | |
int currentTime = time[i]; | |
int min = Integer.MAX_VALUE; | |
for (int j = i - 1; j >= 0; j--) { | |
if (currentDist < 0) | |
break; | |
if (currentDist >= 0 && min >= dpTime[j] + currentTime) { | |
min = dpTime[j] + currentTime; | |
dpStation[i] = j; | |
} | |
currentDist -= dist[j]; | |
} | |
dpTime[i] = min; | |
} | |
// System.out.println("dist -------------"); | |
// | |
// for (int n : dist) | |
// System.out.printf("%3d ", n); | |
// | |
// System.out.println("\ntime -------------"); | |
// | |
// for (int n : time) | |
// System.out.printf("%3d ", n); | |
// | |
// System.out.println("\ndpTime -------------"); | |
// | |
// for (int n : dpTime) | |
// System.out.printf("%3d ", n); | |
// | |
// System.out.println("\ndpStation -------------"); | |
// | |
// for (int n : dpStation) | |
// System.out.printf("%3d ", n); | |
System.out.println(dpTime[N + 1]); | |
LinkedList<Integer> list = new LinkedList<>(); | |
int index = N + 1; | |
while (true) { | |
int station = dpStation[index]; | |
if (station == 0) | |
break; | |
list.add(station); | |
index = station; | |
} | |
System.out.println(list.size()); | |
Collections.reverse(list); | |
for (int n : list) | |
System.out.printf("%d ", n); | |
} | |
} |
반응형
'알고리즘 > 정올' 카테고리의 다른 글
[정올] 2616 앱 (0) | 2018.09.09 |
---|---|
[정올][다이나믹] 1019 소형기관차 (0) | 2017.07.07 |
[정올][다이나믹] 2293, 동전 1 (0) | 2017.06.19 |
[정올][bfs] 2613 토마토(고) (0) | 2017.05.12 |
[정올][다이나믹] 1411 두 줄로 타일 깔기 (0) | 2017.04.25 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 하이브
- S3
- nodejs
- build
- 하둡
- Tez
- yarn
- 백준
- mysql
- Python
- AWS
- java
- 정올
- hbase
- 오류
- Linux
- airflow
- bash
- 파이썬
- Hadoop
- HDFS
- 다이나믹
- emr
- SQL
- error
- HIVE
- SPARK
- 알고리즘
- ubuntu
- oozie
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함