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. SpringBoot+Redis环境搭建

    写在正文前的絮叨: 其实这个环境的搭建是很简单的,照着官网给的说明很快就可以搭建测试出来.为什么又要写出来呢?只是为了记录.保留.分享这其中遇到的坑. 这个环境之前在架构一个简单系统时,也曾经搭建过, ...

  2. 三、Hadoop学习笔记————从MapReduce到Yarn

    Yarn减轻了JobTracker的负担,对其进行了解耦

  3. Java数据结构和算法(三)——冒泡、选择、插入排序算法

    上一篇博客我们实现的数组结构是无序的,也就是纯粹按照插入顺序进行排列,那么如何进行元素排序,本篇博客我们介绍几种简单的排序算法. 1.冒泡排序 这个名词的由来很好理解,一般河水中的冒泡,水底刚冒出来的 ...

  4. 人体姿态的相似性评价基于OpenCV实现最近邻分类KNN K-Nearest Neighbors

    最近学习了人体姿态的相似性评价.需要用到KNN来统计与当前姿态相似的k个姿态信息. 假设我们已经有了矩阵W和给定的测试样本姿态Xi,需要寻找与Xi相似的几个姿态,来估计当前Xi的姿态标签. //knn ...

  5. 机器学习算法 - 支持向量机SVM

    在上两节中,我们讲解了机器学习的决策树和k-近邻算法,本节我们讲解另外一种分类算法:支持向量机SVM. SVM是迄今为止最好使用的分类器之一,它可以不加修改即可直接使用,从而得到低错误率的结果. [案 ...

  6. 算法提高 9-3摩尔斯电码 map

    算法提高 9-3摩尔斯电码 时间限制:1.0s   内存限制:256.0MB     问题描述 摩尔斯电码破译.类似于乔林教材第213页的例6.5,要求输入摩尔斯码,返回英文.请不要使用"z ...

  7. PostgreSQL启动main函数都干了什么(一)

    DB Version:9.5.3 环境:CentOS7.x 调试工具:GDB source:src/backend/main/main.c 56 /* 57 * Any Postgres server ...

  8. ubuntu 一些琐碎知识

    2017/09/01 ubuntu下面配置git公钥 $ git config --global user.name "Your Name" $ git config --glob ...

  9. JavaScript面向对象编程(9)高速构建继承关系之整合原型链

    前面我们铺垫了非常多细节.是为了让大家更加明晰prototype的使用细节: 如今能够将前面的知识整合起来,写一个函数用于高速构建基于原型链的继承关系了: function extend(Child, ...

  10. 【Scala-ML】使用Scala构建机器学习工作流

    引言 在这一小节中.我将介绍基于数据(函数式)的方法来构建数据应用.这里会介绍monadic设计来创建动态工作流,利用依赖注入这种高级函数式特性来构建轻便的计算工作流. 建模过程 在统计学和概率论中, ...