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. “战术竞技类”外挂打击已开始!揭秘腾讯We Test游戏安全服务新动作!

    商业转载请联系腾讯WeTest获得授权,非商业转载请注明出处. 原文链接:http://wetest.qq.com/lab/view/353.html We Test导读 国服PUBG的游戏安全将由我 ...

  2. c# winform 窗体之间的传参

    说起winform程序中窗体之间的参数互传,大家找度娘会找到很多方法: 1.在窗体类中创建全局变量,类型为公开.静态的: 2.在窗体类中定义狗仔函数: 3.通过实践来船体参数: 这三种思路完全来自于霖 ...

  3. Python入门 - 生成随机数

    生成随机数是编程中经常用到的功能,下面讲几种常用的随机函数randint,uniform,  randrange: 一.生成随机整数 randint import random a = random. ...

  4. Python资料汇总(建议收藏)

    整理汇总,内容包括长期必备.入门教程.练手项目.学习视频. 一.长期必备. 1. StackOverflow,是疑难解答.bug排除必备网站,任何编程问题请第一时间到此网站查找. https://st ...

  5. NYOJ 2357: 插塔憋憋乐 贪心

    2357: 插塔憋憋乐 时间限制: 1 Sec  内存限制: 128 MB 提交: 82  解决: 18 [提交][状态][讨论版] 题目描述 众所不知,LLM是一位红警3大佬,打的非常厉害,但是曾经 ...

  6. appium+Linux环境安装配置

      背景:想要在Jenkins上跑appium的自动化测试,所以想要在Jenkins服务器(Linux远程)上安装appium服务,故而研究了一下appium+Linux下安装.虽然有无数的前辈踩过坑 ...

  7. 【算法】字典的诞生:有序数组 PK 无序链表

    参考资料 <算法(java)>                           — — Robert Sedgewick, Kevin Wayne <数据结构>       ...

  8. poj 2271HTML

    poj2271 HTML Description If you ever tried to read a html document on a Macintosh, you know how hard ...

  9. TCP的十一种状态与三次握手分析(有图)

    我们知道TCP是面向连接的,我们只知道有连接断开,其实内部还有一些比较复杂的状态.去了解各个状态之间的切换有助于我们更加深入的了解TCP.下面我们就来分析各个状态. 1.如下图示(图源百度)图中显示出 ...

  10. Linux下实现视频读取(三)---Buffer的准备和数据读取

    前面主要介绍的是:V4L2 的一些设置接口,如亮度,饱和度.曝光时间,帧数,增益.白平衡等.今天看看V4L2 得到数据的几个关键ioctl,Buffer的申请和数据的抓取. 1. 初始化 Memory ...