[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 ...
随机推荐
- npm模块管理器入门
什么是 NPM npm 是 Node 官方提供的包管理工具,他已经成了 Node 包的标准发布平台,用于 Node 包的发布.传播.依赖控制.npm 提供了命令行工具,使你可以方便地下载.安装.升级. ...
- 【微软大法好】VS Tools for AI全攻略(3)
接着上文,现在我们需要一种穷人的方法来搭建好Azure虚拟机. 思路很简单,因为AI组件的原理其实是传送了script文件和命令上去,那么我们这个虚拟机只要做好了所有的配置,那么我们就可以将它当作深度 ...
- RPC是什么
RPC是什么? 通俗的讲就是,调用远程计算机上的服务,就像调用本地服务一样.通常包含传输协议和编码协议. RPC可以基于HTTP或TCP协议,但基于HTTP协议的RPC性能却不如基于TCP协议的RPC ...
- web兼容性测试相关知识
一.客户端兼容性 1.浏览器的兼容性测试 a.内核角度 Tridnt内核:代表作IE.腾讯.遨游.世界之窗等 Gecko内核:代表作Firefox webkit内核:代表作Safari.Chrome ...
- nginx+tomcat安装配置
nginx+tomcat安装配置 # nginx+tomcat安装配置 #创建网站目录 mkdir -p /www/wwwroot cd /www #安装配置 wget http://mirrors. ...
- php常用面试题
1. 有一列数的规则如下 1.1.2.3.5.8.13.21.34... 求第30位数是多少.写出相关函数和算法名称 //$pxx = array(1,1);//for($i=2;$i<=29; ...
- 高质量PHP代码的50个实用技巧必备(上)
1.不要使用相对路径 常常会看到: ? 1 require_once('../../lib/some_class.php'); 该方法有很多缺点: 它首先查找指定的php包含路径, 然后查找当前目录. ...
- Pyhton爬虫实战 - 抓取BOSS直聘职位描述 和 数据清洗
Pyhton爬虫实战 - 抓取BOSS直聘职位描述 和 数据清洗 零.致谢 感谢BOSS直聘相对权威的招聘信息,使本人有了这次比较有意思的研究之旅. 由于爬虫持续爬取 www.zhipin.com 网 ...
- Java Random介绍
一.简介 Random类位于java.util包下,此类的实例用于生成伪随机数流.之所以称之为伪随机,是因为真正意义上的随机数(或者称为随机事件)在某次产生过程中是按照实验过程表现的分布概率随机产生的 ...
- vmware 上ubuntu server连接外网
最近在自己的电脑上搞了个ubuntu server,自己做点小demo,当需要连接网络时问题来了,没法连接网络,网上有一些资料都是一笔带过,并不符合自己的情况,现在将我解决的过程记录下: 首先,在自己 ...