zoukankan      html  css  js  c++  java
  • TopCoder SRM 596 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 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

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

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 class IncrementAndDoubling {
     5 public:
     6     int getMin(vector<int> desiredArray) {
     7         int cnt = 0, maxi = 0;
     8         for (auto i : desiredArray)
     9             for (int j = 0; i >> j; ++j)
    10                 maxi = max(maxi, j), cnt += (i >> j) & 1;
    11         return cnt + maxi;
    12     }
    13 };

    @Author: YouSiki

  • 相关阅读:
    Vuejs
    Vuejs
    Vuejs
    Vuejs
    JS高级之面试必须知道的几个点
    用vue快速开发app的脚手架工具
    作为一名前端开发工程师,你必须掌握的WEB模板引擎:Handlebars
    REST接口设计规范总结
    eclipse实现JavaWeb应用增量打包
    转载:作为面试官,我是怎么快速判断程序员能力的?
  • 原文地址:https://www.cnblogs.com/yousiki/p/6129468.html
Copyright © 2011-2022 走看看