You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python入门者求教:如何关联两个数字数组(Codeforces Lecture Sleep问题)

Lecture Sleep 问题:关联两个数组的方法

Hey there! Let's walk through this clearly since you're new to Python and tackling the Lecture Sleep problem.

First, let's unpack the input you provided: 6 3 1 3 5 2 5 4 1 1 0 1 0 0. The problem's input structure has three key parts (even though it might look like a single line here):

  • The first two numbers are n=6 (total number of students) and k=3 (the length of consecutive students you can wake up)
  • The next 6 numbers form your second array (knowledge points each student contributes): [1, 3, 5, 2, 5, 4]
  • The final 6 numbers form your third array (awake/asleep status: 1 = awake, 0 = asleep): [1, 1, 0, 1, 0, 0]

To pair each knowledge point with its corresponding awake status (exactly the mapping you want: 1→1, 3→1, 5→0, etc.), Python's built-in zip() function is perfect for this job. Here's a step-by-step code example:

# Step 1: Convert the input string into a list of integers
input_values = list(map(int, "6 3 1 3 5 2 5 4 1 1 0 1 0 0".split()))

# Step 2: Extract the problem's variables and arrays
n, k = input_values[0], input_values[1]
knowledge_array = input_values[2:2+n]  # The second array: [1,3,5,2,5,4]
status_array = input_values[2+n:2+n+n]  # The third array: [1,1,0,1,0,0]

# Step 3: Pair the two arrays using zip()
paired_data = list(zip(knowledge_array, status_array))

# Check the result
print(paired_data)
# Output: [(1, 1), (3, 1), (5, 0), (2, 1), (5, 0), (4, 0)]

What zip() does is take elements from each array at the same index and pair them into tuples. So the first element of the knowledge array pairs with the first element of the status array, the second with the second, and so on—exactly the mapping you need!

If you want to iterate over these pairs to do something useful (like calculate total knowledge from awake students), you can loop through the zipped result directly:

total_knowledge = 0
for points, is_awake in zip(knowledge_array, status_array):
    if is_awake == 1:
        total_knowledge += points
print(f"Total knowledge from awake students: {total_knowledge}")
# Output: Total knowledge from awake students: 6 (1+3+2)

That's the core part of linking the two arrays. Once you have this pairing, you can build on it to solve the actual problem of finding the maximum knowledge you can get by waking up k consecutive students.

内容的提问来源于stack exchange,提问作者Andrew Qiu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 20:17:32