P1569 [usaco 11feb] native protest Generic Cow Prote ..., P1569prote
Description
Farmer John's N (1 <= N <= 100,000) cows are lined up in a row and numbered 1 .. n. the cows are conducting cting 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 wowould 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.
The native cows of John's family gathered in a column and are conducting a protest. The intelligence of the I-th dairy cow is Ai, and Ai may be negative. John wanted his cows to be rational during the protest. To this end, he planned to isolate all the cows into several groups, and the total mental power of the cows in each group would be greater than zero. Because the cows are arranged in a straight line, the positions of the cows in a group must be continuous. Please help John calculate the number of groups.
Input/Output Format
Input Format:
The first row contains 1 number N, which indicates the number of cows.
From 2nd to N + 1, each line has one integer Ai.
Output Format:
The output file has only one row and contains one positive integer, that is, the maximum number of groups.
If the group conditions cannot be met, Impossible is output.
Input and Output sample input sample #1:
423-31
Output sample #1:
3
Description
[Data scale and Conventions]
30% of the Data satisfies N ≤ 20.
100% of the data is N ≤ 1000, | Ai | ≤ 100000.
At first, I thought of using the prefix and maintenance. However, I am still not confident ,,
One clever thing used in the question solution is
If (dp [j]> 0 & sum [I]-sum [j]> = 0)
It means that the two of them can not be in one group.
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<queue> 6 using namespace std; 7 void read(int &n) 8 { 9 char c='+';int x=0;bool flag=0;10 while(c<'0'||c>'9')11 {c=getchar();if(c=='-')flag=1;}12 while(c>='0'&&c<='9')13 {x=x*10+(c-48);c=getchar();}14 flag==1?n=-x:n=x;15 }16 int n,m;17 int a[10001];18 int dp[10001];19 int sum[10001];20 int main()21 {22 int i,j,k;23 read(n);24 for(int i=1;i<=n;i++)25 {26 read(a[i]);27 sum[i]=sum[i-1]+a[i];28 if(sum[i]>=0)29 dp[i]=1;30 } 31 for(int i=1;i<=n;i++)32 for(int j=1;j<i;j++)33 if(dp[j]>0&&sum[i]-sum[j]>=0)34 dp[i]=max(dp[i],dp[j]+1);35 dp[n]==0?printf("Impossible"):printf("%d",dp[n]); 36 return 0;37 }