codeforces 558/C Amr and Chemistry(数论+位运算)
题目链接:http://codeforces.com/problemset/problem/558/C
题意:把n个数变成相同所需要走的最小的步数
易得到结论,两个奇数不同,一直×2不可能有重叠
枚举每个数可能到得所有值,以及统计达到该值的时候已经走的步数
最终答案就是1到up中num[i]最小的数
Examples
input output
2 input 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 . 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 .
***********************************************************
AC代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<stdlib.h> using namespace std; #define N 5200
#define INF 0x3f3f3f3f
#define Maxn 100010 int a[Maxn],step[Maxn],num[Maxn]; int main()
{
int n,i; while(scanf("%d", &n) != EOF)
{
memset(step,,sizeof(step));
memset(num,,sizeof(num)); for(i=; i<=n; i++)
scanf("%d", &a[i]); for(i=; i<=n; i++)
{
int x=a[i],sc=; while(x)
{
int s1=;
while(x%==)
{
x/=;
s1++;
} int y=x,s2=;
while(y<=Maxn)
{
num[y]++;
step[y]+=sc+abs(s1-s2);
s2++;
y*=;
} sc+=s1+;
x/=;
}
}
int ans=INF;
for(i=;i<=Maxn;i++)
if(num[i]==n)
ans=min(ans,step[i]); printf("%d\n", ans);
}
return ;
}
这个里写的很清楚:http://blog.csdn.net/u014028317/article/details/46897963
codeforces 558/C Amr and Chemistry(数论+位运算)的更多相关文章
- CodeForces 558C Amr and Chemistry (位运算,数论,规律,枚举)
Codeforces 558C 题意:给n个数字,对每一个数字能够进行两种操作:num*2与num/2(向下取整),求:让n个数相等最少须要操作多少次. 分析: 计算每一个数的二进制公共前缀. 枚举法 ...
- CF 558 C. Amr and Chemistry 暴力+二进制
链接:http://codeforces.com/problemset/problem/558/C C. Amr and Chemistry time limit per test 1 second ...
- 【23.39%】【codeforces 558C】Amr and Chemistry
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- codeforces 558C C. Amr and Chemistry(bfs)
题目链接: C. Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input st ...
- Codeforces Round #461 (Div. 2)B-Magic Forest+位运算或优雅的暴力
Magic Forest 题意:就是在1 ~ n中找三个值,满足三角形的要求,同时三个数的异或运算还要为0: , where denotes the bitwise xor of integers ...
- 暴力 + 贪心 --- Codeforces 558C : Amr and Chemistry
C. Amr and Chemistry Problem's Link: http://codeforces.com/problemset/problem/558/C Mean: 给出n个数,让你通过 ...
- Codeforces Round #312 (Div. 2) C. Amr and Chemistry 暴力
C. Amr and Chemistry Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/558/ ...
- Codeforces Round #312 (Div. 2) C.Amr and Chemistry
Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experime ...
- Codeforces 558C Amr and Chemistry 暴力 - -
点击打开链接 Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input stan ...
随机推荐
- erlang程序优化点的总结(持续更新)
转自:http://wqtn22.iteye.com/blog/1820587 转载请注明出处 注意,这里只是给出一个总结,具体性能需要根据实际环境和需要来确定 霸爷指出,新的erlang虚拟机有很多 ...
- matlab里的nargin
nargin是用来判断输入变量个数的函数,这样就可以针对不同的情况执行不同的功能.
- webstorm2016.2.4激活码
测试日期:2016.11.07 webstorm版本:2016.2.4 系统环境:MacOS(windows环境应该也行) 激活码如下: 43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0 ...
- 生成SQL脚本的方法
2点需要注意的关键: (1)选择特定数据库对象不包含用户选项: (2)要编写脚本的数据的类型选择"架构和数据".
- postgresql删除属性
PostgreSQL update and delete property from JSONB column up vote 2 down vote favorite From this artic ...
- centos7.2自带的php5.4升级为5.6
参考:http://jingyan.baidu.com/article/380abd0a1f176c1d91192c4b.html 今天在新购的阿里云上部署个phpmyadmin,结果显示了个如下信息 ...
- vb脚本自动更新版本信息
使用的串口显示软件为secureCrt,支持脚本功能,今天写了一个简单的软件升级脚本(VB脚本). 如下: # $language = "VBScript" # $interfac ...
- Vowel Counting
Vowel Counting Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Tota ...
- 认识cookie与session的区别与应用
通常我们所说的浏览器自动保存密码,下次不用登陆,网页换皮肤,用户引导,提示一次就不再出现的内容,大部分通过cookie或者session来实现的,在这次制作用户引导中,本人就用到了cookie的内容, ...
- 网络请求 get post
1.新建一个网络请求工具类,负责整个项目中所有的Http网络请求 提示:同步请求会卡住线程,发送网络请求应该使用异步请求(这意味着类方法不能有返回值) 2.工具类的实现 YYHttpTool.h文件 ...