全部评论
感觉就按照题目的要求一步一步走就行,也没用什么数据结构 import sys
[n,m,q] = list(map(int, sys.stdin.readline().strip().split()))
tmp = list(sys.stdin.readline().strip().split())
path = []
for i in tmp:
if i.isdigit():
path.append(int(i))
else:
path.append(i)
for _ in range(q):
# 读取每一行
line = sys.stdin.readline().strip()
# 把每一行的数字分隔后转化成int列表
[l, r] = list(map(int, line.split()))
valid = path[l-1: r]
curr = 0
mark = 0
# 1: left, 0: right
orient = 1
# 上一个是'<'或'>'
flag = 0
while curr >= 0 and curr < len(valid):
if valid[curr] == '>':
if flag:
valid.pop(curr)
if orient:
curr = curr
else:
curr -= 1
else:
orient = 1
curr += 1
flag = 1
elif valid[curr] == '<':
if flag:
valid.pop(curr)
if orient:
curr = curr
else:
curr -= 1
else:
orient = 0
curr -= 1
flag = 1
elif valid[curr] == 0:
valid.pop(curr)
if orient:
curr = curr
else:
curr -= 1
else:
mark += valid[curr]
valid[curr] -= 1
flag = 0
if orient:
curr += 1
else:
curr -= 1
print(mark)
同,蹲一个大佬来解答一下😂
等一个大佬,连续的符号怎么处理呀
我感觉你是不是pop反了,比方说><从左到右,删掉的是>,你好像删掉的是<,题目好像是说(如果当前为》|<且下一个也为这个,那么去掉,我理解的是删掉当前的
一步步删不行,要看结构,如果一段字符串是封闭的">*****<",这里面方块的分都可以得完,最后的方向取决于这段里面最大的数。如果不是封闭的就直接跑出界。
def main(l,r):
sub = list(seq[l:r + 1])
i = 0
score = 0
stack = []
flag = ''
while 0 <= i < len(sub):
####### 还未有标志位的处理
if flag == "" and sub[i] not in "<>":
score += int(sub[i])
sub[i] = str(int(sub[i]) - 1)
stack.append(int(sub[i]))
i += 1
elif flag == "" and sub[i] == "<":
score += sum(stack)
return score
elif flag == '' and sub[i] == ">":
flag += sub[i]
idx = i
i += 1
stack = []
########## 标志位为">"
elif flag == ">" and sub[i] not in "<>":
stack.append(int(sub[i]))
sub[i] = str(int(sub[i]) - 1)
i += 1
if i == len(sub):
score += sum(stack)
return score
elif flag == ">" and sub[i] == ">":
score += sum(stack)
stack = []
idx = i
i += 1
elif flag == ">" and sub[i] == "<":
# 根据stack里最大的数值来判断删除">"还是"<"
maxx = max(stack)
for j in stack:
tmp = ((1 + j) * j) // 2
score += tmp
if maxx % 2 == 0:
sub = sub[:idx + 1] + sub[i + 1:]
i = idx + 1
flag = ">"
else:
sub = sub[:idx] + sub[i:]
i = idx - 1
flag = "<"
stack = []
########### 标志位为"<"
elif flag == "<" and sub[i] not in "<>":
stack.append(int(sub[i]))
sub[i] = str(int(sub[i]) - 1)
i -= 1
if i == -1:
score += sum(stack)
return score
elif flag == "<" and sub[i] == "<":
score += sum(stack)
stack = []
i -= 1
elif flag == "<" and sub[i] == ">":
# 根据stack里最大的数值来判断删除">"还是"<"
maxx = max(stack)
for j in stack:
tmp = ((1 + j) * j) // 2
score += tmp
if maxx % 2 != 0:
sub = sub[:i + 1] + sub[idx + 1:]
i += 1
flag = ">"
else:
sub = sub[:i] + sub[idx:]
i -= 1
flag = "<"
stack = []
return score
if __name__ == '__main__':
n,m,q = map(int,input().split())
seq = [c for c in input().split()]
for _ in range(q):
a,b = map(int,input().split())
l = a - 1
r = b - 1
if seq[l] == "<":
print(0)
elif l == r:
if seq[l] not in "<>":
print(seq[l])
else:
print(0)
else:
print(main(l,r)) 贴一段代码,不需要每次只减一位字符串的数字,优化了(">"..."<")该种情况下的处理,测试案例都通过了,考试的时候没写完,没法测试
相关推荐
11-20 18:35
湘潭大学 人力资源VP/CHO 点赞 评论 收藏
分享