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 ...
随机推荐
- Android视频编辑SDK--RDVECore来自锐动的无UI,高度抽象化API
1 RDVECore功能概述 RDVECore是锐动推出的无UI,高度抽象化API的视频编辑SDK,支持以下功能: 1.1 丰富的编辑功能 RDVECore包含了丰富的基础功能,对于编辑中的视频.图片 ...
- MFC中小笔记(三)
10.在添加新Menu之后,代码中 调用 创建的IDR_MENU1,一直出现 Debug Assertion Failed的情况.原因是,没有写入到 项目.RC中,需要更新下rc(资源文件). 然后进 ...
- 使用map做数组与链表去重
#include<iostream> #include<map> using namespace std; class node{ public: node():value() ...
- Windows下根据端口号查找进程并关闭
经常用到,但是总记不住命令,备忘一下…… netstat -aon|findstr "8080" 找到进程号 tasklist|findstr "7200" ...
- 学习makefile的一个工程示例
前言 makefile推荐资料为陈皓的跟我一起写makefile,需要pdf资源的可以私我 正文 工程目录结构 ---include(放置头文件.h) ------student.h(Student类 ...
- ##3.Keystone 验证服务--openstack
##3.Keystone 验证服务 openstack pike 安装 目录汇总 http://www.cnblogs.com/elvi/p/7613861.html #SQL上创建数据库并授权 #K ...
- Laravel 5 框架性能优化技巧
性能一直是 Laravel 框架为人诟病的一个点,所以调优 Laravel 程序算是一个必学的技能. 接下来分享一些开发的最佳实践,还有调优技巧,大家有别的建议也欢迎留言讨论 1.配置缓存信息 使用l ...
- caioj 1237: 【最近公共祖先】树上任意两点的距离 在线倍增ST
caioj 1237: [最近公共祖先]树上任意两点的距离 倍增ST 题目链接:http://caioj.cn/problem.php?id=1237 思路: 针对询问次数多的时候,采取倍增求取LCA ...
- Hexo + github 打造个人博客
前两年开始用 wordpress 搭了一个网站,但服务器是在 Linode 上,之所以要放在 Linode 上,要从买的域名说起,因为我买的域名是 fengzheng.pub ,.pub 是不允许备案 ...
- CentOS7脱机安装SQL Server 2017
SQL Server on Linux也发布一段时间了,官方上支持Red Hat, SUSE, Ubuntu.手上没有以上Linux版本,选用了与Red Hat最接近的CentOS7.4来进行安装和测 ...