-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
181 lines (139 loc) · 2.81 KB
/
Main.java
File metadata and controls
181 lines (139 loc) · 2.81 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q, p, k;
String instruction;
int cont = -1;
q = in.nextInt();
p = in.nextInt();
int aux;
int i_cont = 0;
Memory m = new Memory();
Buffer b = new Buffer(q);
//for(int j = 0; j < 9; ++j){
while(in.hasNext()){
instruction = in.next();
k = in.nextInt();
i_cont++;
switch(instruction){
case "RECV":
for(int i = 0; i < k; ++i){
cont++;
if(!b.isFull()){
b.add(cont);
}else{
m.push(cont);
}
}
break;
case "SEND":
if(!b.isEmpty()){
for(int i = 0; i < k; ++i){
if(!b.isEmpty()){
aux = b.remove();
System.out.print(aux + " ");
}
}
}
System.out.println();
break;
default:
break;
}
if(i_cont == p){
i_cont = 0;
while(!b.isFull() && !m.isEmpty()){
b.add(m.pop());
}
}
}
while(!b.isEmpty()){
aux = b.remove();
System.out.print(aux + " ");
}
System.out.println();
while(!m.isEmpty()){
aux = m.pop();
System.out.print(aux + " ");
}
in.close();
}
}
class Memory{
int elements[];
int topo;
public Memory(){
elements = new int[1500];
topo = -1; //posicao invalida do vetor
}
public void push(int _value){
if(!isFull()){
topo ++;
elements[topo] = _value;
}else{
throw new RuntimeException("Stack Overflow");
}
}
public int pop(){
if(!isEmpty()){
int e;
e = elements[topo];
topo--;
return e;
}else{
throw new RuntimeException("It's alredy empty");
}
}
public boolean isEmpty(){
return(topo == -1);
}
public boolean isFull(){
return (topo== elements.length);
}
public int top(){
if(!isEmpty()){
return elements[topo];
}else{
throw new RuntimeException("It's empty");
}
}
}
class Buffer{
private int[] values;
private int first;
private int last;
private int total;
public Buffer(int _size){
values = new int[_size];
first = 0;
last = 0;
total = 0;
}
public void add(int _value){
if(!isFull()){
values[last] = _value;
last = (last+1) % values.length;
total++;
}else{
throw new RuntimeException("It's already full");
}
}
public int remove(){
if(!isEmpty()){
int e = values[first];
first = (first + 1)% values.length;
total--;
return e;
}else{
throw new RuntimeException("It's already empty");
}
}
public boolean isEmpty(){
return (total == 0);
}
public boolean isFull(){
return (total == values.length);
}
}