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. android堆栈调试--详细

    1.将ndk中的arm-linux-androideabi-addr2line可执行文件的路径加入配置文件~/.bashrc中,例如: export PATH=$PATH:~/dlna/android ...

  2. Foundation框架的小总结

    一.Foundation框架—结构体 一.基本知识 Foundation框架中包含了很多开发中常用的数据类型,如结构体,枚举,类等,是其他ios框架的基础. 如果要想使用foundation框架中的数 ...

  3. 企业级监控zabbix基础

    一个标准的监控系统所具备的基本功能: 1.数据的采集 2.为了展示其长期走势,将数据存储下来 3.万一某次采样的结果不在被认为是合理的范围内,然后就会做出告警操作,尽早的让相关人员得知到此消息 4.展 ...

  4. Activity讲解

    Activity Activity 是 Android 应用的重要组成单元之一(另外三个是 Service.BroadcastReceiver 和 ContentProvider),而 Activit ...

  5. Servlet交互【重定向 与 请求分派】详解

    Servlet交互 在serlvet中,需要调用另外一个资源来对浏览器的请求进行响应,两种方式实现: 调用HttpServletResponse.sendRedirect 方法实现 重定向 调用Req ...

  6. 享元模式(Flyweight)

    享元模式(Flyweight) 享元模式的主要目的是实现对象的共享,即共享池,当系统中对象多的时候可以减少内存的开销,通常与工厂模式一起使用. FlyWeightFactory负责创建和管理享元单元, ...

  7. UWP 应用程序内购

    今天来说一下应用程序内购的问题,这里面有坑,给自己做个笔记,也给需要的人提个醒. 我目前的需要是可以允许用户捐赠赞助App的形式内购,最终效果如下 只讲上面的列表部分,下面的就是图片布局啥的,没意思了 ...

  8. gem install报错

    [root@app-test ruby-2.2.3]# gem install capistrano -v 2.15 ERROR:  While executing gem ... (Gem::Rem ...

  9. Linux下批量修改文件名方法

    对于在Linux中修改文件名的方式一般我们会用mv命令进行修改,但是mv命令是无法处理大量文件修改名称. 但是在处理大量文件的时候该如何进行批量修改呢? 方法一:mv配合for循环方式进行修改 [ro ...

  10. Android开发之漫漫长途 Ⅷ——Android Binder(也许是最容易理解的)

    该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...