-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCyclist.java
More file actions
57 lines (55 loc) · 2 KB
/
Cyclist.java
File metadata and controls
57 lines (55 loc) · 2 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.ArrayList;
import java.util.Scanner;
public class Cyclist {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
if (scanner.hasNextInt()) {
int t = scanner.nextInt();
while (t-- > 0) {
int n = scanner.nextInt();
int k = scanner.nextInt();
int p = scanner.nextInt();
int m = scanner.nextInt();
ArrayList<Integer> deck = new ArrayList<>(n);
for (int i = 0; i < n; i++) {
deck.add(scanner.nextInt());
}
int targetIndex = p - 1;
int plays = 0;
while (true) {
if (targetIndex < k) {
int cost = deck.get(targetIndex);
if (m >= cost) {
m -= cost;
plays++;
deck.remove(targetIndex);
deck.add(cost);
targetIndex = n - 1;
} else {
break;
}
}
else {
int minCost = Integer.MAX_VALUE;
int minIdx = -1;
for (int i = 0; i < k; i++) {
if (deck.get(i) < minCost) {
minCost = deck.get(i);
minIdx = i;
}
}
if (m >= minCost) {
m -= minCost;
deck.remove(minIdx);
deck.add(minCost);
targetIndex--;
} else {
break;
}
}
}
System.out.println(plays);
}
}
}
}