C - Divide and Multiply
2021/12/14 23:47:23
本文主要是介绍C - Divide and Multiply,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
C - Divide and Multiply
原题链接找不到了...
William has array of nn numbers a1,a2,…,an He can perform the following sequence of operations any number of times:
- Pick any two items from array ai and aj, where ai must be a multiple of 2
- ai=ai/2
- aj = aj*2
Help William find the maximal sum of array elements, which he can get by performing the sequence of operations described above.
Input
Each test contains multiple test cases. The first line contains the number of test cases tt (1 \le t \le 10^41≤t≤104). Description of the test cases follows.
The first line of each test case contains an integer nn (1 \le n \le 15)(1≤n≤15), the number of elements in William's array.
The second line contains nn integers a_1, a_2, \dots, a_na1,a2,…,an (1 \le a_i < 16)(1≤ai<16), the contents of William's array.
Output
For each test case output the maximal sum of array elements after performing an optimal sequence of operations.
Example
Input5 3 6 4 2 5 1 2 3 4 5 1 10 3 2 3 4 15 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8Output
50 46 10 26 35184372088846
Note
In the first example test case the optimal sequence would be:
- Pick i = 2i=2 and j = 1j=1. After performing a sequence of operations a_2 = \frac{4}{2} = 2a2=24=2 and a_1 = 6 \cdot 2 = 12a1=6⋅2=12, making the array look as: [12, 2, 2].
- Pick i = 2i=2 and j = 1j=1. After performing a sequence of operations a_2 = \frac{2}{2} = 1a2=22=1 and a_1 = 12 \cdot 2 = 24a1=12⋅2=24, making the array look as: [24, 1, 2].
- Pick i = 3i=3 and j = 1j=1. After performing a sequence of operations a_3 = \frac{2}{2} = 1a3=22=1 and a_1 = 24 \cdot 2 = 48a1=24⋅2=48, making the array look as: [48, 1, 1].
The final answer 48 + 1 + 1 = 5048+1+1=50.
In the third example test case there is no way to change the sum of elements, so the answer is 1010.
题意:题目不算复杂,给一个大小为n的数组,通过上述运算使得数组总和最大,并输出最大值。
思路:先对数组内所有满足条件的元素进行运算2,即/2,记录次数cnt。将数组排序,数组的最大值 *= 2 cnt次
代码:
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll t,n,a[19],sum,cnt; int main() { cin >> t; while(t--) { memset(a,0,sizeof(a)); cnt = 0,sum = 0; cin >> n; for(int i = 0;i < n;i++) { cin >> a[i]; } for(int i = 0;i < n;i++) { while(a[i]>1 && !(a[i]&1)) a[i]/=2,cnt++; } sort(a,a+n); while(cnt--) a[n-1]*=2; for(int i = 0;i < n;i++) { sum += a[i]; } printf("%lld\n",sum); } }
这篇关于C - Divide and Multiply的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23增量更新怎么做?-icode9专业技术文章分享
- 2024-11-23压缩包加密方案有哪些?-icode9专业技术文章分享
- 2024-11-23用shell怎么写一个开机时自动同步远程仓库的代码?-icode9专业技术文章分享
- 2024-11-23webman可以同步自己的仓库吗?-icode9专业技术文章分享
- 2024-11-23在 Webman 中怎么判断是否有某命令进程正在运行?-icode9专业技术文章分享
- 2024-11-23如何重置new Swiper?-icode9专业技术文章分享
- 2024-11-23oss直传有什么好处?-icode9专业技术文章分享
- 2024-11-23如何将oss直传封装成一个组件在其他页面调用时都可以使用?-icode9专业技术文章分享
- 2024-11-23怎么使用laravel 11在代码里获取路由列表?-icode9专业技术文章分享
- 2024-11-22怎么实现ansible playbook 备份代码中命名包含时间戳功能?-icode9专业技术文章分享