[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 ...
随机推荐
- Android 个推 踩坑小结
公司一个项目之前在手机上一直可以正常运行,后来在平板上运行了一下,在欢迎页面卡出了,一直没有反应. 于是我就将项目在电脑上用Android Studio往平板上运行了一遍,看了下打印的Log日志,发现 ...
- JAVA 中LinkedHashMap要点记录
JAVA 中LinkedHashMap要点记录 构造函数中可能出现的几个参数说明如下: 1.initialCapacity 初始容量大小,使用无参构造方法时,此值默认是16 2.loadFactor ...
- Intellij自动下载导入框架包和常用快捷键
忽然发现intellij尽然可以自动导入 框架所需的包,而且可以选择jar包版本,瞬间发现Maven,gradle管理jar包还得写配置文件弱爆了. 以Hibernate为例: 1.ProjectSt ...
- [安全]服务器安全之 PHP权限目录
1.为每个主机配置增加一个 fastcgi_param PHP_VALUE "open_basedir=$document_root:/tmp/"; 或是直接把这句话放到fa ...
- 小白的Python之路 day2 字符串操作 , 字典操作
1. 字符串操作 特性:不可修改 name.capitalize() 首字母大写 name.casefold() 大写全部变小写 name.center(50,"-") 输出 '- ...
- java学习笔记IO之File类
File类总结 p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Times } p.p2 { margin: 0.0px 0.0px 0.0p ...
- js onmouseover与onmouseout用法
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 《Linux Device Drivers》第十八章 TTY驱动程序——note
简单介绍 tty设备的名称是从过去的电传打字机缩写而来,最初是指连接到Unix系统上的物理或虚拟终端 Linux tty驱动程序的核心紧挨在标准字符设备驱动层之下,并提供了一系列的功能,作为接口被终端 ...
- java使用线程请求訪问每次间隔10分钟连续5次,之后停止请求
java使用线程请求訪问每次间隔10分钟连续5次,收到对应的时候停止请求 package com.qlwb.business.util; /** * * * @类编号: * @类名称:RequestT ...
- openstack初始化Glance数据库时报错解决方式
环境为win7+virtualbox 中的centos6.5 安装Glance 的包 yum install openstack-glance python-glanceclient -y 配置Gla ...