《补题》(Codeforces Round 857 (Div. 2), problem: (B) Settlement of Guinea Pigs)

243 阅读1分钟

思维:首先考虑当前买进的豚鼠数量,以及目前所需要的房子数量和用来更新答案的房子数的最大值。

这里考虑分别用co, ho, ans来表示。接着就要考虑当x == 1和x == 2的不同情况。

首先当x == 1时,此时co++ && ho++,当x == 2时,我们可以得到豚鼠的性别,当co == 0时,此时ho == 0,当co == 1时,此时ho == 1,当co == 2时,此时ho == 2,当co == 3时,此时ho == 2......可知ho == co / 2 + 1 && 对co == 0时不成立。故需要特判,最后利用max函数更新答案。

#include <bits/stdc++.h>

using i64 = long long;

void solve() {
	int n;
	std::cin >> n;

	i64 ans = 0, ho = 0, co = 0;
	for (int i = 0; i < n; i++) {
		int x;
		std::cin >> x;

		if (ho == 0 && x == 2) {
			continue;
		}
		x == 1 ? ho++, co++ : ho = co / 2 + 1;
		ans = std::max(ans, ho);
	}
	std::cout << ans << "\n";


}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);

	int t;
	std::cin >> t;

	while (t--) {
		solve();
	}

	return 0;
}