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的更多相关文章

  1. POJ2479 Maximum sum[DP|最大子段和]

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 39599   Accepted: 12370 Des ...

  2. ural 1146. Maximum Sum

    1146. Maximum Sum Time limit: 0.5 secondMemory limit: 64 MB Given a 2-dimensional array of positive ...

  3. UVa 108 - Maximum Sum(最大连续子序列)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  4. 最大子矩阵和 URAL 1146 Maximum Sum

    题目传送门 /* 最大子矩阵和:把二维降到一维,即把列压缩:然后看是否满足最大连续子序列: 好像之前做过,没印象了,看来做过的题目要经常看看:) */ #include <cstdio> ...

  5. URAL 1146 Maximum Sum(最大子矩阵的和 DP)

    Maximum Sum 大意:给你一个n*n的矩阵,求最大的子矩阵的和是多少. 思路:最開始我想的是预处理矩阵,遍历子矩阵的端点,发现复杂度是O(n^4).就不知道该怎么办了.问了一下,是压缩矩阵,转 ...

  6. ural 1146. Maximum Sum(动态规划)

    1146. Maximum Sum Time limit: 1.0 second Memory limit: 64 MB Given a 2-dimensional array of positive ...

  7. UVa 10827 - Maximum sum on a torus

    题目大意:UVa 108 - Maximum Sum的加强版,求最大子矩阵和,不过矩阵是可以循环的,矩阵到结尾时可以循环到开头.开始听纠结的,想着难道要分情况讨论吗?!就去网上搜,看到可以通过补全进行 ...

  8. POJ 2479 Maximum sum 解题报告

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40596   Accepted: 12663 Des ...

  9. [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 ...

随机推荐

  1. 运行java web项目时报错:Several ports (8005, 8080, 8009) required

    运行java web项目时报错:Several ports (8005, 8080, 8009) required 如下图 之所以报上面的错误是因为安装Tomcat的时候,已经把端口8005,8080 ...

  2. 使用docker搭建Jenkins 及slave的配置

    安装Jenkins 使用docker docker run -d -p 8080:8080 -p 50000:50000 -v /opt/jenkins_home:/var/jenkins_home ...

  3. Azure 基础:使用 Traffic Manager 分流用户请求

    为了减少 web 服务器的宕机时间,同时也提高服务器的响应性能,我们往往部署多个站点并通过负载均衡来对外提供服务.Azure 提供的 Traffic Manager 服务属于负载均衡的一种,特点是工作 ...

  4. 在网站开发中经常用到的javaScript技术

     1>屏蔽功能类 1.1 屏蔽键盘所有键<script language="javascript"><!--function document.onkeyd ...

  5. Java学习之计算机基础(一)

    阅读本文大概需要 4 分钟 想要开始学习Java开发,需要掌握一些必要的计算机基础.如果你是计算机专业的人或者已经学过类似的课程,可以跳过这篇文章的阅读.计算机基础课程有很多,小编在大学里学过的课程就 ...

  6. poj 1035KINA Is Not Abbreviation

    题目链接:http://poj.org/problem?id=4054 本题的题意是在下面那部分待检验的单词中找到与之相对应的正确的代词,包含几种情况,一是全部字母相同,二是有一个字母不相同,三是多一 ...

  7. 遍历输出tuple元素的简洁方式(C++11)

    //遍历输出tuple元素的简洁方式(C++11) //Win32Con17_VS2017_01.cpp #include <iostream> #include <tuple> ...

  8. CSS3学习笔记--media query 响应式布局

    语法:@media screen and (min-width: 320px) and (max-width : 479px) media属性后面跟着的是一个 screen 的媒体类型(上面说过的十种 ...

  9. USACO Section 2.1 The Castle

    /* ID: lucien23 PROG: castle LANG: C++ */ /********************************************************* ...

  10. Linux已经全然统治了这个世界:反对开源社区愚不可及

    原文来自:http://readwrite.jp/archives/9977 不管一个企业多强大,它都不存在和开源社区抗衡的实力 十年前.Unix占有最快的计算机世界排名前10位的五席,以及超级计算机 ...