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)