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]的更多相关文章

  1. ES5对Array增强的9个API

    为了更方便的对Array进行操作,ES5规范在Array的原型上新增了9个方法,分别是forEach.filter.map.reduce.reduceRight.some.every.indexOf ...

  2. JavaScript Array数组方法详解

    Array类型是ECMAScript中最常用的引用类型.ECMAScript中的数据与其它大多数语言中的数组有着相当大的区别.虽然ECMAScript中的数据与其它语言中的数组一样都是数据的有序列表, ...

  3. JavaScript Array 常用函数整理

    按字母顺序整理 索引 Array.prototype.concat() Array.prototype.filter() Array.prototype.indexOf() Array.prototy ...

  4. 数组求和,计算给定数组 arr 中所有元素的总和

    一,题目分析:可以使用数组的归并方法计算,reduce和reduceRight.二者作用几乎相同.只是归并方向相反.reduce和reduceRight都可以接收两个参数.第一个是在每一项上调用的函数 ...

  5. polyfill之javascript函数的兼容写法——Array篇

    1. Array.isArray(obj) if (!Array.isArray) { Array.isArray = function(arg) { return Object.prototype. ...

  6. 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) ...

  7. array_flip() array_merge() array+array的使用总结

    array_flip(array); //传递一个数组参数,对该数组的键.值进行翻转 例如: $a = array( 'a', 'b', 'c' ); print_r(array_flip($a)); ...

  8. Array.prototype.indexOf

    arr.indexOf(searchElement[, fromIndex = 0]) Array.prototype.indexOf()

  9. JavaScript Array(数组)对象

    一,定义数组 数组对象用来在单独的变量名中存储一系列的值. 创建 Array 对象的语法: new Array(); new Array(size); new Array(element0, elem ...

随机推荐

  1. 状态压缩dp第一题

    标签: ACM 题目: Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; ...

  2. CM5(5.11.0)和CDH5(5.11.0)离线安装

    概述 文件下载 系统环境搭建 日志查看 Q&A 参考 概述 CDH (Cloudera's Distribution, including Apache Hadoop),是Hadoop众多分支 ...

  3. python语言 第一个程序

    print("hello word!") # for i in range(1, 10):# for j in range(1, 10):# print(j, "x&qu ...

  4. Less的!important关键字

    Less的!important关键字 在调用 mixin 时,如果在后面追加 !important 关键字,就可以将 mixin 里面的所有属性都标记为 !important.如,以下Less代码: ...

  5. 4.2 例题: 统计字符数 poj2247

    问题描述 判断一个由 a-z 这 26 个字符组成的字符串中哪个字符出现的次数最多 输入:第 1 行是测试数据的组数 n,每组测试数据占 1 行,是一个由 a-z 这 26 个字符组 成的字符串,每组 ...

  6. Ipad弹出UIAlertControllerStyleActionSheet时发生崩溃

    pad弹出UIAlertControllerStyleActionSheet时,在iphone上运行正常,但在ipad上崩溃,解决代码如下: UIAlertController *alertVc = ...

  7. [全排列]--A Number Puzzle

    标签: ACM Lele 最近上课的时候都很无聊,所以他发明了一个数字游戏来打发时间. 这个游戏是这样的,首先,他拿出几张纸片,分别写上0到9之间的任意数字(可重复写某个数字),然后,他叫同学随便写两 ...

  8. 前端构建工具之争——Webpack vs Gulp 谁会被拍死在沙滩上

    .table tr>td:nth-child(1){width: 2em !important;padding-left: .6rem !important;padding-right: .6r ...

  9. mysql索引优化面试题

    曾经偷偷的面试了两个单位,都提到了Mysql的优化问题,所以以后要多多学习数据库的优化知识了.建设数据库的优化大概主要就是索引的优化了吧,因为我们不可能修改数据结构的情况下,提高数据库的查询效率似乎也 ...

  10. System.Security.Cryptography.RSA.FromXmlString 系统找不到指定的文件和X509读取证书文件系统找不到指定的文件异常

    前言: 最近公司增加服务器,在新增加的服务器中发现一些问题. 1.应用程序在读取证书文件中出现"系统找不到指定的文件."异常,但是已经确认证书文件存在.本地测试也可以读取,就在新增 ...