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

  • 相关阅读:
    python异常处理与上下文管理器
    SpringBoot-MongoDB 索引冲突分析及解决
    补习系列(13)-springboot redis 与发布订阅
    带着canvas去流浪系列之八 碰撞
    带着canvas去流浪系列之九 粒子动画
    通过哪吒动漫豆瓣影评,带你分析python爬虫与BeautifulSoup快速入门
    Python小数据保存,有多少中分类?不妨看看他们的类比与推荐方案...
    免费试用 | 多模 NoSQL 服务GeminiDB for Cassandra 全球首发
    Vue+ElementUI项目使用webpack输出MPA
    shell的shift用法
  • 原文地址:https://www.cnblogs.com/yousiki/p/6129468.html
Copyright © 2011-2022 走看看