TopCoder SRM 596 DIV 1 250
Posted You Siki
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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:
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 |
|||||||||||||
|
|||||||||||||
Limits |
|||||||||||||
|
|||||||||||||
Constraints |
|||||||||||||
- | desiredArray will contain between 1 and 50 elements, inclusive. | ||||||||||||
- | Each element of desiredArray will be between 0 and 1,000, inclusive. | ||||||||||||
Examples |
|||||||||||||
0) | |||||||||||||
|
|||||||||||||
1) | |||||||||||||
|
|||||||||||||
2) | |||||||||||||
|
|||||||||||||
3) | |||||||||||||
|
|||||||||||||
4) | |||||||||||||
|
|||||||||||||
5) | |||||||||||||
|
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
以上是关于TopCoder SRM 596 DIV 1 250的主要内容,如果未能解决你的问题,请参考以下文章
Topcoder口胡记 SRM 562 Div 1 ~ SRM 592 Div 1