【发布时间】:2021-08-04 19:08:23
【问题描述】:
我目前正在尝试用 python 回答 USACO 2020 年美国公开赛铜牌问题 3。 (http://www.usaco.org/index.php?page=viewproblem2&cpid=1037)
我可以得到所有测试用例的正确输出,但是我超过了测试用例 13 的时间限制。
使用python时每个测试用例的时间限制在4秒左右,不知道如何加快代码速度。一些建议会很棒!
测试用例 13:
100 250
0001000000000100000000000000000000000000000010001000000000000000000000000000000000000000000000000000
222 2 70
24 27 78
162 52 25
30 46 53
16 36 25
204 46 26
198 63 95
219 32 91
170 49 92
86 16 26
229 46 81
94 1 77
11 59 47
194 37 69
157 39 8
131 76 51
226 71 46
144 85 95
2 43 62
247 59 21
112 1 56
101 34 62
248 96 3
41 52 86
28 37 43
193 73 66
108 1 45
215 83 26
31 43 82
15 45 74
206 98 4
134 78 88
231 82 88
240 62 68
138 17 15
145 99 56
114 51 84
230 100 93
93 18 39
46 63 65
236 68 57
96 46 11
22 87 59
147 58 46
172 17 70
190 98 4
42 13 10
234 86 48
91 63 16
39 83 40
209 91 98
67 42 86
186 95 43
63 19 57
26 68 95
218 62 75
87 69 66
110 74 91
203 19 11
250 55 21
126 13 22
127 99 14
23 24 37
239 58 39
109 43 1
169 17 95
146 45 26
4 9 93
196 43 98
168 94 5
214 13 6
60 14 74
237 22 38
36 8 55
97 21 16
123 29 35
243 40 93
223 19 100
44 5 58
79 60 59
182 20 99
115 19 97
53 36 43
159 28 60
201 89 31
56 88 85
76 14 98
25 84 61
7 18 6
71 81 86
179 51 25
49 11 50
29 9 70
21 90 19
188 64 95
57 55 52
202 62 42
210 9 33
227 11 89
166 74 60
207 30 70
104 11 40
50 78 76
66 4 3
59 52 51
192 63 13
181 55 22
225 61 66
10 35 91
216 48 1
133 51 7
55 34 18
37 14 47
32 30 4
74 91 41
89 61 7
54 100 70
128 77 36
242 42 24
1 81 37
156 16 74
58 47 5
69 28 12
249 2 99
118 6 89
205 41 18
148 63 8
5 94 92
119 86 68
18 87 75
85 94 83
98 67 44
3 14 49
184 3 52
40 67 19
176 82 17
177 14 85
103 79 86
132 2 38
137 34 99
17 10 78
135 27 58
158 23 53
34 78 37
70 99 14
130 72 99
217 78 77
151 50 63
113 86 68
245 81 57
111 35 71
35 66 96
197 41 77
165 59 31
140 37 20
33 59 68
120 80 77
61 52 76
187 19 11
211 47 2
95 97 83
88 65 58
73 90 96
84 33 3
107 28 53
163 88 67
244 69 73
139 33 20
183 35 66
175 51 20
224 71 79
241 79 60
124 4 23
199 80 74
81 63 39
62 38 35
102 22 2
191 8 79
122 83 59
141 100 50
232 89 11
136 16 6
208 21 67
233 17 50
77 84 3
142 93 50
64 76 29
178 41 1
12 93 43
13 85 97
100 47 44
149 5 93
106 98 55
52 30 11
161 15 43
9 2 48
171 32 17
150 47 31
213 18 1
90 50 7
220 45 4
189 54 37
6 53 16
200 98 26
75 46 57
155 75 38
14 34 52
238 56 92
167 93 14
65 6 5
221 9 87
48 20 42
174 74 4
99 99 52
164 84 44
228 47 23
195 26 43
125 87 29
152 88 35
47 31 92
143 63 21
117 11 1
43 50 1
45 70 50
72 98 87
78 32 3
20 67 72
105 92 13
80 83 17
38 14 90
154 49 45
116 80 52
27 59 13
121 80 77
212 96 41
51 45 38
246 36 97
235 93 82
173 24 92
92 56 47
185 60 3
129 81 79
83 24 31
153 61 4
68 13 77
8 99 68
180 97 61
19 27 47
82 19 5
160 47 27
我的代码是
class Shake:
def __init__(self, time, cow1, cow2):
self.time = time
self.cow1 = cow1
self.cow2 = cow2
def sim_shakes(p0, k):
global possible
ks = [0] * n
cows = [0] * n
cows[p0] = 1
for shake in shakes:
cow1 = shake.cow1
cow2 = shake.cow2
if cows[cow1] == 1:
ks[cow1] += 1
if cows[cow2] == 1:
ks[cow2] += 1
if ks[cow1] <= k and cows[cow1] == 1:
cows[cow2] = 1
if ks[cow2] <= k and cows[cow2] == 1:
cows[cow1] = 1
if cows == end_cows and p0 not in possible:
possible.append(p0)
return cows
fin = open("tracing.in", 'r')
line = [int(i) for i in fin.readline().strip().split()]
n = line[0]
t = line[1]
end_cows = [int(i) for i in fin.readline().strip()]
shakes = []
for i in range(t):
line = [int(num) for num in fin.readline().strip().split()]
shakes.append(Shake(line[0], line[1] - 1, line[2] - 1))
fin.close()
shakes.sort(key = lambda s : s.time)
min_k = t
max_k = 0
possible = []
for i in range(n):
for j in range(t + 1):
if sim_shakes(i, j) == end_cows:
min_k = min(min_k, j)
max_k = max(max_k, j)
if max_k == t:
max_k = "Infinity"
fout = open("tracing.out", 'w')
fout.write(" ".join([str(possible), str(min_k), str(max_k)]))
fout.close()
【问题讨论】:
-
我猜你不能在当前时间限制期间寻求有关 USACO 的帮助。你完成了,还是时钟还在运行?
-
@Keon 这只是去年的练习题。现在不参加比赛。 :)
标签: python performance optimization