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
求变成目标序列的最小次数。
#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