Codeforces 617B:Chocolate(思维)

题目链接http://codeforces.com/problemset/problem/617/B

题意

有一个数组,数组中的元素均为0或1 。要求将这个数组分成一些区间,每个区间中的1的个数均为1。问有多少种分法

思路

将数组中的0全部去掉,用一个数组记录原数组中的每个1的位置,相邻的两个1的位置差为两个1之间的0的个数,让这些0的个数一次相乘就行了。但是要注意原数组中全是0的情况,还要注意数据类型,会爆int

AC代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
const double E=exp(1);
const int maxn=1e6+10;
using namespace std;
int a[maxn];
int main(int argc, char const *argv[])
{
	int n;
	cin>>n;
	int x;
	int j=0;
	for(int i=0;i<n;i++)
	{
		cin>>x;
		if(x==1)
			a[++j]+=i;
	}
	ll ans=1;
	if(j==0)
		cout<<"0"<<endl;
	else
	{
		for(int i=1;i<j;i++)
		{
			ans=ans*(a[i+1]-a[i]);
		}
		cout<<ans<<endl;
	}
	return 0;
}
原文地址:https://www.cnblogs.com/Friends-A/p/10324441.html