Hdu 5542 The Battle of Chibi (2015ccpc-c question)

Source: Internet
Author: User

Title Link: Hdu 5542

The first ccpc of the C, the game together for a long time, the last teammate a out, at that time have tried to use a tree array to optimize DP, and then this afternoon with a tree-like array of the afternoon, the results still stepped on and the same pit: I always used to record the state of the DP array and the tree array of built-in arrays to , and the order of the two loops is reversed so that both sets of data

3 2 3 2

1 2 3 and 3 2 1

The program ran all the same results, no words ... Before doing DP and line tree combination of the problem is also innocently confused, in the final analysis is DP's skill is not enough, so in the DP session in this game in addition to water problem I almost nothing to contribute, very heart stuffed depressed for a period of time

Topic Idea: Establish a classic DP model:F[K][I] Indicates the number of ascending sub-sequences (i.e. DP states) with a length of K, ending with b[i], so the recurrence equation is f[k][i] = sum{f[k-1][x], 1 <= b[x] < = B[i]-1}, you can see that K relies only on k-1, and I relies on b[i] smaller than b[i] (the same as the classic inverse principle), so you can use a tree-like array to quickly find out, the reason is written f[k][i] instead of f[i][k] is to facilitate the tree-like array Operation (In fact, the transfer is completely possible, but the brain can not turn a moment, the tree-like array has been written too dead, always template Dafa-_-| | The implementation of the program is also filled with a variety of skills and so on.

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 #defineLowbit (x) ((x) &-(x))6typedefLong Longll;7 Const intN =1003;8 Constll mod =1000000007;9 Ten intMAXN; Onell C[n][n];//c Arrays and MAXN are the data structures required for a tree array All F[n][n];//F[k][i] Indicates the number of ascending subsequence (that is, the state of DP) with a length of K, ending with B[i] -  -ll sum (intLenintx) {//two functions of a tree-like array thell res =0; -      while(x) { -Res + = C[len][x];//c[][] The len corresponding to f[][] k,x corresponds to f[][] b[i] -         if(res >= mod) Res-=MoD; +X-=lowbit (x); -     } +     returnRes; A } at  - voidAddintLenintX, ll v) { -      while(x <=MAXN) { -C[LEN][X] + =v; -         if(C[len][x] >= MoD) c[len][x]-=MoD; -X + =lowbit (x); in     } - } to  + intB[n], a[n]; -  the intMain () { *     intT,n,m,case =0; $scanf"%d",&t);Panax Notoginseng      while(t--) { -scanf"%d%d",&n,&m); the          for(inti =1; I <= N; ++i) { +scanf"%d", B +i); AA[i] =B[i]; the         } +Sort (A +1, A + n +1);//discretization of -          for(inti =1; I <= N; ++i) $B[i] = Lower_bound (A +1, A + n +1, B[i])-A; $  -MAXN = n +1; -Memset (c,0,sizeofc); theMemset (F,0,sizeoff);//remember to clear 0 . - Wuyill ans =0; the         //the order of the double loops cannot be mistaken!  -         //Each time for each b[i], is already calculated the length 1~k the number of ascending sub-sequence, that is, the value of f[1~k][i]; Wu         //and then to B[i + 1]; so it is to enumerate the 1~n after the discretization of each element, and then enumerate the length (1~min (i,m)) -          for(inti =1; I <= N; ++i) { About             inttop =min (i,m); $              for(intK =1; K <= top; ++k) { -                 if(k = =1) F[k][i] =1; -                 Else { -F[k][i] + = SUM (K-1, B[i]-1);//F[k][i] = sum{f[k-1][x], 1 <= b[x] <= b[i]-1} A                     if(F[k][i] >= MoD) f[k][i]-=MoD; +                 } theAdd (k, b[i], f[k][i]);//update the calculated f[k][i] into the tree array -             } $Ans + = f[m][i];//this time f[m][i] has been counted, so add to the answer the             if(ans >= mod) ans-=MoD; the         } theprintf"Case #%d:%i64d\n", ++Case , ans); the     } -     return 0; in}

DP Array record state, and then update into the corresponding data structure, and Hdu 4521 Xiao Ming series Problems-Xiao Ming sequence has similarities, must have a good understanding of the DP and data structure of the combination of this idea and processing.

Hdu 5542 The Battle of Chibi (2015ccpc-c question)

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.