USACO Generic Cow Protests Gold

USACO Generic Cow Protests Gold

JDOJ传送门

洛谷传送门

Description

Farmer John's N (1 <= N <= 100,000) cows are lined up in a row and
numbered 1..N. The cows are conducting another one of their strange
protests, so each cow i is holding up a sign with an integer A_i
(-10,000 <= A_i <= 10,000).

FJ knows the mob of cows will behave if they are properly grouped
and thus would like to arrange the cows into one or more contiguous
groups so that every cow is in exactly one group and that every
group has a nonnegative sum.

Help him count the number of ways he can do this, modulo 1,000,000,009.

By way of example, if N = 4 and the cows' signs are 2, 3, -3, and
1, then the following are the only four valid ways of arranging the
cows:

(2 3 -3 1)
(2 3 -3) (1)
(2) (3 -3 1)
(2) (3 -3) (1)

Note that this example demonstrates the rule for counting different
orders of the arrangements.

Input

* Line 1: A single integer: N

* Lines 2..N + 1: Line i + 1 contains a single integer: A_i

Output

* Line 1: A single integer, the number of arrangements modulo
1,000,000,009.

Sample Input

4 2 3 -3 1

Sample Output

4


题解:

加强版。

可用BFS水过。

代码:

#include<bits/stdc++.h>
#define R register
#pragma GCC optimize(2)
using namespace std;
int n,lie[100001],ans[100001];
bool v[100001];
priority_queue<int,vector<int>,greater<int> > e;
char *p1,*p2,buf[100000];
#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
inline int read()
{
    int x=0,f=1;
    char ch=nc();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
            f=-1;
        ch=nc();
    }
    while(ch>='0'&&ch<='9')
        x=x*10+ch-'0',ch=nc();
   	return x*f;
}
inline void bfs(int now)
{
	long long k=0;
	for(R int i=now+1;i<=n;i++)
    {
		k+=lie[i];
		if(k>=0)
        {
			ans[i]+=ans[now];
			ans[i]%=1000000009;
			if(!v[i])
            {
				e.push(i);
				v[i]=1;
			}
		}
	}
}
int main()
{
	ans[0]=1;
	n=read();
	for(R int i=1;i<=n;i++)
		lie[i]=read();
	e.push(0);
	while(!e.empty())
    {
		bfs(e.top());
        e.pop();
	}
	printf("%d",ans[n]);
    return 0;
}
posted @ 2020-10-16 08:46  Seaway-Fu  阅读(187)  评论(0编辑  收藏  举报