Codeforces 558C AMR and Chemistry

Source: Internet
Author: User

Test instructions

n number, each time you can choose a number to make it *=2 or/=2

Ask at least how many times the operation makes all the numbers equal.

Ideas:

For each number, calculate the number that the number can be converted to, and the smallest number of steps to change to, save with two arrays

Cnt[i] indicates that the number of CNT in a sequence can be changed to I

Step[i] The number of those that can become I is the cost of I and how much.

Which, when encountered odd times to special treatment:

For example, 7 can be 6 by/2 and then * *, which means that any odd I (excluding 1) can be changed to i-1 by 2 operations;

The code is as follows:

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace Std;typedef Long Long ll;const int N = 1e5+10;const int INF = 1e9 + 10;int n;int a[n], cnt[n];//number ll step[n];//total number of steps void u p (int x, int now) {while (X < N) {Cnt[x]++;step[x] + = now;now++;x <<= 1;}} void Go (int u) {up (U, 0), Int. now = 1;while (u) {if ((u>1) && (u&1)) {u >>= 1;up (U, now);} Else{u >>= 1;cnt[u]++;step[u] + = Now;} now++;} }int Main () {while (~SCANF ("%d", &n)) {memset (CNT, 0, sizeof (CNT)), memset (step, 0, sizeof (step)), for (int i = 1; I <= n ; i++) {scanf ("%d", &a[i]); Go (A[i]);} ll ans = step[1];for (int i = 2; i < N; i++) {if (cnt[i] = = N) ans = min (ans, step[i]);} printf ("%i64d\n", ans);} return 0;}



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

Codeforces 558C AMR and Chemistry

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.