关注
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)) 贴一段代码,不需要每次只减一位字符串的数字,优化了(">"..."<")该种情况下的处理,测试案例都通过了,考试的时候没写完,没法测试
查看原帖
点赞 评论
相关推荐
牛客热帖
正在热议
# 拼多多求职进展汇总 #
235710次浏览 2038人参与
# ai智能作图 #
25251次浏览 296人参与
# 阿里云管培生offer #
61116次浏览 1755人参与
# 25届秋招总结 #
404554次浏览 4053人参与
# 25届机械人为了秋招做了哪些准备? #
25789次浏览 360人参与
# 地方国企笔面经互助 #
6769次浏览 16人参与
# 北方华创开奖 #
66599次浏览 550人参与
# 机械求职避坑tips #
22992次浏览 245人参与
# 实习,投递多份简历没人回复怎么办 #
2438361次浏览 34725人参与
# 软件开发投递记录 #
1480251次浏览 23940人参与
# 我的实习求职记录 #
6128013次浏览 83997人参与
# 机械人怎么评价今年的华为 #
157555次浏览 1350人参与
# 我在牛爱网找对象 #
74641次浏览 554人参与
# 歌尔求职进展汇总 #
42847次浏览 294人参与
# 如果再来一次,你还会选择这个工作吗? #
113663次浏览 1127人参与
# 在职场上,你最讨厌什么样的同事 #
5917次浏览 89人参与
# 硬件兄弟们 甩出你的华为奖状 #
78319次浏览 627人参与
# 如果可以,你希望哪个公司来捞你 #
32876次浏览 193人参与
# 虾皮求职进展汇总 #
87283次浏览 701人参与
# 经纬恒润求职进展汇总 #
99609次浏览 961人参与
# 华为工作体验 #
110811次浏览 860人参与
# 牛客租房专区 #
5934次浏览 146人参与