现在比赛结束了,所以我想问我的算法在我的代码上失败了。
This is the problem. If anyone interested, you can see it at here
def solve():
S = int(input())
D, A, B, M, N = [], [], [], [], []
for i in range(S):
d, a, b = [int(c) for c in input().split(" ")]
D.append(d); A.append(a); B.append(b)
M.append(d+a); N.append(d-b)
straightMstart, straightNstart = [0]*(S), [0]*(S)
crossedMstart, crossedNstart = [0]*(S), [0]*(S) # cross over
for i in range(1, S):
if M[i] == M[i-1]:
straightMstart[i] = straightMstart[i-1]
crossedNstart[i] = crossedNstart[i-1]
else:
straightMstart[i] = i
if N[i] == N[i-1]:
straightNstart[i] = straightNstart[i-1]
crossedMstart[i] = crossedMstart[i-1]
else:
straightNstart[i] = i
if M[i] != M[i-1]:
crossedNstart[i] = straightNstart[i-1]
if N[i] != N[i-1]:
crossedMstart[i] = straightMstart[i-1]
maxlen = 1
maxlensubsets = 1
for i in range(1, S):
thislen = i - min(crossedMstart[i], crossedNstart[i]) + 1
if maxlen < thislen:
maxlen = thislen
maxlensubsets = 1
elif maxlen == thislen:
maxlensubsets += 1
# print(crossedNstart)
# print(crossedMstart)
return "%d %d" % (maxlen, maxlensubsets)
testcase = int(input())
for tc in range(1, testcase+1):
print("Case %d: %s" % (tc, solve()))
我使用交叉最大长度来找到最大的集合大小(对于M和Ns)
我将举出以下示例,以便更容易理解我的逻辑:
# Let's suppose that the M and N is:
M (=D[i]+A[i]) = [ 9, 9, 18, 22, 22]
N (=D[i]-B[i]) = [-10, -5, 7, -1, -1]
# Straight Start means starting index for same value.
# M=9 starts from index 0, M=18 starts from index 2, M=22 starts from index 3
straightMstart = [0, 0, 2, 3, 3]
# Same logic applied to straightNstart
straightNstart = [0, 1, 2, 3, 3]
# Crossed Start means cross-starting index of opponent array.
# For crossedMstart, you start from N[i] and climb N then cross to climb M
# The reason why I swapped order of cNs and cMs is that both arrays are based on opponent arrays
crossedNstart = [0, 0, 1, 2, 2]
crossedMstart = [0, 0, 0, 2, 2]
我真的很困惑,我真的不明白我的错是什么意思。请帮我纠正我的逻辑。
答案 0 :(得分:0)
您的算法有多种失败方式。最简单的是在任何输入中输入+-----------+-----------+
| 1 | 2 |
+-----------+-----------+
| … | … |
+-----------+-----------+
| 19 | 20 |
+-----------+-----------+
。然后,我确实发现string
存在问题,如果straightNstart[i]
或N[i]
匹配M[i]
或N[i-1]
,则当前迭代始终为零。这会将M[i-1]
和crossedMstart[i]
值设置为crossedNstart[i]
。另一个问题是当0
忽略两个循环时。
第一个问题很容易解决,可能会解决d,a,b,testcase的问题,其中d,a,b,testcase应该只引发一个TypeError:
S <= 1
对于另一个问题,可以迭代两次,以便S = input("[description]")
if S.isdigit():
S = int(S)
if S <= 1:
raise ValueError("Value must be greater then 1")
else:
raise TypeError("Value must be numeric")
,straightNstart[i]
分配对结果有任何影响(至少当M [i]或N [i]是不等于M [i-1]或N [i-1])。
答案 1 :(得分:0)
我发现我的算法不处理多个交叉。