暴力 + 贪心 --- Codeforces 558C : Amr and Chemistry
C. Amr and Chemistry
Problem's Link: http://codeforces.com/problemset/problem/558/C
Mean:
给出n个数,让你通过下面两种操作,把它们转换为同一个数。求最少的操作数。
1.ai = ai*2
2.ai = ai/2 (向下取整)
analyse:
基本思路:首先枚举出每个数能够到达的数字并且记录下到达该数组需要的步数,然后从到达次数为n次的数字中选择步数最小的即为答案。
对于一个数字Ai,它可以变换得到的数字可以分为三类:
1.向左移动n位得到的数字;
2.向右移动n位得到的数字;
3.奇数/2后得到的偶数向右移动n位得到的数字。
处理一个数 Ai:
1、对 Ai 执行左移操作,记录 Ai 通过左移能够得到的数字,上限为1e5,vis 数组加1,cnt数组记录步数
2、对 Ai 执行右移操作,直到 Ai 为0.
若 Ai 的最低位为1,右移一步之后,进行左移,上限为1e5,维持vis数组和cnt数组.
若 Ai 的最低位为0,右移一步,维持vis数组和cnt数组.
这样我们就把一个数的所有情况都处理出来了,并且是最少的操作数.
最后遍历数组找 vis[i] 为n,并且操作数最小。
Time complexity: O(n*m*log(MAX))
Source code:
/*
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-07-15-18.53
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define LL long long
#define ULL unsigned long long
using namespace std; const int MAXN(+int(1e5));
int arrive_step[MAXN<<];
int arrive_num[MAXN<<];
int a[MAXN],n; void init()
{
for(int i=; i<MAXN<<; ++i) { arrive_step[i]=,arrive_num[i]=; }
} inline void count_arrive(int &x)
{
int t=x,step=;
arrive_num[t]++;
while(t<=int(1e5))
{
t<<=;
step++;
arrive_step[t]+=step;
arrive_num[t]++;
}
t=x,step=;
while(t>)
{
if(t!= && (t&))
{
int sta=t>>;
int sstep=step+;
while(sta<=int(1e5))
{
sta<<=;
sstep++;
arrive_step[sta]+=sstep;
arrive_num[sta]++;
}
}
t>>=;
step++;
arrive_step[t]+=step;
arrive_num[t]++;
}
} int main()
{
ios_base::sync_with_stdio(false);
cin.tie();
while(~scanf("%d",&n))
{
init();
for(int i=; i<n; ++i)
{
scanf("%d",&a[i]);
count_arrive(a[i]);
}
int ans=INT_MAX;
for(int i=; i<(MAXN<<); ++i)
{
if(arrive_num[i]==n)
ans=ans<arrive_step[i]?ans:arrive_step[i];
}
printf("%d\n",ans);
}
return ;
}
/* */
暴力 + 贪心 --- Codeforces 558C : Amr and Chemistry的更多相关文章
- Codeforces 558C Amr and Chemistry 暴力 - -
点击打开链接 Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input stan ...
- CodeForces 558C Amr and Chemistry (位运算,数论,规律,枚举)
Codeforces 558C 题意:给n个数字,对每一个数字能够进行两种操作:num*2与num/2(向下取整),求:让n个数相等最少须要操作多少次. 分析: 计算每一个数的二进制公共前缀. 枚举法 ...
- Codeforces 558C Amr and Chemistry 全都变相等
题意:给定一个数列,每次操作仅仅能将某个数乘以2或者除以2(向下取整). 求最小的操作次数使得全部的数都变为同样值. 比赛的时候最后没实现.唉.之后才A掉.開始一直在想二分次数,可是半天想不出怎 ...
- Codeforces 558C Amr and Chemistry
题意: n个数.每次能够选一个数 让其 *=2 或者 /=2 问至少操作多少次使得全部数相等. 思路: 对于每一个数,计算出这个数能够变成哪些数,以及变成那个数的最小步数,用两个数组保存 cnt[i] ...
- 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 558C C. Amr and Chemistry(bfs)
题目链接: C. Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input st ...
- 【23.39%】【codeforces 558C】Amr and Chemistry
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- C. Amr and Chemistry(Codeforces Round #312 (Div. 2) 二进制+暴力)
C. Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...
- CF 558 C. Amr and Chemistry 暴力+二进制
链接:http://codeforces.com/problemset/problem/558/C C. Amr and Chemistry time limit per test 1 second ...
随机推荐
- 删除数据报ORA-00600: internal error code, arguments: [ktbesc_plugged]
Oracle在删除数据是以下错误: ORA-00600: internal error code, arguments: [ktbesc_plugged], [], [], [], [], [], [ ...
- dapper 操作类封装
using System; using System.Collections.Generic; using System.Data; using System.Data.SQLite; using S ...
- ch5 MySQL 备份与恢复
第 5 章 MySQL 备份与恢复 前言 数据库的备份与恢复一直都是 DBA 工作中最为重要的部分之一,也是基本工作之一.任何正式环境的数据库都必须有完整的备份计划和恢复测试,本章内容将主要介绍 My ...
- Rails: No such file or directory - getcwd
这个的意思就是你从一个删除的目录里面运行实例:rails s
- ffrpc相关文章列表
ffrpc 是异步c++通信库.可以说是传统rpc模式和zeromq模式的一个结合,采用broker模式封装client和server之间的拓扑关系,而client和server的通信仍然按照请求应答 ...
- dWebBrowser常用知识点
1.webbrowser调用的就是本机IE,并且webbrowser默认就是运行在IE7 mode下,除非你改变它. 2.不装IE,无法用webbrowser. 3.设置WebBrowser在IE9 ...
- 关于MySQL redo log,挖些坑,慢慢填
1. 为什么可以设置为多个redo log ? (innodb_log_files_in_group,默认值和推荐值都是2,我们线上设的统一为4): 2. 什么条件下会触发刷脏?除了master_th ...
- IOS Application Security Testing Cheat Sheet
IOS Application Security Testing Cheat Sheet [hide] 1 DRAFT CHEAT SHEET - WORK IN PROGRESS 2 Int ...
- wamp2.5 不能运行在win2003的解决方法
安装时提示 httpd.exe 不是有效的 win32程序 之后就启动不了,连小icon都不显示了 经查发现 wampserver 2.5用 vc11编译,并使用了他的类库 vc11是不支持 xp和 ...
- 数轴上从左到右有n个点a[0],a[1]…,a[n-1],给定一根长度为L的绳子,求绳子最多能覆盖其中的几个点。要求算法复杂度为o(n)。
#include <iostream> using namespace std; int maxCover(int* a, int n, int l) { ; ; ; while(end ...