SRM 596 1A 2013.12.9

SRM 596 1A 2013.12.9

DIV 1

250

 

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 ofthe array and increment the value by one.

Doubling operation: Double the value ofeach element.

You are given a vector <int>desiredArray containing N elements. Compute and return the smallest possiblenumber 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)

    

 

Constraints

-

desiredArray will contain between 1 and 50elements, inclusive.

-

Each element of desiredArray will bebetween 0 and 1,000, inclusive.

Examples

0)

 

    

{2, 1}

Returns: 3

One of the optimal solutions is to applyincrement operations to element 0 twice and then to element 1 once. Totalnumber 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 doublingoperation 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 andproprietary property of TopCoder, Inc. Any unauthorized use or reproduction ofthis information without the prior written consent of TopCoder, Inc. isstrictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

 

 

202.21

#include<iostream>

#include<string>

#include<vector>

 

using namespace std;

 

class IncrementAndDoubling

{

public: int getMin(vector <int>desiredArray)

         {

                            intans=0,doubleans=-1;

                            intadd[60]={0},dou[60]={0};

                            for(inti=0;i<desiredArray.size();i++)

                            {

                                     intp=desiredArray[i];

                                     while(p!=0)

                                     {

                                               if(p%2==0)

                                               {

                                                        dou[i]++;

                                                        p/=2;

                                               }

                                               else

                                               {

                                                        add[i]++;

                                                        p--;

                                               }

                                     }

                                     if(dou[i]>doubleans) doubleans=dou[i];

                                     ans+=add[i];

                            }

                            returnans+doubleans;

         }

};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值