CF1428F
这题,*1700 吧\
似乎跟题解区做法都不同。。。
考虑从左往右枚举右端点
分三种情况讨论:
-
-
s_i=1$ 且 $c\ge 2$: 由于以这段 1 前面的点为左端点时答案至少为 $c$,所以 $t\leftarrow(cnt_{c-1}-1), cnt_c\leftarrow(cnt_{c-1}-1) -
s_i=1$ 且 $c=1$:$cnt_c\leftarrow cnt_{c-1}
注意最后要将
code:
#include<bits/stdc++.h>
using namespace std;
int n;
string s;
int c;
long long ans,t,cnt[500005];
int main(){
cin>>n>>s;
for(int i=0;i<n;i++){
if(s[i]=='0'){
c=0;
cnt[0]++;
}
else if(!c){
c++;
cnt[1]+=cnt[0]+1;
t+=cnt[0]+1;
cnt[0]=0;
}
else{
c++;
cnt[c]++;
t+=c;
cnt[c]+=cnt[c-1]-1;
t+=cnt[c-1]-1;
cnt[c-1]=1;
}
ans+=t;
}
cout<<ans;
return 0;
}