Problem Statement

    

You have an array with N elements. Initially, each element is 0. You can perform the following operations:

  • Increment operation: Choose one element of the array and increment the value by one.
  • Doubling operation: Double the value of each element.

You are given a vector <int> desiredArray containing N elements. Compute and return the smallest possible number of operations needed to change the array from all zeros to desiredArray.

Definition

    
Class: IncrementAndDoubling
Method: getMin
Parameters: vector <int>
Returns: int
Method signature: int getMin(vector <int> desiredArray)
(be sure your method is public)

Limits

    
Time limit (s): 2.000
Memory limit (MB): 64

Constraints

- desiredArray will contain between 1 and 50 elements, inclusive.
- Each element of desiredArray will be between 0 and 1,000, inclusive.

Examples

0)  
    
{2, 1}
Returns: 3
One of the optimal solutions is to apply increment operations to element 0 twice and then to element 1 once. Total number of operations is 3.
1)  
    
{16, 16, 16}
Returns: 7
The optimum solution looks as follows. First, apply an increment operation to each element. Then apply the doubling operation four times. Total number of operations is 3+4=7.
2)  
    
{100}
Returns: 9
 
3)  
    
{0, 0, 1, 0, 1}
Returns: 2
Some elements in desiredArray may be zeros.
4)  
    
{123, 234, 345, 456, 567, 789}
Returns: 40
 
5)  
    
{7,5,8,1,8,6,6,5,3,5,5,2,8,9,9,4,6,9,4,4,1,9,9,2,8,4,7,4,8,8,6,3,9,4,3,4,5,1,9,8,3,8,3,7,9,3,8,4,4,7}
Returns: 84
 

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

集训队题解

给出一个序列,n个元素,初始为0。操作有二:

1.给某个数加1

2.给所有数乘2

求变成目标序列的最小次数。

 #include <bits/stdc++.h>
using namespace std; class IncrementAndDoubling {
public:
int getMin(vector<int> desiredArray) {
int cnt = , maxi = ;
for (auto i : desiredArray)
for (int j = ; i >> j; ++j)
maxi = max(maxi, j), cnt += (i >> j) & ;
return cnt + maxi;
}
};

@Author: YouSiki

TopCoder SRM 596 DIV 1 250的更多相关文章

  1. Topcoder SRM 656 (Div.1) 250 RandomPancakeStack - 概率+记忆化搜索

    最近连续三次TC爆零了,,,我的心好痛. 不知怎么想的,这题把题意理解成,第一次选择j,第二次选择i后,只能从1~i-1.i+1~j找,其实还可以从j+1~n中找,只要没有被选中过就行... [题意] ...

  2. 【topcoder SRM 702 DIV 2 250】TestTaking

    Problem Statement Recently, Alice had to take a test. The test consisted of a sequence of true/false ...

  3. Topcoder SRM 661 (Div.1) 250 MissingLCM - 数论

    [题意] 给你一个数N(1<=N<=10^6),要求最小的M(M>N),使得lcm(n+1,n+2,...m)=lcm(1,2,3,...,m) [思路] 手速太慢啦,等敲完代码的时 ...

  4. TopCoder SRM 560 Div 1 - Problem 1000 BoundedOptimization & Codeforces 839 E

    传送门:https://284914869.github.io/AEoj/560.html 题目简述: 定义"项"为两个不同变量相乘. 求一个由多个不同"项"相 ...

  5. TopCoder SRM 667 Div.2题解

    概览: T1 枚举 T2 状压DP T3 DP TopCoder SRM 667 Div.2 T1 解题思路 由于数据范围很小,所以直接枚举所有点,判断是否可行.时间复杂度O(δX × δY),空间复 ...

  6. 竞赛图的得分序列 (SRM 717 div 1 250)

    SRM 717 DIV 1 中 出了这样一道题: 竞赛图就是把一个无向完全图的边定向后得到的有向图,得分序列就是每个点的出度构成的序列. 给出一个合法的竞赛图出度序列, 要求构造出原图(原题是求(u, ...

  7. [topcoder]SRM 646 DIV 2

    第一题:K等于1或者2,非常简单.略.K更多的情况,http://www.cnblogs.com/lautsie/p/4242975.html,值得思考. 第二题:http://www.cnblogs ...

  8. [topcoder]SRM 633 DIV 2

    第一题,http://community.topcoder.com/stat?c=problem_statement&pm=13462&rd=16076 模拟就可以了. #includ ...

  9. SRM 596 DIV 2

    前段时间终于配置好了TopCoder的环境,所以就拿这场的DIV2练习了一下 1. 250pt FoxAndSightseeing 题意 给你n个城市的位置,他们在同一直线上,要求你跳过其中某一个城市 ...

随机推荐

  1. JavaScript Number 对象

    JavaScript Number 对象 Number 对象 Number 对象是原始数值的包装对象. Number 创建方式 new Number(). 语法 var num = new Numbe ...

  2. Openjudge 1.13-40 提取数字串按数值排序

    40:提取数字串按数值排序 查看 总时间限制:  1000ms 内存限制:  65536kB 描述 给定一个字符串,请将其中的所有数字串提取,并将每个数字串作为整数看待(假设可以用int 表示),按从 ...

  3. Post model至Web Api

    Post model可以解决多动态性的参数至Web Api中去,获取数据或是创建数据.如果一个对象有很多字段,需要对每一个字段进行查询或是在创建对象时,我们可以考虑使用model来作参数.这样不必在控 ...

  4. HBase简介

    HBase简介 HBase – Hadoop Database,是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统,利用HBase技术可在廉价PC Server上搭建起大规模结构化存储集群. HB ...

  5. Linux shell特性

    一:别名 .alias 查看本用户下的alias配置 --自定义别名:alias 别名='shell命令' (注意是单引号) --cat $HOME/.bashrc 在这个用户下配置着alias名的配 ...

  6. SQL server 数据库备份还原Sql

    /************ 一.数据库备份 ************/ --完整备份默认追加到现有的文件 backup database DBXS To disk='d:\backup\DBXS_fu ...

  7. node基础06:回调函数

    1.Node异步编程 Node.js 异步编程的直接体现就是回调. 异步编程依托于回调来实现,但不能说使用了回调后程序就异步化了. 回调函数在完成任务后就会被调用,Node 使用了大量的回调函数,No ...

  8. Datatable删除行的Delete和Remove方法

    在C#中,如果要删除DataTable中的某一行,大约有以下几种办法: 1,使用DataTable.Rows.Remove(DataRow),或者DataTable.Rows.RemoveAt(ind ...

  9. 字符串匹配(hash算法)

    hash函数对大家来说不陌生吧 ? 而这次我们就用hash函数来实现字符串匹配. 首先我们会想一下二进制数. 对于任意一个二进制数,我们将它化为10进制的数的方法如下(以二进制数1101101为例): ...

  10. 工作随笔——pre-commit钩子限制日志长度和提交的文件类型

    2014-09-18:解决Subversion edge 的hook中文乱码问题 近期检查SVN时发现备份好的文件体积异常庞大.才跑2个月备份出来的大小就有4G多.仔细查询发现很多很多IDE自动生成的 ...