*hiho 1475-array split, DP, reduced from N^2 to Nlogn

Source: Internet
Author: User

Topic links

Describe

Little Ho got an array as a gift for his new year, and he liked this array very much!

After a few days of careful study, small Ho succeeded in splitting the array into segments, and each paragraph was not 0!

Now little Ho would like to know how many of these splitting methods are there?

The two split methods are treated differently when and only if the array is made up of all the locations where the arrays are broken.

Input

The first behavior of each set of inputs is a positive integer n, which indicates the length of the array

The second behaves n integers a1~an, describing the array that little Ho received

For 40% of data, meet 1<=n<=10

For 100% of data, meet 1<=n<=105, | ai|<=100

Output

For each set of inputs, the output is one line of ans, which represents the remainder of the split scheme divided by (109+7).

-----------------------------------------------------------------------------------------------------------

It is easy to think of a n^2 solution:

F[i] = Sum (F[0<=j<i]) + (pre[i]!=0), Pre[j]!=pre[i]. Where Pre[i] is prefixed and.

Because N is 10w, it must not be over.

We can first not judge whether Pre[j] is equal to pre[i], that is, to maintain an accumulative sum, and then subtract the sum of the prefix and equals pre[i] and: f[i] = The sum of the current summation and-pre[i].

Pre[i] accumulation and maintenance with map, is the complexity of LOGN.

So it's n^2 down to Nlogn.

#include <map>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;Const intMOD = 1e9+7;Const intN =100100;intSum[n];Long LongDp[n];intMain () {Long LongPre =0; Map<Long,Long>Records; intN cin>>n; for(intI=0; i<n;i++) {scanf ("%d", sum+i);if(i) sum[i]+=sum[i-1]; Dp[i]= Sum[i]? ((pre+1)%MOD):p re; Auto ITER=Records.find (Sum[i]); if(Iter==records.end ()) records[sum[i]]=Dp[i]; Else{Dp[i]= (dp[i]-iter->second+mod)%MOD; ITER->second = (Iter->second+dp[i])%MOD; } Pre= (Pre+dp[i])%MOD; } printf ("%lld\n", dp[n-1]); return 0;}

*hiho 1475-array split, DP, reduced from N^2 to Nlogn

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.