-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHash.py
149 lines (111 loc) · 2.9 KB
/
Hash.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
ka=[]
with open('example.txt','r') as f:
f_contents = f.readlines() # gives list of each line \
for j in range (0 , len (f_contents) ) :
for i in f_contents[j].split():
ka.append(i)
#print ka
"""
from string import rstrip
with open('example.txt') as f:
ka = map(rstrip, f)
for i in range(len(ka)):
p=ka[i]
p.split()
ka[i]=p """
def matrix(ka,x):
return[ka[i:i+x] for i in xrange(0,len(ka),x)]
ka=matrix(ka,6)
#print ka
R=int(ka[0][0])
C=int(ka[0][1])
F=int(ka[0][2])
N=int(ka[0][3])
B=int(ka[0][4])
T=int(ka[0][5])
ka.pop(0)
#print ka
#print ka[0][8] ###pada artio
def steps(a,b,x,y):
return (abs(a-x)+abs(y-b))
"""b=[]
for i in range (int(N)):
b1=ka[i].split(' ')
b.append(b1)
#print b
"""
b=ka
rides=[]
for i in range(int(N)):
rides.append([steps(int(b[i][0]),int(b[i][1]),int(b[i][2]),int(b[i][3])),i])
reversed(sorted(rides))
#print rides
fromzero=[]
for i in range(int(N)):
fromzero.append([steps(int(b[i][0]),int(b[i][1]),0,0),i])
#print fromzero
def mergeSort(alist):
if len(alist)>1:
mid = len(alist)//2
lefthalf = alist[:mid]
righthalf = alist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
i=0
j=0
k=0
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
alist[k]=lefthalf[i]
i=i+1
else:
alist[k]=righthalf[j]
j=j+1
k=k+1
while i < len(lefthalf):
alist[k]=lefthalf[i]
i=i+1
k=k+1
while j < len(righthalf):
alist[k]=righthalf[j]
j=j+1
k=k+1
mergeSort(fromzero)
#print fromzero
time=[]
timetostart=[]
for i in range(len(rides)):
temp=rides[i][1]
for j in range(len(fromzero)):
if (fromzero[j][1]==temp):
dist_index=j
#print fromzero[j][0]
break
time.append([(fromzero[dist_index][0]+rides[i][0]+int(b[temp][4])),i])
timetostart.append([(fromzero[dist_index][0]+int(b[temp][4])),i])
#print time
mergeSort(timetostart)
#print timetostart
diadr=[]
sums=[]
for i in range(F):
sums.append(0)
for i in range(F):
diadr.append(str(timetostart[i][1]))
sums[i]=sums[i]+timetostart[i][0]
#print diadr
j=0
for i in range(F,N):
if ((time[j][0]+sums[j])<int(b[i][5]) or ((int(b[i][5])-time[j][0]-sums[j])<0)): #and gia to D
(diadr[j])=str(diadr[j])+' '+str(timetostart[i][1])
j=j+1
if (j==F):
j=0
#print diadr
cd=open("example2.txt",'w')
for i in range (F) :
cd.write("%d" %len(diadr[i].split(' ')))
cd.write(' ')
cd.write ("%s" %diadr[i])
cd.write( '\n')
cd.close()