-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeque.rb
More file actions
309 lines (262 loc) · 5.32 KB
/
Copy pathdeque.rb
File metadata and controls
309 lines (262 loc) · 5.32 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
class Deque
include Enumerable
def self.[](*args)
new(args)
end
def initialize(ary = [], max_size: 0)
@n = [max_size, ary.size].max + 1
@buf = ary + [nil] * (@n - ary.size)
@head = 0
@tail = ary.size
end
def empty?
size == 0
end
def size
(@tail - @head) % @n
end
alias length size
def <<(x)
__extend if __full?
@buf[@tail] = x
@tail += 1
@tail %= @n
self
end
def push(*args)
args.each{ |x| self << x }
self
end
alias append push
def unshift(x)
__extend if __full?
@head -= 1
@head %= @n
@buf[@head] = x
self
end
alias prepend unshift
def pop
return nil if size == 0
@tail -= 1
@tail %= @n
@buf[@tail]
end
def shift
return nil if size == 0
ret = @buf[@head]
@head += 1
@head %= @n
ret
end
def last
@buf[@tail - 1] # self[-1]
end
def [](a, b = nil)
if b
slice2(a, b)
elsif a.is_a?(Range)
s = a.begin
t = a.end
t -= 1 if a.exclude_end?
slice2(s, t - s + 1)
else
slice1(a)
end
end
alias slice []
def at(idx)
slice1(idx)
end
private def slice1(idx)
sz = size
return nil if idx < -sz || sz <= idx
@buf[__index(idx)]
end
private def slice2(i, cnt)
sz = size
i += sz if i < 0
return nil if cnt < 0 || i > sz
if i == sz
Deque[]
else
j = [i + cnt - 1, sz].min
slice_indexes(i, j)
end
end
private def slice_indexes(i, j)
s = __index(i)
t = __index(j) + 1
Deque.new(__to_a(s, t))
end
def []=(idx, value)
@buf[__index(idx)] = value
end
def ==(other)
return false unless size == other.size
to_a == other.to_a
end
def hash
to_a.hash
end
def reverse
dup.reverse!
end
def reverse!
if @head <= @tail
@buf[@head...@tail] = @buf[@head...@tail].reverse
else
sz = size
@buf[0...sz] = @buf[@head..-1].concat(@buf[0...@tail]).reverse
@head = 0
@tail = sz
end
self
end
def dig(*args)
case args.size
when 0
raise ArgumentError, "wrong number of arguments (given 0, expected 1+)"
when 1
self[args[0].to_int]
else
i = args.shift.to_int
self[i]&.dig(*args)
end
end
def each
return enum_for(:each) unless block_given?
if @head <= @tail
@buf[@head...@tail].each{ |e| yield e }
else
@buf[@head..-1].each{ |e| yield e }
@buf[0...@tail].each{ |e| yield e }
end
end
def clear
sz = size
@head = 0
@tail = 0
self
end
def join(sep = $,)
to_a.join(sep)
end
# def rotate(cnt = 1)
# sz = size
# h, t = @head, @tail
# # p [sz, h, t]
# hh = __index(0)
# tt = __index((sz - cnt) % sz) + 1
# # if (@head <= @tail) == (h <= t)
# # @head, @tail = @tail, @head
# # end
# p [sz, h, t, hh, tt]
# @head = 1
# @tail = 1
# self
# end
def replace(other)
ary = other.to_a
@n = ary.size + 1
@buf = ary + [nil] * (@n - ary.size)
@head = 0
@tail = ary.size
self
end
private def __build(other)
ary = other.to_a
@n = ary.size + 1
@buf = ary + [nil] * (@n - ary.size)
@head = 0
@tail = ary.size
self
end
def self.to_a_to_deque(name, args = 0)
args = (0...args).map{ |i| "a#{i}" }.join(", ")
code = "def #{name}(#{args})
Deque.new(to_a.#{name}(#{args}))
end"
class_eval(code)
end
to_a_to_deque(:compact)
def rotate(cnt = 0)
Deque.new(to_a.rotate(cnt))
end
def rotate!(cnt = 0)
replace(to_a.rotate!(cnt))
end
def map!(&blk)
replace(to_a.map!(&blk))
end
def sample(n = 1)
Deque.new(to_a.shuffle)
end
def shuffle(n = nil)
Deque.new(to_a.shuffle(n))
end
def shuffle!(n = nil)
replace(to_a.shuffle!(n))
end
def swap(i, j)
i = __index(i)
j = __index(j)
@buf[i], @buf[j] = @buf[j], @buf[i]
self
end
def to_a
__to_a
end
# alias to_ary to_a
private def __to_a(s = @head, t = @tail)
s <= t ? @buf[s...t] : @buf[s..-1].concat(@buf[0...t])
end
def to_s
"#{self.class}#{to_a}"
end
# alias inspect to_s
def inspect
"Deque#{to_a}(@n=#{@n}, @buf=#{@buf}, @head=#{@head}, @tail=#{@tail}, size #{size}#{" full" if __full?})"
end
private def __full?
raise if size > @n - 1
size == @n - 1
end
def __index(i)
l = size
raise IndexError, "index out of range: #{i}" unless -l <= i && i < l
i += l if i < 0
(@head + i) % @n
end
private def __extend
if @tail + 1 == @head
tail = @buf.shift(@tail + 1)
@buf.concat(tail).concat([nil] * @n)
@head = 0
@tail = @n - 1
@n = @buf.size
else
@buf[(@tail + 1)..(@tail + 1)] = [nil] * @n
@n = @buf.size
@head += @n if @head > 0
end
end
end
def typical90_044
n, q = gets.to_s.split.map{ |e| e.to_i }
a = gets.to_s.split.map{ |e| e.to_i }
a = Deque.new(a)
puts a
q.times do |i|
t, x, y = gets.to_s.split.map{ |e| e.to_i - 1 }
case t
when 0
a[x], a[y] = a[y], a[x]
when 1
a.unshift(a.pop)
when 2
puts a[x]
end
end
end
# [Pythonで各要素にO\(1\)でランダムアクセスできるdeque\(両端キュー\)を書いてみた \- 30歳で競プロに目覚めた霊長類のブログ](https://prd-xxx.hateblo.jp/entry/2020/02/07/114818)