GT and sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 219 Accepted Submission(s): 60
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 .
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.
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
Recommend
解析:用flag记录有没有0,如果有0,在之后的结果中与 0 取一个最大值。
直接排序,对于a[i],如果a[i]>0,那么ans*=a[i];
如果a[i]==0,那么不作处理;
如果a[i]<0并且a[i+1]<0,那么ans*=a[i]*a[i+1],否则不作处理。
代码:
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=62;
LL a[maxn+10];
int main()
{
freopen("1.in","r",stdin);
LL t,n,i,ans; bool flag;
scanf("%I64dd",&t);
while(t--)
{
scanf("%I64d",&n);
for(i=1;i<=n;i++)scanf("%I64d",&a[i]);
sort(a+1,a+n+1),flag=a[0]=0;
for(i=1;i<=n;i++)
{
if(a[i]<0 && (i+1<=n && a[i+1]<0))
a[++a[0]]=a[i],a[++a[0]]=a[i+1],i++;
if(a[i]>0)a[++a[0]]=a[i];
if(a[i]==0)flag=1;
}
if(a[0]==0)
{
if(flag)ans=0;
else ans=a[1];
}
else
for(ans=1,i=1;i<=a[0];i++)ans*=a[i];
printf("%I64d\n",ans);
}
return 0;
}