DP Classic Bzoj 1584: [Usaco2009 mar]cleaning up cleaning

Source: Internet
Author: User

Bzoj 1584: [Usaco2009 mar]cleaning up cleaning time limit:10 Sec Memory limit:64 MB
submit:419 solved:278
Description

There are N cows, each of which has a label pi,1 <= Pi <= M <= N <= 40000. Now farmer John is going to divide these cows into segments, defining the non-crab degree for each paragraph: if there are k different numbers in this paragraph, then the degree of the river Crab is k*k. The total non-crab degree is the sum of all sections of the non-crab degree.

Input

First line: two integers n,m

2nd.. N+1 line: n integers represent the number of each cow

Output

An integer representing the minimum non-crab degree

Sample Input13 4
1
2
1
3
2
2
3
4
3
4
3
1
4
Sample Output One
1 /*because this topic grouping is unlimited, so we can not put the DP equation as a state2 Positive Solution: The worst case of each digit in a paragraph, ans=n, so if there is a range of different number of <=SQRT (n), otherwise the result must not be optimal. 3 nsqrt (N) Method: Maintain B[j],c[j],f[j],pre[j] array. 4 B[j] Indicates that the B[J]+1...I has the leftmost end of the interval of a different number of J. 5 so you know f[i]=min{f[i],f[b[j]]+j*j}; So the complexity of time falls down.6 how to maintain the B[j] array, when I move backward one bit, pre[a[i]] record A[i] where is the last position to appear? 7 well: After i++, pre[a[i]]<=b[j], description b[j]+1 ... to the number of different numbers of this sequence is j+1, we use c[j] to record the situation, by updating pre[a[i]], and always maintain c[j]==j;8 then B[j] is still in line with test instructions. 9 Maintenance C[j] from the beginning of the b[j]+1 to delete data, delete when judged if pre[a[t]]>t, it is deleted the same number, for the final harmony value has no effect, so also to delete the numberTen know pre[a[t]]<=t, delete a[t], by the way update B[j] location One */ A #defineN 40100 -#include <iostream> - using namespacestd; the#include <cstdio> -#include <cmath> -#include <cstring> - intF[n],b[n],c[n],pre[n],a[n]; + intn,m; - voidinput () + { Ascanf"%d%d",&n,&m); at      for(intI=1; i<=n;++i) -scanf"%d",&a[i]); -memset (pre,-1,sizeof(pre));/*do not forget to set to-1, because the following will be compared with the initial value of B[j]==0*/ -Memset (F,127,sizeof(f)); - } - voidChuli () in { -     intSQRTN=SQRT (n+0.5); tof[0]=0;/*initialization, the first 0 number of disharmony value is 0,*/ +      for(intI=1; i<=n;++i) -     { the          for(intj=1; j<=sqrtn;++j) *         { $             if(pre[a[i]]<=B[j])Panax Notoginsengc[j]++;/*statistics on the number of newly added are not in line with the requirements*/ -         } thePre[a[i]]=i;/*Update Pre*/ +          for(intj=1; j<=sqrtn;++j) A         { the             if(C[J]&GT;J)/*Delete number, shorten sequence*/ +             { -                 intt=b[j]+1; $                  while(pre[a[t]]>t) + +T; $b[j]=t;c[j]--; -             } -         } the          for(intj=1; j<=sqrtn;++j) -F[i]=min (F[I],F[B[J]]+J*J);/*Update F*/Wuyi     } the } - intMain () Wu { - input (); About Chuli (); $cout<<f[n]<<Endl; -     return 0; - } - /*since this topic is not divided into the number of States, then you can consider, the length of the dividing sequence*/

DP Classic Bzoj 1584: [Usaco2009 mar]cleaning up cleaning

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.