-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL1Q1.java
101 lines (91 loc) · 1.97 KB
/
L1Q1.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
class Disco{
int diam;
Disco next;
Disco(int diam){
this.diam = diam;
this.next = null;
}
}
class Stack{
Disco top;
Stack(){
this.top = null;
}
public void push(int diam){
Disco novo = new Disco(diam);
novo.next = top;
top = novo;
}
public int pop(){
if (top==null) return -1;
int r=top.diam;
top = top.next;
return r;
}
public int top(){
if (top!=null) return top.diam;
return -1;
}
}
public class L1Q1 {
public static void main(String[] args) {
Arquivo arq = new Arquivo("L1Q1.in", "L1Q1.out");
int n;
int nmaior;
while(!arq.isEndOfFile()){
Stack a = new Stack();
int cases = arq.readInt();
for (int i=0; i<cases; i++){
n = arq.readInt();
nmaior = n;
for (int j=0; j<n; j++){
a.push(arq.readInt());
}
int[]numPassado=new int[210];
int[]quemPraquem=new int[210];
int indice=0;
Stack b = new Stack();
Stack c = new Stack();
while (a.top()!=-1){
numPassado[indice]=a.top();
quemPraquem[indice]=0;
indice++;
b.push(a.pop());
while (b.top()==nmaior) {
numPassado[indice]=b.top();
quemPraquem[indice]=1;
indice++;
c.push(b.pop());
nmaior--;
}
}
boolean impossivel = false;
while (b.top()!=-1 && impossivel==false){
if (b.top()>c.top()){
impossivel = true;
arq.println("impossivel");
} else {
c.push(b.pop());
}
}
//------------print-------------//
if (!impossivel){
for (int j=0; numPassado[j]!=0; j++){
//StringBuffer sb = new StringBuffer();
if (quemPraquem[j]==1){
//sb.append("tirar ").append(numPassado[j]).append(" de B e colocar em C");
arq.println("tirar " + numPassado[j]
+ " de B e colocar em C");
} else {
//sb.append("tirar ").append(numPassado[j]).append(" de A e colocar em B");
arq.println("tirar " + numPassado[j]
+ " de A e colocar em B");
}
}
}
arq.println();
}
}
arq.close();
}
}