-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshame.py
executable file
·298 lines (213 loc) · 7.18 KB
/
shame.py
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
#!/home/ernesto/programas/instalados/python/python3.8/bin/python3.8
from bisect import insort, bisect_right
'''
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):
if not root:
return TreeNode(key, value)
elif key < root.key:
root.left = self.insertNode(root.left, key, value)
else:
if key > root.key:
root.right = self.insertNode(root.right, key, value)
else:
root.value = value
return root
root.height = 1 + max(self.getHeight(root.left),
self.getHeight(root.right))
balanceFactor = self.getBalance(root)
if balanceFactor > 1:
if key < root.left.key:
return self.rightRotate(root)
else:
root.left = self.leftRotate(root.left)
return self.rightRotate(root)
if balanceFactor < -1:
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, root):
if not root:
return
self.preOrder(root.left)
logger.debug("{0} ".format(root.key))
self.preOrder(root.right)
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)
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):
# Create prefix tree
prefix = [0] * len(a)
curr = 0;
for i in range(len(a)):
curr = (a[i] % m + curr) % m
prefix[i] = curr
# Compute max modsum
pq = [prefix[0]]
maxmodsum = max(prefix)
for i in range(1, len(a)):
# Find cheapest prefix larger than prefix[i]
left = bisect_right(pq, prefix[i])
if left != len(pq):
# Update maxmodsum if possible
modsum = (prefix[i]%m - pq[left]%m + m) % m
maxmodsum = max(maxmodsum, modsum)
# add current prefix to heap
insort(pq, prefix[i])
return maxmodsum
if __name__ == '__main__':
logging.basicConfig(format='%(asctime)s %(levelname)-10s %function(processName)s [%(filename)s:%(lineno)s - %(funcName)20s() ] %(name)s %(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()