-
Notifications
You must be signed in to change notification settings - Fork 0
/
1756(Greedy).java
63 lines (52 loc) · 1.57 KB
/
1756(Greedy).java
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
58
59
60
61
62
63
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st = new StringTokenizer(br.readLine());
int D = Integer.parseInt(st.nextToken());
int N = Integer.parseInt(st.nextToken());
int oven[] = new int[D];
st = new StringTokenizer(br.readLine());
for(int i = 0; i < D; i++){
oven[i] = Integer.parseInt(st.nextToken());
}
int pizza[] = new int[N];
st = new StringTokenizer(br.readLine());
for(int i = 0; i < N; i++){
pizza[i] = Integer.parseInt(st.nextToken());
}
for(int i = 0; i < D - 1; i++){
if(oven[i] < oven[i + 1]){
oven[i + 1] = oven[i];
}
}
boolean fail = false;
int end = D;
for(int i = 0; i < N; i++){
int current = pizza[i];
if(end == 0){
fail = true;
break;
}
for(int j = end - 1; j >= 0; j--){
if(oven[j] >= current){
end = j;
break;
}
if(j == 0){
fail = true;
}
}
if(fail){
break;
}
}
if(fail){
System.out.println(0);
}else{
System.out.println(end + 1);
}
}
}