这个题不要想复杂了,只需要关注两个量即可。第一个是有新来的猪猪时,你要留出的地方给猪住。第二个就是有医生来看时,之后需要的鸟舍是 x / 2 + 1 个, 所以这个题的代码比较简短。
#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main()
{
int t = 1;
cin >> t;
while (t --)
{
int n; cin >> n;
int ans = 0, m = 0, s = 0;
for (int i = 0; i < n; i ++)
{
int x; cin >> x;
if (x == 1) s ++, m ++, ans = max(ans, m);
else {if (s > 0) m = s / 2 + 1;}
}
cout << ans << "\n";
}
return 0;
}
注意一下,有可能ans 没有更新,所以ans 初始化的时候要初始化成 0, 不要初始化成 -2e9
该文描述了一个关于Codeforces平台上Problem-B的问题解决方案。问题涉及到管理猪猪和医生来访时的鸟舍需求。关键在于处理新来的猪和医生访问时所需的空间,代码实现简洁,主要关注两个变量的变化。在循环中更新答案ans,确保其始终为最大值,同时注意避免未正确更新ans的情况。

被折叠的 条评论
为什么被折叠?



