首页 > 其他 > 详细

codeforces 558/C Amr and Chemistry

时间:2016-08-12 16:44:26      阅读:200      评论:0      收藏:0      [点我收藏+]

题目链接:http://codeforces.com/problemset/problem/558/C

 

题意:把n个数变成相同所需要走的最小的步数
易得到结论,两个奇数不同,一直×2不可能有重叠
枚举每个数可能到得所有值,以及统计达到该值的时候已经走的步数
最终答案就是1到up中num[i]最小的数

 

Examples
input
3
4 8 2
output
2

input 3 3 5 6 output 5

Note In the first sample test, the optimal solution is to divide the second chemical volume by two, and multiply the third chemical volume by two to make all the volumes equal 4. In the second sample test, the optimal solution is to divide the first chemical volume by two, and divide the second and the third chemical volumes by two twice to make all the volumes equal 1.

 

***********************************************************

 

AC代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<iostream>
 6 #include<queue>
 7 #include<map>
 8 #include<stdlib.h>
 9 
10 using namespace std;
11 
12 #define N 5200
13 #define INF 0x3f3f3f3f
14 #define Maxn 100010
15 
16 int a[Maxn],step[Maxn],num[Maxn];
17 
18 int main()
19 {
20     int n,i;
21 
22     while(scanf("%d", &n) != EOF)
23     {
24         memset(step,0,sizeof(step));
25         memset(num,0,sizeof(num));
26 
27         for(i=1; i<=n; i++)
28             scanf("%d", &a[i]);
29 
30         for(i=1; i<=n; i++)
31         {
32             int x=a[i],sc=0;
33 
34             while(x)
35             {
36                 int s1=0;
37                 while(x%2==0)
38                 {
39                     x/=2;
40                     s1++;
41                 }
42 
43                 int y=x,s2=0;
44                 while(y<=Maxn)
45                 {
46                     num[y]++;
47                     step[y]+=sc+abs(s1-s2);
48                     s2++;
49                     y*=2;
50                 }
51 
52                 sc+=s1+1;
53                 x/=2;
54             }
55         }
56         int ans=INF;
57         for(i=1;i<=Maxn;i++)
58             if(num[i]==n)
59             ans=min(ans,step[i]);
60 
61         printf("%d\n", ans);
62     }
63     return 0;
64 }

 

codeforces 558/C Amr and Chemistry

原文:http://www.cnblogs.com/weiyuan/p/5765368.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!