HDU 5504 GT and sequence(排除陷阱就是正解)——BestCoder Round #60

本文解析了GTandsequence题目,给出了高效的解题思路及代码实现。面对整数序列,如何选择元素使其乘积最大化?文章通过具体示例阐述了解决方案,并提供了详细的程序代码。

GT and sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


Problem Description
You are given a sequence of  N  integers.

You should choose some numbers(at least one),and make the product of them as big as possible.

It guaranteed that **the absolute value of** any product of the numbers you choose in the initial sequence will not bigger than  2631 .
 

Input
In the first line there is a number  T  (test numbers).

For each test,in the first line there is a number  N ,and in the next line there are  N  numbers.

1T1000
1N62

You'd better print the enter in the last line when you hack others.

You'd better not print space in the last of each line when you hack others.
 

Output
For each test case,output the answer.
 

Sample Input
  
1 3 1 2 3
 

Sample Output
  
6
 

Source
 

/************************************************************************/

附上该题对应的中文题

GT and sequence

 
 
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 65536/65536 K (Java/Others)
问题描述
给出NN个整数。你要选择至少一个数,使得你选的数的乘积最大。
保证任意选一些数相乘的绝对值都不会大于2^{63}-12631
输入描述
第一行读入一个数TT表示数据组数。
对于每组数据:
第一行是一个数NN,第二行是NN个整数。

1 \leq T \leq 10001T1000
1 \leq N \leq 621N62

hack时建议输出最后一行的行末回车;每一行的结尾不要输出空格。
输出描述
对于每组数据,输出一个数表示最大的乘积。
输入样例
1
3
1 2 3
输出样例
6
/****************************************************/

出题人的解题思路:

1001 GT and sequence

注意先特判00的情况:如果读入的数据有00,那么去掉所有的00且最后答案和00取一个max。

剩下的正数显然全部乘起来比较优。

对于负数的话,如果个数是奇数个我们就去掉绝对值最小的那一个,然后全部乘起来即可。

这题就不多说了,就是坑比较多,出题人挖了这么多坑,只能由我们自己来填,与其说得再多,不如提供几组数据测试吧
INPUT
1
1
0
OUTPUT
0

INPUT
1
1
-1
OUTPUT
-1

INPUT
1
2
1 -1
OUTPUT
1

INPUT
1
3
0 0 0
OUTPUT
0

INPUT
1
3
-1 0 0
OUTPUT
0

INPUT
1
3
-1 0 2
OUTPUT
2

INPUT
1
5
-2 -3 2 4 1
OUTPUT
48

INPUT
1
5
-2 -3 -5 4 2
OUTPUT
120
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
#define inf 10000000000
using namespace std;
const int N = 100;
const int mod = 2009;
__int64 s[N];
int main()
{
    int t,n,i,k,flag;
    __int64 ans,Max;
    scanf("%d",&t);
    while(t--)
    {
        ans=flag=k=0;
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%I64d",&s[i]);
            if(s[i]==0)
            {
                flag++;
                continue;
            }
            if(s[i]<0)
            {
                if(k)
                    Max=max(Max,s[i]);
                else
                    Max=s[i];
                k++;
            }
            if(i==flag)
                ans=s[i];
            else
                ans*=s[i];
        }
        if(k%2&&n!=1&&!(k==1&&flag==n-k))
            ans/=Max;
        if(flag>0&&(ans<=0||n==1))
            ans=0;
        printf("%I64d\n",ans);
    }
    return 0;
}
菜鸟成长记
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值