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 ...
随机推荐
- test only
Test TinyMCE only , Test Link
- Idea Live Templates代码模板
一. 概念 创建代码模板进行快速代码编写,如sout-->System.out.println();. 如我们经常要写logger的定义:private static final Logger ...
- C++反汇编第三讲,反汇编中识别虚表指针,以及指向的虚函数地址
C++反汇编第三讲,反汇编中识别虚表指针,以及指向的虚函数地址 讲解之前,了解下什么是虚函数,什么是虚表指针,了解下语法,(也算复习了) 开发知识为了不码字了,找了一篇介绍比较好的,这里我扣过来了,当 ...
- 缓存与ABP Redis Cache
缓存与ABP Redis Cache 为什么要用缓存 为什么要用缓存呢,说缓存之前先说使用缓存的优点. 减少寄宿服务器的往返调用(round-trips). 如果缓存在客户端或是代理,将减少对服务器的 ...
- 深入解析Java垃圾回收机制
引入垃圾回收 哪些内存需要回收? 引用计数法 可达性分析 如何回收 Marking 标记 Normal Deletion 清除 Deletion with Compacting 压缩 为什么需要分代收 ...
- springBoot系列教程02:mongodb的集成及使用
1.安装mongodb mongdb的安装很简单,只需要下载解压后运行mongod就好了 wget https://fastdl.mongodb.org/linux/mongodb-linux-x86 ...
- iBeacon硬件厂商和产品
最近项目比较忙,也没有时间写Blog. 这几天因为项目需要采购为数不小的iBeacon,所以花了点时间对市面上主要的iBeacon简单的调研总结了一下. 分享给需要买相关设备进行试验的小伙伴,数据不权 ...
- 这个接口管理平台 eoLinker 开源版部署指南你一定不想错过
本文主要内容是讲解如何在本地部署eoLinker开源版. 环境要求 1.PHP 5.5+ / PHP7+(推荐) 2.Mysql 5.5+ / Mariadb 5.5+ 3.Nginx(推荐) / A ...
- django序列化时使用外键的真实值
展示: 普通情况下序列化得到的外键的内容仅仅是id: ... { fields: { uat_date: "2015-07-25", statu: "CG", ...
- mysql本地訪问linuxserver,出现SQLSTATE[HY000] [1130] Host '127.0.0.1' is not allowed to connect to this
解决方式:网上看了说.更改mysql库的user表,加入一条host为%的数据就能够.可是还是不行. 后来,直接登录mysql.给訪问加权限就能够了. 运行 [root@iZ25p77kem7Z ~] ...