QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#124239#5665. AA Country and King DreamoonbakuganulitRE 7ms8128kbPython31.6kb2023-07-14 14:45:122023-07-14 14:45:13

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-14 14:45:13]
  • 评测
  • 测评结果:RE
  • 用时:7ms
  • 内存:8128kb
  • [2023-07-14 14:45:12]
  • 提交

answer

def solve():
    n = int(input())
    path = list(map(int, input().split()))

    path[-1] = 1
    path[0] = 1

    available = set(range(1, n + 1))

    for num in path:
        if num in available:
            available.remove(num)

    zeros = len([x for x in path if x == 0])
    last_city = 0
    last_city_index = 0
    for i, x in enumerate(path):
        if x != 0:
            last_city = x
            last_city_index = i
        else:
            break
    next_cities = [x for x in path[last_city_index+1:] if x != 0]
    if next_cities:
        next_city = next_cities[0]
    else:
        next_city = 0

    missing_path = []
    if zeros % 2 == 0:
        if last_city > next_city:
            for i in range(zeros):
                if i % 2 == 0:
                    missing_path.append(next_city)
                else:
                    missing_path.append(available.pop())
        elif last_city < next_city:
            for i in range(zeros):
                if i % 2 == 0:
                    missing_path.append(available.pop())
                else:
                    missing_path.append(last_city)
    else:
        for i in range(zeros):
            if i % 2 == 0:
                missing_path.append(available.pop())
            else:
                missing_path.append(last_city)

    missing_path = missing_path[::-1]
    ans = [x if x != 0 else missing_path.pop() for x in path]
    print(" ".join(str(num) for num in ans))


if __name__ == "__main__":
    t = int(input())
    for _ in range(t):
        solve()

详细

Test #1:

score: 100
Accepted
time: 7ms
memory: 8128kb

input:

9
5
1 2 3 2 0 2 1 5 1
5
1 2 3 0 0 2 1 5 1
5
1 2 0 0 0 2 1 5 1
5
1 2 0 0 0 0 1 5 1
5
1 0 0 0 0 0 1 5 1
5
1 0 0 0 0 0 0 5 1
5
1 0 0 0 0 0 0 0 1
5
1 0 0 0 0 0 0 0 0
5
0 0 0 0 0 0 0 0 0

output:

1 2 3 2 4 2 1 5 1
1 2 3 2 4 2 1 5 1
1 2 3 2 4 2 1 5 1
1 2 1 3 1 4 1 5 1
1 2 1 3 1 4 1 5 1
1 2 1 3 1 4 1 5 1
1 2 1 3 1 4 1 5 1
1 2 1 3 1 4 1 5 1
1 2 1 3 1 4 1 5 1

result:

ok 9 lines

Test #2:

score: -100
Runtime Error

input:

28668
2
0 2 1
2
0 0 1
2
0 0 0
2
1 0 1
2
1 0 0
2
1 2 0
3
0 2 1 3 1
3
0 0 1 3 1
3
0 0 0 3 1
3
0 0 0 0 1
3
0 0 0 0 0
3
1 0 1 3 1
3
1 0 0 3 1
3
1 0 0 0 1
3
1 0 0 0 0
3
1 2 0 3 1
3
1 2 0 0 1
3
1 2 0 0 0
3
1 2 1 0 1
3
1 2 1 0 0
3
1 2 1 3 0
3
0 2 3 2 1
3
0 0 3 2 1
3
0 0 0 2 1
3
1 0 3 2 1
3
1 0 0 2 1
3
1 2 ...

output:

1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1

result: