Find the Maximum sum
Given an array of n elements.Find the maximum sum when the array elements will be arranged in such way. Multiply the elements of each pair and add to get maximum Sum. Sum could be larger so take mod with 10^9+7.
Example1:
Input: n=
-,,,,-,-,
Output:
So to ,-},{-,},{,} and {}.So the answer *(-))+((-)*)+(*)+() ={}.
Example2:
Input: n=
-,,
Output:
So to ,} and {}.So the answer )*)+()={}.
Input:
The first line consists of an integer T i.e number of test cases. The first line of each test case consists of an integer n.The next line consists of n spaced integers(positive or negative).
Output:
Print the maximum sum % 10^9+7.
Constraints:
1<=T<=100
1<=n,a[i]<=10000
Example:
Input:
2
3
8 7 9
6
-1 9 4 5 -4 7
Output:
79
87
下面是我的代码实现:
主要思路:(假设0是负数)
先对于输入的数组进行排序。排序按照从小到大的顺序。
如果数组个数是偶数个,那么直接按照顺序两两相乘然后相加即可得到最大和。
如果数组个数是奇数,需要考虑正数负数的个数,其中负数为偶数个,正数是奇数个(例如:-7 -4 -1 0 4 5 9)只需要将负数依次两两相乘,忽略第一个正数,其余正数依次两两相乘再求和即可得到最大和。如果负数的个数是奇数个,正数是偶数个(例如:-7 -4 -1 4 5 9 10)只需要跳过最后一个负数,其余依次相乘然后相加即可得到最大和。
具体代码实现如下:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num,i;
scanf("%d",&num);
int *result=(int *)malloc(sizeof(int)*num);
;i<num;i++)
{
int N,j,k,temp;
;
scanf("%d",&N);
int *Arr=(int *)malloc(sizeof(int)*N);
;j<N;j++)
scanf("%d",&Arr[j]);
//首先是对于数组元素进行排序。这里随便用一个冒泡排序。
;j<N;j++)
{
;k<N;k++)
{
])
{
temp=Arr[k];
Arr[k]=Arr[k+];
Arr[k+]=temp;
}
}
}
==)//N是偶数,两两相乘即可。
{
;j<N;j=j+)
{
sum=sum+Arr[j]*Arr[j+];
}
}
else//N是奇数的情况
{
k=;
;j<N;j++)
{
)
{
k++;//记录负数的个数
}
}
==) //数组中正奇、负偶:跳过第一个正数Arr[k]
{
;j<N;j=j+)
{
if(j!=k)
{
sum=sum+Arr[j]*Arr[j+];
}
else
{
sum=sum+Arr[k];
j--;
}
}
}
else//数组中正偶、负奇:跳过最后一个负数Arr[k-1]
{
;j<N;j=j+)
{
))
{
sum=sum+Arr[j]*Arr[j+];
}
else
{
sum=sum+Arr[k-];
j--;
}
}
}
}
result[i]=sum;
}
;i<num;i++)
printf("%d\n",result[i]);
;
}
但是这个程序,现在没有正确通过,在本地运行是没问题的,但是在geeksforgeeks上结果如下:

who can help me?
Find the Maximum sum的更多相关文章
- POJ2479 Maximum sum[DP|最大子段和]
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39599 Accepted: 12370 Des ...
- ural 1146. Maximum Sum
1146. Maximum Sum Time limit: 0.5 secondMemory limit: 64 MB Given a 2-dimensional array of positive ...
- UVa 108 - Maximum Sum(最大连续子序列)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- 最大子矩阵和 URAL 1146 Maximum Sum
题目传送门 /* 最大子矩阵和:把二维降到一维,即把列压缩:然后看是否满足最大连续子序列: 好像之前做过,没印象了,看来做过的题目要经常看看:) */ #include <cstdio> ...
- URAL 1146 Maximum Sum(最大子矩阵的和 DP)
Maximum Sum 大意:给你一个n*n的矩阵,求最大的子矩阵的和是多少. 思路:最開始我想的是预处理矩阵,遍历子矩阵的端点,发现复杂度是O(n^4).就不知道该怎么办了.问了一下,是压缩矩阵,转 ...
- ural 1146. Maximum Sum(动态规划)
1146. Maximum Sum Time limit: 1.0 second Memory limit: 64 MB Given a 2-dimensional array of positive ...
- UVa 10827 - Maximum sum on a torus
题目大意:UVa 108 - Maximum Sum的加强版,求最大子矩阵和,不过矩阵是可以循环的,矩阵到结尾时可以循环到开头.开始听纠结的,想着难道要分情况讨论吗?!就去网上搜,看到可以通过补全进行 ...
- POJ 2479 Maximum sum 解题报告
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40596 Accepted: 12663 Des ...
- [LeetCode] Maximum Sum of 3 Non-Overlapping Subarrays 三个非重叠子数组的最大和
In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...
随机推荐
- 苹果快速的修复了Mac OS High Sierra 上出现了root的漏洞
最近苹果因为Mac最新系统 Mac OS High Sierra 上出现了root的漏洞走上了风口浪尖,不过还好,在一封苹果给科技媒体'9to5 Mac'的回复中得知,苹果在接收到报告之后,立即展开修 ...
- JAVA基础5——与String相关的系列(1)
与String相关的系列 String, 是JAVA中常见的一个引用类型,且其具有一定的特殊性. String类型被设置为final型,即不可继承,也就不可修改其中的实现. String可以改变吗 S ...
- tensorflow初探
TensorFlow是一个采用数据流图,用于数值计算的开源软件库.自己接触tensorflow比较的早,可是并没有系统深入的学习过,现在TF在深度学习已经成了"标配",所以打算系统 ...
- Django框架中的视图和模板
视图views django中的视图就是用来定义函数来处理一些逻辑的核心地方. django中通过urls来建立路径跟views中的视图函数的映射关系. urls中的映射关系 ''' urlpatte ...
- Cracking the Coding Interview 题目分析笔记—— Array and String
1.Determine if a string has all unique characters learn: 为了减少空间利用率,其比较优秀的算法一般都适用位操作 返回值的命名方法,我们需要学习 ...
- LINQ学习系列-----3.1 查询非泛型集合
一.问题起源 LINQ to object在设计时,是配合IEnumerable<T>接口的泛型集合类型使用的,例如字典.数组.List<T>等,但是对于继承了IEnumera ...
- 读取不标准的JSON数据
正常的JSON数据 [ {"key":"UI","value":"UII"}, {"key ...
- zzuli 2131 Can Win dinic+链式前向星(难点:抽象出网络模型+建边)
2131: Can Win Time Limit: 1 Sec Memory Limit: 128 MB Submit: 431 Solved: 50 SubmitStatusWeb Board ...
- constructor 属性,判断是否为数组
<!--你可以使用 constructor 属性来查看是对象是否为数组 (包含字符串 "Array"):--><p>constructor属性返回变量或对象 ...
- [置顶]
Java WebService接口生成和调用 图文详解
webservice简介: Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的.专门的第三方软件或硬件, 就可相互交换数据或集成.依据Web Service规范实施的应用之间 ...