明灯
————————————————————————————————————— 这题贪心,找出3个连续段判断即可
#include<bits/stdc++.h> using namespace std; const int N=100005; int n,a[N]; struct mess{ int suf,pre,val; }q[N]; int main(){ scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); } int cnt=0,sum=1,l=1;// for(int r=2;r<=n;r++){ if(a[r]==a[r-1]){ q[++cnt].pre=l,q[cnt].suf=r-1,q[cnt].val=sum; l=r,sum=1; continue; } sum++; } q[++cnt].pre=l,q[cnt].suf=n,q[cnt].val=sum; q[0].pre=2,q[1].suf=2,q[cnt+1].pre=2,q[cnt+1].suf=2; int ans=0; for(int i=1;i<=cnt;i++){ int sum=0,sum1=0; sum=q[i].val,sum1=q[i].val; int prehead=a[q[i-1].suf],nowhead=a[q[i].pre],nowend=a[q[i].suf],sufhead=a[q[i+1].pre];// if(prehead!=nowhead){ sum+=q[i-1].val; } if(prehead==nowhead){ sum1+=q[i-1].val;// } if(nowend!=sufhead){ sum+=q[i+1].val; } if(nowend==sufhead){ sum1+=q[i+1].val; } ans=max(ans,sum); ans=max(ans,sum1); } printf("%d",ans); }