-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCODERE3.java
155 lines (115 loc) · 3.73 KB
/
CODERE3.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
/**
* Created by poplig on 2/28/15.
*/
public class Main {
private static int maxElement = 1001;
private int n;
private int elem[] = new int[maxElement];
public static void main(String [] args) {
// Scanner scanner = new Scanner(System.in);
InputReader inputReader = new InputReader(System.in);
// int t = scanner.nextInt();
int t = inputReader.nextInt();
int [] elements = new int[maxElement];
while(t-->0) {
int n = inputReader.nextInt();
// int n = scanner.nextInt();
for(int i = 0; i < n; ++i) {
// elements[i] = scanner.nextInt();
elements[i] = inputReader.nextInt();
}
int ans = new Main(elements, n).getMaxSubsequence();
System.out.println(ans);
}
}
Main(int [] elem, int n) {
this.elem = elem;
this.n = n;
}
public int getMaxSubsequence() {
Position [] positions = new Position[n];
int maxFound = 0;
for(int i = 0; i < n; ++i) {
positions[i] = new Position(1, 1);
for(int j = 0;j < i; ++j) {
if( elem[j] > elem[i] ) {
// Case where there is decrement.
// take the counter from the position and then take action.
int decCounter = positions[j].getDownCounter();
positions[i].setDownCounter(Math.max( decCounter+1, positions[i].getDownCounter() ));
} else if(elem[j] < elem[i]) {
int upCounter = positions[j].getUpCounter();
positions[i].setUpCounter( Math.max(positions[i].getUpCounter(), upCounter + 1) );
positions[i].setDownCounter( Math.max(positions[i].getDownCounter(),
Math.max(positions[i].getUpCounter(), upCounter + 1) ));
}
}
if(positions[i].getDownCounter() > maxFound) {
maxFound = positions[i].getDownCounter();
}
}
return maxFound;
}
}
class Position {
Position(int a, int b) {
this.upCounter = a;
this.downCounter = b;
}
int upCounter;
int downCounter;
public int getUpCounter() {
return upCounter;
}
public int getDownCounter() {
return downCounter;
}
public void setUpCounter(int upCounter) {
this.upCounter = upCounter;
}
public void setDownCounter(int downCounter) {
this.downCounter = downCounter;
}
}
class InputReader {
private BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
String line = "";
try {
line = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return line;
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}