"SCOI2014" "BZOJ3598" Uncle's shopping trip (digital DP)

Source: Internet
Author: User

Portal: http://www.lydsy.com/JudgeOnline/problem.php?id=3598

Test instructions

For a number x, it contains a few pebbles, each stone has a value of a[i] (A[i] is x in the K-binary under the I-bit), select a stone position pos make sum (a[i] * ABS (I-POS)) the smallest. Find the value of all the numbers in [L,r].

Exercises

For a number, when we enumerate the optimal position, we can find the change from i->i+1 to +pre[i]-suf[i+1] (prefix and suffix respectively).

30% of violent points, we enumerate the numbers in [L,r], and then do the above.

What about 100% cents? We consider setting the position of each number to 1, the answer is a[i]* (i-1), and the digital DP will calculate the initial cost. For the change of position i->i+1, + number of previous I-i+1 number. We consider enumerating this operation and finding out how much better it will be for these numbers after this operation.

Above those who do not understand the words to see here _ (: З"∠) _.

1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <iostream>5 #defineLL Long Long6 using namespacestd;7 Const intMAXN = $;8 intU[MAXN], D[MAXN], LEN;9 LL L, R, K;TenLL f[maxn][2][2], g[maxn][ the], ans[2333], sum[maxn][2][2]; One  ALL DP (intLenintUpintDown ) { -     if(!len) {Sum[len][up][down] =1;return 0; } -     if(F[len][up][down]!=-1)returnF[len][up][down]; theLL ret = 0LL, num =0LL; -     inttop = up? U[len]: (int) K-1, bot = down? D[len]:0; -      for(inti = bot; I <= top; i++){ -         intP1 = (i==top) &&up, p2 = i==bot&&Down ; +RET + = DP (len-1, p1, p2) + i* (len-1) *sum[len-1][P1][P2]; -num + = sum[len-1][P1][P2]; +     } AF[len][up][down] = ret, sum[len][up][down] =num; at     returnret; - } -  - ll Calc (ll X, ll y) { -     intLen =0, Len2 =0; -      while(x) { inU[++len] = (int) (%k); -X/=K; to     } +      while(y) { -D[++LEN2] = (int) (y%k); theY/=K; *     } $LEN =Len;Panax Notoginseng     returnDP (LEN,1,1); - } the  +LL DFS (intLenintUpintDownintPints) { A     if(!len)return(LL) s; the     if(!up &&!down && g[len][s]! =-1)returnG[len][s]; +LL ret =0; -     inttop = up? U[len]:(int) K-1, bot = down? D[len]:0; $      for(inti = bot; I <= top; i++){ $         intx = (len >= p?)1: -1), C = i==top&&up, d = i==bot&&Down ; -x = X*i +s; -         if(X <0) Break; theRET + = DFS (len-1, C, D, p, x); -     }Wuyi     if(!up &&!down) G[len][s] =ret; the     returnret; - } Wu  - voidSolve () { AboutMemset (f, -1ll,sizeof(f)); $LL ret =Calc (R, l); -     //cout<<ret<<endl; -LL Dec =0LL; -      for(inti =2; I <= LEN; i++){ AMemset (g, -1LL,sizeof(g)), Dec + = DFS (LEN,1,1I0); +     } theCout<<ret-Dec; - } $  the intMain () { theCin>>l>>r>>K; the Solve (); the     return 0; -}
View Code

"SCOI2014" "BZOJ3598" Uncle's shopping trip (digital 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.