Official

B - Multi Test Cases Editorial by en_translator


A multi test-case problem is a problem where an input file contains multiple test cases, and this problem is an instance.
With many test cases in a single input, multi test-case is commonly used to augment the test cases or reduce the input files.

The implementation required for a multi test-case problem is almost the same as for an ordinary problem. The only difference is that you have to write a code that solves the problem \(T\) times instead of once. It can be achieved with a for statement as follows:

int main() {
  int T;
  cin >> T;
  for(int t = 0; t < T; t++) {
    // Solve the problem here
  }
}

Some of you may complain that the deeply nested scope (the part surrounded by the curly braces) is confusing. If you prefer, you may write the answering part in a separate function.

void solve() {
  // Solve the problem here
}

int main() {
  int T;
  cin >> T;
  for(int t = 0; t < T; t++) solve();
}

Once you write such a code, all that left is implement as usual in the commented place.

Next, we describe how to solve each test case. You can use a for statement to solve it. Specifically, implement the following procedure:

  • Receive \(N\) from the input.
  • Prepare a variable \(\mathrm{ans}\) to store the answer, initialized with \(0\).
  • Receive \(N\) integers from the input with a for statement. For each integer, if it is odd, i.e. indivisible by \(2\), add \(1\) to \(\mathrm{ans}\).
  • The resulting \(\mathrm{ans}\) is the answer.

The time complexity is \(\mathrm{O}(N)\), so the time complexity for an input is \(\mathrm{O}(TN)\), which is fast enough.

posted:
last update: