[Maximize ∑arr[i]*i of an Array]
Given an array of N integers. Your task is to write a program to find the maximum value of ∑arr[i]*i, where i = 0, 1, 2,…., n – 1.
You are allowed to rearrange the element of the array
Examples:
Input : N = , arr[] = { , , , } Output : If we arrange arr[] , , , }. Sum of arr[i]*i * + * + * + * = , which is maximum Input : N = , arr[] = { , } Output :
Note: Since output could be large, hence module 10^9+7 and then print answer.
Input:
First line of the input contains an integer T, denoting the number of test cases. Then T test case follows. First line of each test case contains an integer N, denoting the size of the array. Next line contains N space separated integers denoting the elements of the array.
Output:
For each test case print the required answer on a new line.
Constraints:
1<=T<=103
1<=N<=103
Example:
Input:
2
5
5 3 2 4 1
3
1 2 3
Output:
40
8
下面是我的代码实现:思路:先对数组进行升序排列,然后按着公式即是最大值。我就奇了怪了,我的代码应该是没问题的,在OJ上出现了不可理解的错误:
代码如下:
#include <bits/stdc++.h> using namespace std; int main() { int num,i; cin>>num; for(i=0;i<num;i++) { int N,j,k,temp; long sum=0; cin>>N; int *Arr=new int[N]; for(j=0;j<N;j++) cin>>Arr[j]; for(j=0;j<N;j++) { for(k=0;k<N;k++) { if(Arr[k]>Arr[k+1]) { temp=Arr[k]; Arr[k]=Arr[k+1]; Arr[k+1]=temp; } } } for(j=0;j<N;j++) sum=sum+j*Arr[j]; cout<<sum<<endl; } return 0; }
然后提交,反馈是:
Wrong Answer. !!!Wrong Answer Possibly your code doesn't work correctly for multiple test-cases (TCs). The first test case where your code failed: Input: Its Correct output is: And Your Code's output is:
但是我在本地运行的这个例子的答案是:237220
就是不明白为什么在OJ上就是233266,好气哦
[Maximize ∑arr[i]*i of an Array]的更多相关文章
- ES5对Array增强的9个API
为了更方便的对Array进行操作,ES5规范在Array的原型上新增了9个方法,分别是forEach.filter.map.reduce.reduceRight.some.every.indexOf ...
- JavaScript Array数组方法详解
Array类型是ECMAScript中最常用的引用类型.ECMAScript中的数据与其它大多数语言中的数组有着相当大的区别.虽然ECMAScript中的数据与其它语言中的数组一样都是数据的有序列表, ...
- JavaScript Array 常用函数整理
按字母顺序整理 索引 Array.prototype.concat() Array.prototype.filter() Array.prototype.indexOf() Array.prototy ...
- 数组求和,计算给定数组 arr 中所有元素的总和
一,题目分析:可以使用数组的归并方法计算,reduce和reduceRight.二者作用几乎相同.只是归并方向相反.reduce和reduceRight都可以接收两个参数.第一个是在每一项上调用的函数 ...
- polyfill之javascript函数的兼容写法——Array篇
1. Array.isArray(obj) if (!Array.isArray) { Array.isArray = function(arg) { return Object.prototype. ...
- Find the largest K numbers from array (找出数组中最大的K个值)
Recently i was doing some study on algorithms. A classic problem is to find the K largest(smallest) ...
- array_flip() array_merge() array+array的使用总结
array_flip(array); //传递一个数组参数,对该数组的键.值进行翻转 例如: $a = array( 'a', 'b', 'c' ); print_r(array_flip($a)); ...
- Array.prototype.indexOf
arr.indexOf(searchElement[, fromIndex = 0]) Array.prototype.indexOf()
- JavaScript Array(数组)对象
一,定义数组 数组对象用来在单独的变量名中存储一系列的值. 创建 Array 对象的语法: new Array(); new Array(size); new Array(element0, elem ...
随机推荐
- PHP启动:动态库加载失败(PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/php7/lib/php/extensions/no-debug-non-zts-20151012/*.so')
今天在linux上面智障一般搞了好久,本来想安装个swoole的,然后用 php -m 的命令想看下安装的PHP扩展库,发现有的扩展库有的可以出来,有的加载失败, 加载失败的错误类型: PHP Wa ...
- 万能动态库调用工具IDMA(InvokeDllMethodsAdvance)
万能动态库调用工具IDMA 开发者:马兆瑞 QQ/微信:624762543 百度云下载链接:https://pan.baidu.com/s/1skW5W4H CSDN下载链接:http://d ...
- 大数据基础篇(一):联机分析处理(OLAP) 与 联机事务处理(OLTP)
联机事务处理(OLTP) OLTP也称实时系统(Real Time System),支持事务快速响应和大并发,这类系统典型的有ATM机(Automated Teller Machine)系统.自动售票 ...
- C#Winform窗体 DataGridView全选按钮的实现方式
最近,在做CS程序遇到一个头疼的问题,datagridview列表的全选按钮遇到各种问题,做的是自适应窗体大小,当窗体最大化导致全选按钮出现与列表数据不一致,特别不搭配,试了很久,网上也找了好多资料各 ...
- [flask/python/web] 解析flask web开发(Miguel著)一书第11章主页不显示博文表单的问题
---------------------------------------------以下内容2017.7.14更新---------------------------------------- ...
- xampp 出现403 无法访问问题(已解决)
最近重新安装xampp,配置虚拟主机做本地测试,但是总是出现服务器无法访问,权限不够的提示. 查找error文件后排查错误,发现是权限的问题.具体错误如下: 重新查看配置文件httpd.conf,才发 ...
- HDU3792---Twin Prime Conjecture(树状数组)
Twin Prime Conjecture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 深入理解ES6之—增强的数组功能
创建数组 Array.of()方法 ES6为数组新增创建方法的目的之一,是帮助开发者在使用Array构造器时避开js语言的一个怪异点.Array.of()方法总会创建一个包含所有传入参数的数组,而不管 ...
- ubuntu14.04 升级mysql到5.7版本
Ubuntu14.04默认安装的是mysql5.5,由于开发需要支持utf8mb4,因此需要升级到mysql5.7 默认情况下,apt是无法直接升级到mysql5.7的,因此需要额外设置 首先,备份数 ...
- Zookeeper 集群安装
负载均衡(Load Balance)是分布式系统架构设计中必须考虑的因素之一,它通常是指,将请求/数据[均匀]分摊到多个操作单元上执行,负载均衡的关键在于[均匀].常见互联网分布式架构如上,分为客户端 ...