4444В этих случаях состояние (head, tail, size) не меняется, что сохраняет инварианты.
4545
4646Сложность
47- Каждая операция дека выполняет константное число действий: O(1) по времени.
47+ Время: O(n), где n - количество операций. Каждая операция дека выполняется за O(1)
4848Память: массив из m элементов и несколько целых переменных: O(m) по памяти.
4949*/
5050
51- public class Deque {
52-
53- // -------------------- FAST INPUT --------------------
54- static final class FastIn {
55- private final InputStream in ;
56- private final byte [] buf = new byte [1 << 16 ];
57- private int ptr = 0 , len = 0 ;
58-
59- FastIn (InputStream in ) {
60- this .in = in ;
61- }
62-
63- private int read () throws IOException {
64- if (ptr >= len ) {
65- len = in .read (buf );
66- ptr = 0 ;
67- if (len <= 0 ) {
68- return -1 ;
69- }
70- }
71- return buf [ptr ++];
72- }
73-
74- int nextInt () throws IOException {
75- int c ;
76- do {
77- c = read ();
78- if (c == -1 ) {
79- throw new EOFException ("Unexpected EOF" );
80- }
81- } while (c <= ' ' );
8251
83- int sign = 1 ;
84- if (c == '-' ) {
85- sign = -1 ;
86- c = read ();
87- }
88-
89- int val = 0 ;
90- while (c > ' ' ) {
91- val = val * 10 + (c - '0' );
92- c = read ();
93- }
94- return val * sign ;
95- }
96-
97- String next () throws IOException {
98- int c ;
99- do {
100- c = read ();
101- if (c == -1 ) {
102- throw new EOFException ("Unexpected EOF" );
103- }
104- } while (c <= ' ' );
105-
106- byte [] tmp = new byte [32 ];
107- int n = 0 ;
108- while (c > ' ' ) {
109- if (n == tmp .length ) {
110- byte [] t2 = new byte [tmp .length * 2 ];
111- System .arraycopy (tmp , 0 , t2 , 0 , tmp .length );
112- tmp = t2 ;
113- }
114- tmp [n ++] = (byte ) c ;
115- c = read ();
116- if (c == -1 ) {
117- break ;
118- }
119- }
120- return new String (tmp , 0 , n );
121- }
122- }
123-
124- // -------------------- FAST OUTPUT --------------------
125- static final class FastOut {
126- private final OutputStream out ;
127- private final byte [] buf = new byte [1 << 16 ];
128- private int p = 0 ;
129- private final byte [] tmp = new byte [12 ];
130-
131- FastOut (OutputStream out ) {
132- this .out = out ;
133- }
134-
135- void writeByte (int b ) throws IOException {
136- if (p == buf .length ) {
137- flush ();
138- }
139- buf [p ++] = (byte ) b ;
140- }
141-
142- void writeStr (String s ) throws IOException {
143- for (int i = 0 ; i < s .length (); i ++) {
144- writeByte (s .charAt (i ));
145- }
146- }
147-
148- void writeInt (int x ) throws IOException {
149- if (x == 0 ) {
150- writeByte ('0' );
151- return ;
152- }
153- if (x < 0 ) {
154- writeByte ('-' );
155- x = -x ;
156- }
157-
158- int k = 0 ;
159- while (x > 0 ) {
160- tmp [k ++] = (byte ) ('0' + (x % 10 ));
161- x /= 10 ;
162- }
163- for (int i = k - 1 ; i >= 0 ; i --) {
164- writeByte (tmp [i ]);
165- }
166- }
167-
168- void flush () throws IOException {
169- out .write (buf , 0 , p );
170- p = 0 ;
171- }
172- }
52+ public class Deque {
17353
17454 // -------------------- RING BUFFER DEQUE --------------------
17555 static final class RingDeque {
@@ -185,19 +65,11 @@ static final class RingDeque {
18565 }
18666
18767 private int next (int i ) {
188- i ++;
189- if (i == cap ) {
190- i = 0 ;
191- }
192- return i ;
68+ return (i +1 ) % cap ;
19369 }
19470
19571 private int prev (int i ) {
196- i --;
197- if (i == -1 ) {
198- i = cap - 1 ;
199- }
200- return i ;
72+ return (i -1 +cap ) % cap ;
20173 }
20274
20375 boolean isEmpty () {
@@ -244,33 +116,38 @@ private static void process(FastIn in, FastOut out) throws Exception {
244116 for (int i = 0 ; i < n ; i ++) {
245117 String cmd = in .next ();
246118
247- if ("push_back" .equals (cmd )) {
248- int x = in .nextInt ();
249- if (dq .isFull ()) {
250- out .writeStr ("error\n " );
251- } else {
252- dq .pushBack (x );
119+ switch (cmd ) {
120+ case "push_back" -> {
121+ int x = in .nextInt ();
122+ if (dq .isFull ()) {
123+ out .writeStr ("error\n " );
124+ } else {
125+ dq .pushBack (x );
126+ }
253127 }
254- } else if ("push_front" .equals (cmd )) {
255- int x = in .nextInt ();
256- if (dq .isFull ()) {
257- out .writeStr ("error\n " );
258- } else {
259- dq .pushFront (x );
128+ case "push_front" -> {
129+ int x = in .nextInt ();
130+ if (dq .isFull ()) {
131+ out .writeStr ("error\n " );
132+ } else {
133+ dq .pushFront (x );
134+ }
260135 }
261- } else if ("pop_front" .equals (cmd )) {
262- if (dq .isEmpty ()) {
263- out .writeStr ("error\n " );
264- } else {
265- out .writeInt (dq .popFront ());
266- out .writeByte ('\n' );
136+ case "pop_front" -> {
137+ if (dq .isEmpty ()) {
138+ out .writeStr ("error\n " );
139+ } else {
140+ out .writeInt (dq .popFront ());
141+ out .writeByte ('\n' );
142+ }
267143 }
268- } else { // pop_back
269- if (dq .isEmpty ()) {
270- out .writeStr ("error\n " );
271- } else {
272- out .writeInt (dq .popBack ());
273- out .writeByte ('\n' );
144+ case "pop_back" -> {
145+ if (dq .isEmpty ()) {
146+ out .writeStr ("error\n " );
147+ } else {
148+ out .writeInt (dq .popBack ());
149+ out .writeByte ('\n' );
150+ }
274151 }
275152 }
276153 }
@@ -385,4 +262,126 @@ public static void main(String[] args) throws Exception {
385262 run ();
386263 }
387264 }
265+
266+ // -------------------- FAST INPUT --------------------
267+ static final class FastIn {
268+ private final InputStream in ;
269+ private final byte [] buf = new byte [1 << 16 ];
270+ private int ptr = 0 , len = 0 ;
271+
272+ FastIn (InputStream in ) {
273+ this .in = in ;
274+ }
275+
276+ private int read () throws IOException {
277+ if (ptr >= len ) {
278+ len = in .read (buf );
279+ ptr = 0 ;
280+ if (len <= 0 ) {
281+ return -1 ;
282+ }
283+ }
284+ return buf [ptr ++];
285+ }
286+
287+ int nextInt () throws IOException {
288+ int c ;
289+ do {
290+ c = read ();
291+ if (c == -1 ) {
292+ throw new EOFException ("Unexpected EOF" );
293+ }
294+ } while (c <= ' ' );
295+
296+ int sign = 1 ;
297+ if (c == '-' ) {
298+ sign = -1 ;
299+ c = read ();
300+ }
301+
302+ int val = 0 ;
303+ while (c > ' ' ) {
304+ val = val * 10 + (c - '0' );
305+ c = read ();
306+ }
307+ return val * sign ;
308+ }
309+
310+ String next () throws IOException {
311+ int c ;
312+ do {
313+ c = read ();
314+ if (c == -1 ) {
315+ throw new EOFException ("Unexpected EOF" );
316+ }
317+ } while (c <= ' ' );
318+
319+ byte [] tmp = new byte [32 ];
320+ int n = 0 ;
321+ while (c > ' ' ) {
322+ if (n == tmp .length ) {
323+ byte [] t2 = new byte [tmp .length * 2 ];
324+ System .arraycopy (tmp , 0 , t2 , 0 , tmp .length );
325+ tmp = t2 ;
326+ }
327+ tmp [n ++] = (byte ) c ;
328+ c = read ();
329+ if (c == -1 ) {
330+ break ;
331+ }
332+ }
333+ return new String (tmp , 0 , n );
334+ }
335+ }
336+
337+ // -------------------- FAST OUTPUT --------------------
338+ static final class FastOut {
339+ private final OutputStream out ;
340+ private final byte [] buf = new byte [1 << 16 ];
341+ private int p = 0 ;
342+ private final byte [] tmp = new byte [12 ];
343+
344+ FastOut (OutputStream out ) {
345+ this .out = out ;
346+ }
347+
348+ void writeByte (int b ) throws IOException {
349+ if (p == buf .length ) {
350+ flush ();
351+ }
352+ buf [p ++] = (byte ) b ;
353+ }
354+
355+ void writeStr (String s ) throws IOException {
356+ for (int i = 0 ; i < s .length (); i ++) {
357+ writeByte (s .charAt (i ));
358+ }
359+ }
360+
361+ void writeInt (int x ) throws IOException {
362+ if (x == 0 ) {
363+ writeByte ('0' );
364+ return ;
365+ }
366+ if (x < 0 ) {
367+ writeByte ('-' );
368+ x = -x ;
369+ }
370+
371+ int k = 0 ;
372+ while (x > 0 ) {
373+ tmp [k ++] = (byte ) ('0' + (x % 10 ));
374+ x /= 10 ;
375+ }
376+ for (int i = k - 1 ; i >= 0 ; i --) {
377+ writeByte (tmp [i ]);
378+ }
379+ }
380+
381+ void flush () throws IOException {
382+ out .write (buf , 0 , p );
383+ p = 0 ;
384+ }
385+ }
386+
388387}
0 commit comments