File tree 1 file changed +63
-0
lines changed
1 file changed +63
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Created by Jim on 2/8/2015.
3
+ */
4
+
5
+ import java .util .Scanner ;
6
+ import java .util .Stack ;
7
+
8
+ public class ccc14s3 {
9
+
10
+ public static void main (String [] args ){
11
+ Scanner keyboard = new Scanner (System .in );
12
+
13
+ int caseCount = keyboard .nextInt ();
14
+
15
+ while (caseCount -- != 0 ){
16
+ Stack <Integer > mountainTop = new Stack <Integer >();
17
+ Stack <Integer > branch = new Stack <Integer >();
18
+
19
+ int cartCount = keyboard .nextInt ();
20
+
21
+ for (int i =0 ; i <cartCount ; i ++)
22
+ mountainTop .add (keyboard .nextInt ());
23
+
24
+ int currentNumber = 1 ;
25
+
26
+ boolean noSolution = true ;
27
+
28
+ while (true ){
29
+ if (mountainTop .isEmpty () && branch .isEmpty ()) {
30
+ noSolution = false ;
31
+ break ;
32
+ }
33
+
34
+ /*
35
+ Check if mountainTop has the current number
36
+ */
37
+
38
+ if (!mountainTop .isEmpty () && mountainTop .peek () == currentNumber ){
39
+ mountainTop .pop ();
40
+ currentNumber += 1 ;
41
+ continue ;
42
+ } else if (!branch .isEmpty () && branch .peek () == currentNumber ){
43
+ branch .pop ();
44
+ currentNumber += 1 ;
45
+ continue ;
46
+ } else {
47
+ if (mountainTop .isEmpty ()){
48
+ break ;
49
+ }
50
+
51
+ branch .push (mountainTop .pop ());
52
+ }
53
+
54
+ }
55
+
56
+ if (noSolution )
57
+ System .out .println ("N" );
58
+ else
59
+ System .out .println ("Y" );
60
+ }
61
+ }
62
+
63
+ }
You can’t perform that action at this time.
0 commit comments