返回学习页
知识点指南

Arrays

Array problems are about positions and values in a sequence. The main decision is what to remember while scanning: a count, best value, previous value, current streak, or another compact state.

什么时候使用

读题时可以留意这些信号。

  • The input is a sequence of numbers, labels, positions, or scores.
  • The answer depends on neighbours, ranges, counts, or positions.
  • A single pass or a sorted pass can keep enough information.

常见错误

提交前可以检查这些小陷阱。

  • Mixing one-based and zero-based positions.
  • Reading outside the array bounds.
  • Initialising best or answer to a value that fails on negative inputs.

怎么写成代码

把知识点翻译成变量、循环、判断和更新。

  • Read values into a vector/list when you need positions, neighbours, sorting, or multiple passes.
  • Use a loop index when the position matters.
  • Use a direct value loop when only the value matters.
  • Store previous, current, best, count, or total depending on the story.

在 Block 题里长什么样

答题时通常会看到这些步骤被拆成代码块。

  1. 1Read N.
  2. 2Read the array.
  3. 3Initialise state from 0, first value, or a large/small sentinel.
  4. 4Loop over positions or values.
  5. 5Compare/update state.
  6. 6Print result.

题目里怎么出现

读题时先找这些信号,再决定答案区应该选哪些 blocks。

  • The statement mentions first, last, neighbouring, consecutive, position, or list.
  • The input gives N followed by N values.
  • The answer can be discovered by scanning all records once.

Track a best value

一个小型模板,展示代码和 blocks 的对应关系。

C++

int best = values[0];
for (int i = 1; i < N; i++) {
    if (values[i] > best) {
        best = values[i];
    }
}
cout << best << '\n';

Python

best = values[0]
for i in range(1, N):
    if values[i] > best:
        best = values[i]
print(best)

对应 block 顺序

  1. 1Read N and the array.
  2. 2Set best from the first value.
  3. 3Loop from the second position onward.
  4. 4Compare current value with best.
  5. 5Update best when needed.
  6. 6Output best.

建议先练这道题

可以先从 Buried Treasure 开始,再回到这里复盘知识点信号。

打开题目