跳到正文

题解

题解:P12663 [KOI 2023 Round 2] 湖边的蚁穴

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

文章信息

发布
2026年2月12日
收录
2026年2月12日
更新
2026年11月11日
篇幅
约 1 分钟
题目
P12663

版本

  1. v1当前

简单题。

容易发现小房间住满一定优,首先因为小房间不相邻,满足条件,其次如果有小房间,肯定比只住在大房间的数量要多。

住满小房间后,大房间就不能住了,此时不能住的大房间把环分成一个一个链,显然在链上最多住 ⌈len2⌉\lceil \frac{len}{2} \rceil。

当然有特殊情况:当没有小房间时,就是一个环,在环上,与链的情况不同,显然原来能在链两头的,现在不能,所以在环上最多住 ⌊len2⌋\lfloor \frac{len}{2} \rfloor。

还要注意这个链可能跨越了环,特别判断,具体见代码。

码风不良,见谅。

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n;cin>>n;
	int len=0,llen=0,ans=0;
	for(int i=1;i<=n;i++){
		int x;cin>>x;ans+=x;
		if(x) ans+=(len+1)/2,len=0;
		else (ans?len++:llen++);
	}ans+=(len+llen+1)/2;
	if(llen+len==n) cout<<n/2<<endl;
	else cout<<ans<<endl;
	return 0;
}
原文