zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    c++ struct 使用
    c++数组、字符串操作
    c++ List、Vector、Stack、Queue使用
    十三、哈希表
    十二、234树
    十一、红黑树
    十、哈夫曼编码
    九、二叉树
    八、高级排序
    七、递归
  • 原文地址:https://www.cnblogs.com/fusiwei/p/13824420.html
Copyright © 2011-2022 走看看