- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14002.java
More file actions
Latest commit
58 lines (43 loc) · 1.32 KB
/
Copy path14002.java
File metadata and controls
58 lines (43 loc) · 1.32 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
58
importjava.util.*;
publicclassMain {
publicstaticvoidmain(String[] args) {
Scannersc = newScanner(System.in);
intn = sc.nextInt();
int[] arr = newint[n];
for(inti=0; i<n; i++)
arr[i] = sc.nextInt();
ArrayList<Integer>[] max = newArrayList[n];
max[0] = newArrayList<>();
max[0].add(arr[0]);
for(inti=1; i<n; i++) {
inthighest = 1;
ArrayList<Integer> temp = null;
for(intj=i-1; j>=0; j--) {
if(arr[j] < arr[i] && highest < 1+max[j].size()) {
temp = max[j];
highest = 1+max[j].size();
}
}
if(temp == null) {
max[i] = newArrayList<>();
max[i].add(arr[i]);
}
else {
max[i] = (ArrayList)temp.clone();
max[i].add(arr[i]);
}
}
intsize = 0;
intidx = 0;
for(inti=0; i<n; i++) {
if(max[i].size() > size) {
size = max[i].size();
idx = i;
}
}
System.out.println(max[idx].size());
for(inti=0; i<max[idx].size(); i++) {
System.out.print(Integer.toString(max[idx].get(i)) + " ");
}
}
}