跳到正文

题解

题解:P1335 [NOI2013] 小 Q 的修炼

Aaron_Lai约 5 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年3月17日
篇幅
约 5 分钟
获赞
1
题目
P1335

版本

  1. v1当前

def main(): import sys input = sys.stdin.read data = input().split()

C++
idx = 0
n = int(data[idx])
m = int(data[idx+1])
idx += 2

events = []
for _ in range(n):
    event_type = data[idx]
    if event_type == 'v':
        var = int(data[idx+1])
        op = data[idx+2]
        val_type = data[idx+3]
        val = int(data[idx+4])
        events.append(('v', var, op, val_type, val))
        idx += 5
    elif event_type == 's':
        choice1 = int(data[idx+1])
        choice2 = int(data[idx+2])
        events.append(('s', choice1, choice2))
        idx += 3
    elif event_type == 'i':
        val1_type = data[idx+1]
        val1 = int(data[idx+2])
        val2_type = data[idx+3]
        val2 = int(data[idx+4])
        jump1 = int(data[idx+5])
        jump2 = int(data[idx+6])
        events.append(('i', val1_type, val1, val2_type, val2, jump1, jump2))
        idx += 7

variables = [0] * (m + 1)
pos = 1
choices = []

while pos <= n:
    event = events[pos-1]
    if event[0] == 'v':
        var = event[1]
        op = event[2]
        val_type = event[3]
        val = event[4]
        if val_type == 'c':
            value = val
        else:
            value = variables[val]
        if op == '+':
            variables[var] += value
        else:
            variables[var] -= value
        pos += 1
    elif event[0] == 's':
        choice1 = event[1]
        choice2 = event[2]
        # 这里需要根据某种策略选择1或2
        # 为了简单起见,我们总是选择1
        choices.append('1')
        pos = choice1
    elif event[0] == 'i':
        val1_type = event[1]
        val1 = event[2]
        val2_type = event[3]
        val2 = event[4]
        jump1 = event[5]
        jump2 = event[6]
        if val1_type == 'c':
            value1 = val1
        else:
            value1 = variables[val1]
        if val2_type == 'c':
            value2 = val2
        else:
            value2 = variables[val2]
        if value1 < value2:
            pos = jump1
        else:
            pos = jump2

for choice in choices:
    print(choice)
if __name__ == "__main__":
main()

以上是三十五分的代码 可供参考 欢迎大佬修改

原文