题解:P12663 [KOI 2023 Round 2] 湖边的蚁穴
luckyyunji · · 题解
简单题。
容易发现小房间住满一定优,首先因为小房间不相邻,满足条件,其次如果有小房间,肯定比只住在大房间的数量要多。
住满小房间后,大房间就不能住了,此时不能住的大房间把环分成一个一个链,显然在链上最多住
当然有特殊情况:当没有小房间时,就是一个环,在环上,与链的情况不同,显然原来能在链两头的,现在不能,所以在环上最多住
还要注意这个链可能跨越了环,特别判断,具体见代码。
码风不良,见谅。
#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;
}