您的位置:首页 > 产品设计 > UI/UE

hdoj5501GT and sequence

2016-01-25 00:27 357 查看


GT and sequence

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

Total Submission(s): 2444    Accepted Submission(s): 556


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 263−1.

 

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.

1≤T≤1000
1≤N≤62

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

BestCoder Round #60

 
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=10010;
long long num[70];
int main()
{
int t,i,j,k,n,m;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(i=0;i<n;++i){
scanf("%lld",&num[i]);
}
sort(num,num+n);
long long ans1=1,ans2=1,pos,cnt=0;
bool flag=false;
for(i=n-1;i>=0;--i){
if(num[i])flag=true;
if(num[i]>0)ans1*=num[i];
else if(num[i]<0)ans2*=num[i];
if(num[i]<0)cnt++;
if(i!=n-1&&num[i]<0&&num[i+1]>=0)pos=i;
}
if(num[n-1]<0)pos=n-1;
if(!flag){
printf("0\n");
}
else if(ans2>0){
printf("%lld\n",ans1*ans2);
}
else {
if(cnt==1){
if(num[n-1]==0){
printf("0\n");
}
else if(num[n-1]<0){
printf("%lld\n",num[n-1]);
}
else printf("%lld\n",ans1);
}
else
printf("%lld\n",ans1*ans2/num[pos]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hdoj5501GT and seque