跳到正文

题解

CF1802B 题解

Vitamin_B约 1 分钟
原文
文章信息与版本

文章信息

发布
2024年4月20日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 1 分钟
获赞
1
题目
CF1802B

版本

  1. v1当前

思路

首先我们要把当前的猪分成两类——有性别和无性别。无性猪只能一猪一笼,有性别时最坏情况下是 ⌊s2⌋+1\lfloor\frac s2\rfloor+1 个笼(没有有性猪时要特判)。需要的笼子就是它们之和。

代码

C++
# include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int t, n, x, sum, tot, maxx;
int main () {
	ios::sync_with_stdio (0);
	cin.tie (0);
	cout.tie (0);
	cin >> t;
	while (t --) {
		cin >> n;
		maxx = sum = tot = 0;
		for (int i = 0; i < n; ++ i) {
			cin >> x;
			if (x < 2)
				++ tot;
			else
				sum += tot, tot = 0;
			maxx = max (maxx, (sum >> 1) + tot + (sum > 0));
		}
		cout << maxx << '\n';
	}
	return 0;
}
原文