HDU 5798 Stabilization

Source: Internet
Author: User

Topic Description:

Given a sequence of positive integers (sequence length $n \le 10^5$) Number size $ < 2 ^ {20}$

Now select a positive integer number $x$ with all the numbers in the original sequence or

Then ask the number of the sum of the absolute values of the adjacent two-digit difference of the sequence after the operation

What is the smallest $x$ that satisfies the minimum of this value?

---------------------------------------------------------------------------------------

When the game took a teammate to knock the $1h+$ and then wrote it off.

After the game to see the official solution to a face, but it is based on the first sentence of the idea of the thinking of their own thinking of one and the solution to achieve a different approach

We know that there is no change in the number of digits before or after the difference in the number of bits before the highest bit of the next two numbers or the relative size of the two number.

Thus can be used $delta[i][j], J \le I $ means the $i$ bit for the highest difference only the number of $j$ bit XOR or after the sum of the change

(In other words, the $n-1$ logarithm to $20$ logarithm to calculate the contribution)

If the $i$ bit changed $ ($ XOR or a $ $) $ then the $j$ bit contribution value takes the opposite number

The next intuitive approach is to enumerate $x$ and figure out the answer for each $x$.

But sweep once $delta$ array is $20^2$ again multiply on $2^{20}$ single $case$ can't $1s$ run finish

Then consider using the memory to reuse some of the results.

We found that if we were to consider the number of the most different bits in the first $i$ bit,

So two different $x$, as long as the lowest $i$ bit is the same, they are equivalent at this point, and many calculations are repetitive.

So we can open an array of $ans[i][x]$ to store the corresponding results and then transfer to do some preprocessing can also be done $o (1) $

This complexity is $O ($ enum $x * $ enum highest different bit $i *$ after averaging once the memory search $*$ Data Group Count $t$)

$= 2^{20} * 2 * Ten ^ 8$ $3s$ time limit is sufficient

But follow the idea. Ans Array to open $2 ^ {20}$ space to explode so you can change the order of the enumeration $ ($ round $) $ to eliminate $20$ that dimension

So we can get through this problem.

1#include <cstdio>2#include <cstring>3#include <cmath>4#include <algorithm>5 using namespacestd;6 Const intM = -, N =1<<M;7 Long LongDelta[m][m], ans[n], tans[n];8 intUsed[n];9 intMask[m], nexte[n];Ten intans1; One Long Longans2, sum; A intt, N, CA; - Long LongDfsintBintInvintx) - { the     if(Used[x] = =CA) -         returnANS[X] *INV; -USED[X] =CA; -ANS[X] =0; +     if(!x) -         return 0; +     inti =Nexte[x]; AANS[X] = Delta[b][i] + DFS (b,1, x ^ (1<<i)); at     returnANS[X] *INV; - } - intMain () - { -mask[0] =1; -      for(inti =1; i < M; ++i) inMask[i] = mask[i-1] *2+1; -      for(inti =2; i < N; ++i) toNexte[i] = (i = = (I & i))? Nexte[i-1] +1: Nexte[i-1]; +scanf"%d", &t); -      while(t--) the     { *memset (Delta,0,sizeofdelta); $sum =0;Panax Notoginseng         intx, y, Top, L, R; -scanf"%d", &n); thescanf"%d", &x); +          for(inti =2; I <= N; ++i) A         { thescanf"%d", &y); +             if(Y = =x) -                 Continue; $top = +; $              while((x ^ y ^ (1<< top) > (x ^y)) ---top; -L = min (x, y), R =Max (x, y); theSum + = R-L; -Delta[top][top] + = (L ^ (1<< top))-(R ^ (1<< top))-(R-L);Wuyi              for(intj = Top-1; J >=0; --j) the                 if((x ^ y) & (1<<j)) -DELTA[TOP][J] + = (R ^ (1<< j))-(L ^ (1<< j))-(R-L); Wux =y; -         } About         intM2 =M; $          while(M2 >0&&!delta[m2-1][M2-1]) ---M2; -         intN2 =1<<M2; -Ans2 = (Long Long) N2 *N; A         intINV; +          for(inti =0; i < N2; ++i) the         { -INV = i &1; $Tans[i] = delta[0][0] *INV; the         } the          for(intj =1; J < M2; ++j) the         { the++CA; -              for(inti =0; i < N2; ++i) in             { theINV = (I & (1<< j))! =0; theTans[i] + = delta[j][j] * INV + DFS (J, (INV-1:1), I & Mask[j-1]); About             } the         } the          for(inti =0; i < N2; ++i) the             if(Tans[i] <ans2) +             { -Ans2 =Tans[i]; theANS1 =i;Bayi             } theAns2 + =sum; theprintf"%d%lld\n", ans1, ans2); -     } -     return 0; the}

HDU 5798 Stabilization

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.