-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha.py
More file actions
326 lines (239 loc) · 8.19 KB
/
a.py
File metadata and controls
326 lines (239 loc) · 8.19 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/home/ernesto/programas/instalados/python/python3.8/bin/python3.8
'''
Created on Feb 27, 2020
@author: ernesto
'''
# XXX: https://www.hackerrank.com/challenges/maximum-subarray-sum/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=search
import math
import os
import random
import re
import sys
import logging
from functools import partial, reduce
from collections import defaultdict
from bisect import bisect_right
from builtins import set
class TreeNode(object):
def __init__(self, key, value):
self.key = key
self.value = value
self.left = None
self.right = None
self.height = 1
class AVL_Tree(object):
def __init__(self):
self.root = None
def insert(self, key, value):
self.root = self.insertNode(self.root, key, value)
def insertNode(self, root, key, value):
child = TreeNode(key, value)
if not root:
return child
cur = root
v = []
while cur:
v.append(cur)
if key < cur.key:
cur = cur.left
else:
if key > cur.key:
cur = cur.right
else:
# Actualizar valor si llave ya esta
cur.value = value
return root
while v:
cur = v.pop()
if key < cur.key:
cur.left = child
else:
cur.right = child
child = self.rebalancea(cur, key)
return child
def rebalancea(self, root, key):
root.height = 1 + max(self.getHeight(root.left),
self.getHeight(root.right))
balanceFactor = self.getBalance(root)
if balanceFactor > 3:
if key < root.left.key:
return self.rightRotate(root)
else:
root.left = self.leftRotate(root.left)
return self.rightRotate(root)
if balanceFactor < -3:
if key > root.right.key:
return self.leftRotate(root)
else:
root.right = self.rightRotate(root.right)
return self.leftRotate(root)
return root
def deleteNode(self, root, key):
if not root:
return root
elif key < root.key:
root.left = self.delete(root.left, key)
elif key > root.key:
root.right = self.delete(root.right, key)
else:
if root.left is None:
temp = root.right
root = None
return temp
elif root.right is None:
temp = root.left
root = None
return temp
temp = self.getMinValueNode(root.right)
root.key = temp.key
root.value = temp.value
root.right = self.delete(root.right,
temp.key)
if root is None:
return root
root.height = 1 + max(self.getHeight(root.left),
self.getHeight(root.right))
balanceFactor = self.getBalance(root)
if balanceFactor > 1:
if self.getBalance(root.left) >= 0:
return self.rightRotate(root)
else:
root.left = self.leftRotate(root.left)
return self.rightRotate(root)
if balanceFactor < -1:
if self.getBalance(root.right) <= 0:
return self.leftRotate(root)
else:
root.right = self.rightRotate(root.right)
return self.leftRotate(root)
return root
def leftRotate(self, z):
y = z.right
T2 = y.left
y.left = z
z.right = T2
z.height = 1 + max(self.getHeight(z.left),
self.getHeight(z.right))
y.height = 1 + max(self.getHeight(y.left),
self.getHeight(y.right))
return y
def rightRotate(self, z):
y = z.left
T3 = y.right
y.right = z
z.left = T3
z.height = 1 + max(self.getHeight(z.left),
self.getHeight(z.right))
y.height = 1 + max(self.getHeight(y.left),
self.getHeight(y.right))
return y
def getHeight(self, root):
if not root:
return 0
return root.height
def getBalance(self, root):
if not root:
return 0
return self.getHeight(root.left) - self.getHeight(root.right)
def getMinValueNode(self, root):
if root is None or root.left is None:
return root
return self.getMinValueNode(root.left)
def preOrder(self):
return self.preOrderRec(self.root, [])
def preOrderRec(self, root, res):
if not root:
return res
return self.preOrderRec(root.right, self.preOrderRec(root.left, res) + [root])
def find_ge(self, key):
root = self.root
if not root:
return root
cur = root
last = None
while cur:
if key < cur.key:
last = cur
cur = cur.left
else:
if key > cur.key:
cur = cur.right
else:
return cur.value
return last.value if last else None
def find_gt(self, key):
root = self.root
if not root:
return root
cur = root
last = None
while cur:
if key < cur.key:
last = cur
cur = cur.left
else:
cur = cur.right
return last.value if last else None
class OrderedSet():
def __init__(self):
self.arbol = AVL_Tree()
def add(self, key):
self.arbol.insert(key, key)
def find_gt(self, key):
return self.arbol.find_gt(key)
# XXX: https://thispointer.com/python-how-to-make-a-class-iterable-create-iterator-class-for-it/
def __iter__(self):
return iter(self.arbol.preOrder())
def fuerza_bruta(a, m):
r = 0
suma_mod = partial(lambda m, x, y:(x % m + y % m) % m, m)
for i in range(len(a)):
for j in range(i + 1, len(a)):
st = reduce(suma_mod, a[i:j + 1], 0)
if st > r:
r = st
return r
def maximumSum(a, m):
# logger.debug("a {} m {}".format(a, m))
suma_mod = partial(lambda m, x, y:(x % m + y % m) % m, m)
resta_mod = partial(lambda m, x, y:(x - y + m) % m, m)
ord_set = OrderedSet()
unord_set = set()
acum = 0
r = 0
for n in a:
acum = suma_mod(acum, n)
logger.debug("acm {}".format(acum))
sig = ord_set.find_gt(acum)
optim = acum
if sig:
optim = resta_mod(acum, sig)
logger.debug("optimus {}".format(optim))
if optim > r:
r = optim
if acum not in unord_set:
ord_set.add(acum)
unord_set.add(acum)
# todos = list(map(lambda n:n.key, ord_set))
# todos_s = sorted(todos)
# assert todos == todos_s, "esperado {} obtenido {}".format(todos_s, todos)
# logger.debug("r es {} ord set {}".format(r, list(map(lambda n:n.key, ord_set))))
# rt=fuerza_bruta(a, m)
# assert r==rt, "esperado {} real {}".format(r,rt)
return r
if __name__ == '__main__':
# logging.basicConfig(format='%(asctime)s %(levelname)-10s %function(processName)s [%(filename)s:%(lineno)s - %(funcName)20s() ] %(name)s %(message)s')
logging.basicConfig(format='%(message)s')
logger = logging.getLogger('main')
logger.setLevel(logging.DEBUG)
logger.setLevel(logging.ERROR)
fptr = open(os.environ['OUTPUT_PATH'], 'w')
q = int(input())
for q_itr in range(q):
nm = input().split()
n = int(nm[0])
m = int(nm[1])
a = list(map(int, input().rstrip().split()))
result = maximumSum(a, m)
fptr.write(str(result) + '\n')
fptr.close()