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. HTML5学习指导路线

    HTML5是现在热门的技术,经过8年的艰苦努力,该标准规范终于制定完成,在这里为想要学习HTML5初级程序员详细划分一下学习内容和步骤,让大家清楚的知道HTML5需要学什么?能够快速掌握HTML5开发 ...

  2. 用JS实现Ajax请求

    AJAX核心(XMLHttpRequest) 其实AJAX就是在Javascript中多添加了一个对象:XMLHttpRequest对象.所有的异步交互都是使用XMLHttpServlet对象完成的. ...

  3. windows配置git

    每次要使用git指令的时候都要去打开git bash 操作,太麻烦,要想直接在dos窗口下使用git指令需要再进行如下环境变量配置. 1.系统环境变量path添加:D://programFiles/g ...

  4. CCF-201409-3-字符串匹配

    问题描述 试题编号: 201409-3 试题名称: 字符串匹配 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 给出一个字符串和多行文字,在这些文字中找到字符串出现的那些行.你 ...

  5. linux配置分步安装lnmp环境----ghj

    前台:nignx 后台:apache[linux命令]用户文件目录启动定时任务:[开启RZ命令]yum -y install lrzsz [防火墙]防火墙配置文件: /etc/sysconfig/ip ...

  6. PHP实现的进度条效果详解

      <?php //防止执行超时 set_time_limit(0); //清空并关闭输出缓存 ob_end_clean(); //需要循环的数据 for($i = 0; $i < 188 ...

  7. Scala中的override

    Scala中的override override是覆盖的意思,在很多语言中都有,在scala中,override是非常常见的,在类继承方面,它和java不一样,不是可写可不写的了,而是必须写的.如果不 ...

  8. Linux压力测试软件Stress安装及使用指南

      一.Stress是什么 stress是一个linux下的压力测试工具,专门为那些想要测试自己的系统,完全高负荷和监督这些设备运行的用户. 二.安装 将stress的安装包上传并解压到linux服务 ...

  9. linux shell中单引号、双引号和没有引号的区别

    单引号: 可以说是所见即所得:即将单引号的内的内容原样输出,或者描述为单引号里面看到的是什么就会输出什么. 双引号: 把双引号内的内容输出出来:如果内容中有命令.变量等,会先把变量.命令解析出结果,然 ...

  10. 【jQuery】复选框的全选、反选,推断哪些复选框被选中

    本文与<[JavaScript]复选框的全选.反选.推断哪些复选框被选中>(点击打开链接)为姊妹篇,把里面内容再与jQuery框架中实现一次,相同做到例如以下的效果: 布局还是相同的布局, ...