Official

B - Locked Rooms Editorial by en_translator


If \(L_i = 0\) for all \(i\)

Two people can both reach rooms \(0, 1, \ldots, N\), so the answer is \(0\).

If there exists \(i\) such that \(L_i = 1\)

Let \(X\) and \(Y\) be the minimum and maximum \(i\) with \(L_i = 1\), respectively. The person initially in room \(0\) can reach rooms \(0, 1, \ldots, X - 1\), and the person initially in room \(N\) can reach rooms \(Y, Y + 1, \ldots, N\). Therefore, the rooms where neither of the two can reach are \(X, X + 1, \ldots, Y - 1\), which is \((Y - X)\) rooms. In particular, if \(X = Y\), then the answer is \(0\).

Sample code

#include <bits/stdc++.h>
using namespace std;

int main() {
	int n;
	cin >> n;
	vector<int> l(n);
	for (int i = 0; i < n; i++) cin >> l[i];
	int x = 0, y = 0;
	for (int i = 0; i < n; i++) {
		if (l[i] == 1) {
			x = i;
			break;
		}
	}
	for (int i = n - 1; i >= 0; i--) {
		if (l[i] == 1) {
			y = i;
			break;
		}
	}
	cout << y - x << '\n';
}

posted:
last update: