B. Yet Another Bookshelf(思维)

it2024-11-21  6

https://codeforces.com/contest/1433/problem/B


思路:统计每个1与其最近1之间有多少个0,有就累加。

#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=60; typedef long long LL; LL a[maxn]; int main(void) { cin.tie(0);std::ios::sync_with_stdio(false); LL t;cin>>t; while(t--) { LL n;cin>>n; LL num=0; for(LL i=1;i<=n;i++) { cin>>a[i]; if(a[i]==1){ num++; } } if(num==1||num==0){ cout<<0<<endl;continue; } else{ LL sum=0; for(LL i=1;i<=n;i++) { if(a[i]==1){ for(LL j=i+1;j<=n;j++) { if(a[j]==1){ sum+=((j-i-1)); break; } } } } cout<<sum<<endl; } } return 0; }  

 

最新回复(0)