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. react入门到进阶(一)

    一.何为react Facebook在F8会议上首次提出这个概念,一套全新的框架就此诞生. React 不是一个完整的 MVC.MVVM 框架,其只负责 View 层 React 跟 Web Comp ...

  2. 晓莲说-何不原创:java 实现二维数组冒泡排序

    新手从业路-为自己回顾知识的同时,也希望和大家分享经验: 话不多说,上代码 public class 冒泡排序 { /**     * @param admin     * @2017.12.4   ...

  3. 自动化运维工具——ansible详解(一)

    ansible 简介 ansible 是什么? ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.chef.func.fabric)的优点,实现了批量系统 ...

  4. Elasticsearch JAVA api搞定groupBy聚合

    本文给出如何使用Elasticsearch的Java API做类似SQL的group by聚合.为了简单起见,只给出一级groupby即group by field1(而不涉及到多级,例如group ...

  5. YiShop_做个网上商城系统多少钱

    随着国内电商的兴起,不少企业和个人卖家都已经意识到电商的重要性,于是就开始搭建自己网上商城,但是还是有很多人对网上商城还不是很了解,今天就由YiShop小编跟大家讲解网上商城系统的一些知识一.网上商城 ...

  6. javascript设计模式——模板方法模式

    前面的话 在javascript开发中用到继承的场景其实并不是很多,很多时候喜欢用mix-in的方式给对象扩展属性.但这不代表继承在javascript里没有用武之地,虽然没有真正的类和继承机制,但可 ...

  7. JAVA实现同域单点登录

    所用技术: SSM MySQL Maven Tomcat8.0 同域单点登录详细步骤如下: 1.首先写一个登录界面(隐藏域为暂存地址) 2.判断用户密码是否正确,正确则添加cookie,否则返回错误页 ...

  8. 【微信小程序开发】快速开发一个动态横向导航模板并使用

    目标:做个横向导航,可以横向滚动. 思路:使用scroll-view组件,可实现横向滚动功能.scroll-view内包含一个动态的view列表,代表导航的每一项,导航要接收动态数组,然后使用列表展示 ...

  9. Android异步消息机制

    Android中的异步消息机制分为四个部分:Message.Handler.MessageQueue和Looper. 其中,Message是线程之间传递的消息,其what.arg1.arg2字段可以携 ...

  10. Socket TCP Server一个端口可以有多少个长连接?受到什么影响?linux最大文件句柄数量总结

    Socket TCP Server一个端口可以有多少个长连接? 网上答案很多,不知道那个才是正确的 理论上是无限的 16.Linux中,一个端口能够接受tcp链接数量的理论上限是? A.1024 B. ...