"ZOJ 5689" Deque and Balls (normal DP)

Source: Internet
Author: User

Test Instructions: give a sequence, in order one by one into the double-ended queue (can be placed in the head can also be placed in the tail), a queue of beautiful index is the number of a[i]>a[i+1 in the series , to seek the expectation of beauty index *2^n the value.

problem-solving ideas: for convenience, we refer to a[i]>a[i+1] as the case of D .

By test instructions can know that the last obtained sequence has a total of 2^ (n-1) , set the sum of the number of D in all sequences of the ans= , the last is to ask sum/2^ (n-1) *2^n = 2*sum

For a[j " sum= original case sum *2+a[j] The resulting d -(A[i]=a[j]) The case of

If a[j] is adjacent to A[i] , then a[i+1 ... J-1] can only be placed in the A[i] the opposite end, then appears D the sequence of the conditions altogether has 2^ (i-2) a

So the D case of A[j] has altogether 1+∑2^ (i-2) (2<=I<J)

A[I]=A[J] The situation is similar to the idea of J .

1#include <bits/stdc++.h>2 using namespacestd;3 #definell Long Long4 Const intn=1e5+Ten;5 Constll mod=1e9+7;6 intA;7 ll F[n], Sum[n], dp[n], p[n];8 intMain () {9f[0]=1, f[1]=1, sum[0]=1, sum[1]=2;Ten      for(intI=2; i<n; i++) F[i] = (f[i-1]*2)%mod, sum[i] = (sum[i-1]+f[i])%MoD; One      A     intt, N; -scanf"%d", &t); -      while(t--){ theMemset (P,0,sizeof(P)); -scanf"%d", &n); -dp[0] =0, dp[1] =0; -          for(intI=1; i<=n; i++){ +scanf"%d", &a); -             if(i>1) Dp[i] = ((dp[i-1]*2)%mod + sum[i-2]-p[a] + MoD)%MoD; +P[a] = (p[a]+f[i-1])%MoD; A         } atprintf"%lld\n", (dp[n]*2)%MoD); -     } -     return 0; -}

"ZOJ 5689" Deque and Balls (normal DP)

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.