[topcoder]IncrementAndDoubling
http://community.topcoder.com/stat?c=problem_statement&pm=12790&rd=15708
这道题只有两个操作,一是加一,二是数组所有元素乘以二,求最少操作次数从全0数组变成目标数组。我的方法是从目标数组反推全0数组,如果有奇数就变成偶数,全偶数就除以二。这个方法是可以过的,就是效率拙计:
import java.util.*;
public class IncrementAndDoubling
{
public int getMin(int[] A)
{
int count = 0;
int len = A.length;
while (true)
{
boolean done = true;
for (int i = 0; i < len; i++)
{
if (A[i] % 2 == 1)
{
A[i]--;
count++;
}
if (A[i] != 0)
done = false;
}
if (done) break;
for (int i = 0; i < len; i++)
{
A[i] = A[i] / 2;
}
count++;
}
return count;
}
}
但更好的方法如标程里所介绍,观察到除以2就是移位,0和1的转换。那么最后的结果其实是数组里所有二进制1的个数,加上最长的二进制表示长度。
int getMin(vector<int> desiredArray)
{
int mx = 1; // '0' has length 1
int sum = 0;
for (int x: desiredArray) {
int c = 0;
// extract bits from x:
while (x > 0) {
c++;
sum += x % 2; //the last bit
x /= 2;
}
mx = std::max(mx, c);
// the number c of bits is wrong for '0', but it doesn't matter
// because mx is initially set to 1.
}
return mx - 1 + sum;
}
[topcoder]IncrementAndDoubling的更多相关文章
- TopCoder SRM 596 DIV 1 250
body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } P ...
- TopCoder kawigiEdit插件配置
kawigiEdit插件可以提高 TopCoder编译,提交效率,可以管理保存每次SRM的代码. kawigiEdit下载地址:http://code.google.com/p/kawigiedit/ ...
- 记第一次TopCoder, 练习SRM 583 div2 250
今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...
- TopCoder比赛总结表
TopCoder 250 500 ...
- Topcoder几例C++字符串应用
本文写于9月初,是利用Topcoder准备应聘时的机试环节临时补习的C++的一部分内容.签约之后,没有再进行练习,此文暂告一段落. 换句话说,就是本文太监了,一直做草稿看着别扭,删掉又觉得可惜,索性发 ...
- TopCoder
在TopCoder下载好luncher,网址:https://www.topcoder.com/community/competitive%20programming/ 选择launch web ar ...
- 求拓扑排序的数量,例题 topcoder srm 654 div2 500
周赛时遇到的一道比较有意思的题目: Problem Statement There are N rooms in Maki's new house. The rooms are number ...
- TopCoder SRM 590
第一次做TC,不太习惯,各种调试,只做了一题...... Problem Statement Fox Ciel is going to play Gomoku with her friend ...
- Topcoder Arena插件配置和训练指南
一. Arena插件配置 1. 下载Arena 指针:http://community.topcoder.com/tc?module=MyHome 左边Competitions->Algorit ...
随机推荐
- Spark技术内幕:Client,Master和Worker 通信源码解析
http://blog.csdn.net/anzhsoft/article/details/30802603 Spark的Cluster Manager可以有几种部署模式: Standlone Mes ...
- C#学习笔记15:字符串、文件、目录的操作方法
字符串:不可变性 String str=”abcdf”; 将字符串转换为char数组:ToCharArray(); Char[] ch=str.ToCharAarray(); 将char数组转换为字符 ...
- 认识JSON
JSON 全称 JavaScript Object Notation,意思是JavaScript对象表示法.是一种基于文本的.独立于语言的轻量级数据交换格式.易于阅读和编写,易于机器解析和生成. 一. ...
- sql标准化的后缀
今天在SQL编码风格中看到的sql编码标准
- wamp优化
友情链接:IT狂人博客 转载请注明作者:浮沉雄鹰 和本文链接:http://www.cnblogs.com/xby1993/p/3342085.html 一.修改php.ini, 修改上传文件大小限制 ...
- vue防止闪烁
v-text也可以 转意的话使用v-html <style> [v-clock]{ display:none } <style> <span>{{msg}}< ...
- mina2.0 spring
Apache MINA是一个网络应用程序框架,它可以帮助用户开发的高性能.高扩展性的网络应用程序.它提供了一个抽象的事件驱动的异步API在不同传输如TCP/IP和UDP/IP通过java NIO. A ...
- python 自动化之路 day 04
内容目录: 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 4.嵌套函数 5.递归 6.匿名函数 7.函数式编程介绍 8.高阶函数 9.内置函数 1.函数基本语法及特性 背景提要 现在老 ...
- rpm方式安装gcc缺少依赖项的解决方法
使用rpm方式安装gcc时,有时会报缺少依赖项: libmpfr.so.1 is needed by cpp-4.4.4-13.el6.i686 libppl.so.7 is needed by cl ...
- android hander 线程用法
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanc ...