"Tree-like array +dp" HDU 5542 the Battle of Chibi

Source: Internet
Author: User
Tags cas

http://acm.hdu.edu.cn/showproblem.php?pid=5542

Test instructions

    • Given a sequence of length n, how many strict ascending sub-sequences of M are asked?

Ideas

    • DP[I][J] Indicates how many strict ascending sub-sequences with a length of J ending with A[i]
    • Dp[i][j]=sum{dp[k][j-1]},k less than I and A[k]<a[i]
    • Interval DP, complexity O (n^3)
    • Is timed out, and the third loop sum is accelerated with a tree-like array
    • Time Complexity of O (N^2LOGN)
    • Discretization of the time after sorting does not go heavy (otherwise WA)

"Accepted"

#include <iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<cmath>using namespaceStd;typedefLong Longll;Constll mod=1e9+7;Const intmaxn=1e3+3; ll A[maxn];ll B[maxn];ll DP[MAXN][MAXN];intCNT;intn,m;intLowbit (intx) {    returnx& (-x);} ll Add (intXintY,ll D) {     while(x<=N) {Dp[x][y]= (dp[x][y]+d)%MoD; X+=lowbit (x); }}ll sum (intXinty) {LL res=0LL;  while(x) {res= (Res+dp[x][y])%MoD; X-=lowbit (x); }    returnRes;}intMain () {intT; scanf ("%d",&u); intcas=0;  while(t--) {memset (DP,0,sizeof(DP)); scanf ("%d%d",&n,&m);  for(intI=1; i<=n;i++) {scanf ("%lld",&A[i]); } memcpy (B,a,sizeof(b)); Sort (a+1, a+n+1); //Cnt=unique (a+1,a+n+1)-a-1;         for(intI=1; i<=n;i++)        {            intPos=lower_bound (A +1, a+n+1, B[i])-A;  for(intj=1; J<=min (i+1, m); J + +) {ll tmp; if(j==1) tmp=1ll; ElseTmp=sum (pos-1, J-1);            Add (pos,j,tmp); }} ll ans=sum (n,m); printf ("Case #%d:%d\n", ++cas, (int) ans); }    return 0;}
View Code

Questions

Why can't we go heavy?

"Tree-like array +dp" HDU 5542 the Battle of Chibi

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.