#include<iostream> #include<vector> #include<queue> #include<cstring> #include<cmath> #include<map> #include<set> #include<cstdio> #include<algorithm> #define debug(a) cout<<#a<<"="<<a<<endl; using namespace std; const int maxn=1e5+1000; typedef long long LL; LL a[maxn]; LL sum0[maxn],sum1[maxn]; LL ones=0,zeros=0; int main(void) { cin.tie(0);std::ios::sync_with_stdio(false); LL n;cin>>n; for(LL i=1;i<=n;i++){ cin>>a[i]; sum0[i]=sum0[i-1];sum1[i]=sum1[i-1]; if(a[i]) sum1[i]++,ones++; else sum0[i]++,zeros++; } LL p=0x3f3f3f3f; p=min(p,ones); p=min(p,zeros+1); for(LL i=1;i<=n;i++){ //到i这个位置的对前面0的操作,对后面的数进行单点 p=min(p,sum0[i]+1+ones-sum1[i]); } LL k=0; for(LL i=1;i<=n;i++){ if(a[i]!=a[i+1]&&i+1!=n+1) k++; } if(a[n]==1) k++; cout<<max(min(p,k),(LL)0)<<endl; return 0; }
全部评论
(3) 回帖