Skip to content

Commit 8047bd2

Browse files
committed
[Silver V] Title: 왕복, Time: 260 ms, Memory: 26648 KB -BaekjoonHub
1 parent 87558c5 commit 8047bd2

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# [Silver V] 왕복 - 18311
2+
3+
[문제 링크](https://www.acmicpc.net/problem/18311)
4+
5+
### 성능 요약
6+
7+
메모리: 26648 KB, 시간: 260 ms
8+
9+
### 분류
10+
11+
구현
12+
13+
### 제출 일자
14+
15+
2025년 1월 28일 17:19:14
16+
17+
### 문제 설명
18+
19+
<p>왕복 달리기 선수는 <em>N</em>개의 이어진 일직선상의 코스들을 모두 지나 끝까지 도달한 뒤에, 다시 출발 지점으로 돌아와야 한다. 전체 코스들을 지나고 있는 상황에서 이동 거리가 <em>K</em>일 때, 현재 지나고 있는 코스의 번호를 출력하는 프로그램을 작성하시오. 단, 이동 거리가 <em>K</em>가 두 코스 사이에 위치한 경우에는 ‘지나야 할’ 코스의 번호를 출력한다.</p>
20+
21+
<p>예를 들어 <em>N</em>=5일 때, 각 코스의 길이가 차례대로 7, 4, 2, 4, 5라고 가정하자. 출발 지점을 0이라고 하면, 전체 코스가 구성된 형태를 다음과 같이 그릴 수 있다.</p>
22+
23+
<p style="text-align: center;"><img alt="" src="https://upload.acmicpc.net/31205870-182b-4c54-ad41-2c4892be90a9/-/preview/" style="height: 115px; width: 600px;"></p>
24+
25+
<ol>
26+
<li style="text-align: justify;"><em>K</em>=0일 때, 1번 코스를 지나고 있으므로 1을 출력한다.</li>
27+
<li style="text-align: justify;"><em>K</em>=7일 때, 2번 코스를 지나고 있으므로 2를 출력한다.</li>
28+
<li style="text-align: justify;"><em>K</em>=9일 때, 2번 코스를 지나고 있으므로 2를 출력한다.</li>
29+
<li style="text-align: justify;"><em>K</em>=12일 때, 3번 코스를 지나고 있으므로 3을 출력한다.</li>
30+
<li style="text-align: justify;"><em>K</em>=28일 때, 이는 끝까지 도달한 뒤에 시작 위치로 돌아오는 과정으로 볼 수 있다. 4번 코스를 지나고 있으므로 4를 출력한다.</li>
31+
</ol>
32+
33+
<p>또한 <em>K</em>는 항상 왕복 거리보다 작은 양의 정수 혹은 0으로 주어진다. 예를 들어 위와 같이 전체 코스들의 길이 합을 22라고 하면, 0≤<em>K</em>≤43이다.</p>
34+
35+
### 입력
36+
37+
<p>첫째 줄에 정수 <em>N</em>, <em>K</em>가 공백을 기준으로 구분되어 주어진다. (1≤<em>N</em>≤100,000) 단, <em>K</em>는 항상 왕복 거리보다 작은 양의 정수 혹은 0으로 주어진다. 둘째 줄에 1번부터 <em>N</em>번까지 각 코스의 길이가 공백을 기준으로 구분되어 차례대로 주어진다. 각 코스의 길이는 50,000보다 작거나 같은 자연수다.</p>
38+
39+
### 출력
40+
41+
<p>첫째 줄에 이동 거리가 <em>K</em>일 때, 지나고 있는 코스의 번호를 출력한다.</p>
42+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.io.*;
2+
3+
public class Main {
4+
public static void main(String[] args) throws IOException {
5+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
6+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
7+
8+
String[] split = br.readLine().split(" ");
9+
int N = Integer.parseInt(split[0]);
10+
long K = Long.parseLong(split[1]);
11+
12+
String[] line = br.readLine().split(" ");
13+
int[] arr = new int[N];
14+
long distance = 0L;
15+
for(int i=0; i<N; i++){
16+
int parsed = Integer.parseInt(line[i]);
17+
distance+=parsed;
18+
arr[i] = parsed;
19+
}
20+
21+
int result = 0;
22+
23+
if(K <= distance){//끝까지 안간경우
24+
for(int i=0; i<N; i++){
25+
if(K<0){
26+
break;
27+
}
28+
K-=arr[i];
29+
result=i+1;
30+
}
31+
32+
}else{ //끝까지 간 경우
33+
long back = K-distance; //다시 돌아가야 할 거리
34+
for(int i=N-1; i>=0; i--){
35+
if(back<0){
36+
break;
37+
}
38+
back-=arr[i];
39+
result=i+1;
40+
}
41+
}
42+
43+
System.out.println(result);
44+
}
45+
}

0 commit comments

Comments
 (0)