! Codeforces 558C AMR and Chemistry-yy problem-(bit arithmetic related)

Source: Internet
Author: User

Test instructions: There are n number, each operation can only be divided by 2 or multiplied by 2, the number of N to convert to the same number of the minimum required operation steps

Analysis:

At first glance the topic, feel is very difficult, for this each time has two kind of situation to arrive at the end of the Balabala I feel very complex, this problem explanation actually not terrible, at least part is not terrible.

The practice of this problem is violence to cite each number can go to all the number, record the number of steps, and finally find the intersection output minimum value. Find the intersection also do not want to complicate, this n number can reach the number is the intersection point, then only need to use an array record to reach the point of the number of points, the last starting point number equals n is the intersection, which at each starting point enumeration at the time can be maintained. Cnt[i] maintains the number of start points of the point I, Vis[i] maintains all the beginnings of the steps and the step I.

Say that the operation of the correlation is just the number of binary with the left shift to the right to see this intuitive, in fact, do not use this also line. The maximum intersection is the maximum input MX, because if you want to go to a point greater than MX, the point of the MX must go through to the minimum number of steps in contradiction.

So the problem-solving process is: Scan each starting point, move right to the MX, then go back to the starting point to move to the left, check whether the point is odd or even, if it is even, continue to the left, and if it is odd, move left then right to MX, then move left. That is, in the left-hand process as long as encountered a node is odd, it is necessary to fork open a road, let this odd number first except 2 and then constantly multiplied by 2, but the original left shift route has been unchanged, just a few more routes, if not understand the painting will understand.

There are also some small trick in this question:

1.cnt[a[i]] can not be preprocessed to 1, because there may be multiple starting point is the same as: A[i]=a[j], then cnt[a[i] "should not be 1 but 2, this error is accidental discovery;

2. Array cnt[] and vis[] to open 2*10^5, although we launch to the end of the limit is the maximum value of the input mx,mx<10^5, but because in the calculation process may be counted more than the number of MX (although this does not contribute to the answer), so the array only open 10^5, it is small, So it's WA.

Code:

#include <iostream> #include <cstring> #define MAX (A, b) A>b?a:b#define min (A, b) a<b?a:b#define INF 1000000007using namespace Std;int n,a[1000010];int vis[1000010],cnt[1000010];int mi,mx;int Main () {Cin>>n;mx=-1 , Mi=inf;memset (vis,0,sizeof (Vis)), memset (cnt,0,sizeof (CNT)), for (int i=0;i<n;i++) {Cin>>a[i];mx=max (mx,a [i]);} for (int i=0;i<n;i++) cnt[a[i]]=1;  Error, cannot initialize for (int i=0;i<n;i++) {cnt[a[i]]++;int tmp=a[i];int tot=0;while (tmp<=mx) {tmp*=2;tot++;cnt[tmp]++; Vis[tmp]+=tot;} Tmp=a[i];tot=0;while (tmp>1) {if ((tmp%2==1) && (tmp!=1)) {int tmp2=tmp/2;int step=tot+1;while (TMP2<=MX) {tmp2*=2;step++;cnt[tmp2]++;vis[tmp2]+=step;}} Tmp/=2;tot++;cnt[tmp]++;vis[tmp]+=tot;}} for (int i=1;i<=mx;i++) if (cnt[i]==n) mi=min (Mi,vis[i]); Cout<<mi<<endl;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

! Codeforces 558C AMR and Chemistry-yy problem-(bit operation related)

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.