首页 > 代码库 > TopCoder SRM 596 DIV 1 250
TopCoder SRM 596 DIV 1 250
<style>body { font-family: Monospaced; font-size: 12pt }
pre { font-family: Monospaced; font-size: 12pt }</style>
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。