hdu5504 GT and sequence(BestCoder Round #60 )

GT and sequence

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

Total Submission(s): 334 Accepted Submission(s): 85

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 <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
#define ll long long
#define CL(a) memset(a,0,sizeof(a))

int main()
{
    int T;
    ll n,x,sum;
    cin>>T;
    while(T--)
    {
        cin>>n;
        sum = 0;
        int k=0,re=0;
        ll maxx = -INF;
        for(int i=0; i<n; i++)
        {
            cin>>x;
            if(x == 0) {re++; continue;}
            if(x < 0)
            {
                k++;
                maxx = max(maxx, x);
            }
            if(i == re) sum = x;
            else sum *= x;
        }
        //cout<<re<<" "<<k<<endl;
        if(k%2&&n!=1&&!(k==1&&re+k==n)) sum /= maxx;
        if(re>0&&(sum<0||n==1)) sum = 0;

        cout<<sum<<endl;
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-03 21:51:23

hdu5504 GT and sequence(BestCoder Round #60 )的相关文章

HDU 4908 BestCoder Sequence(BestCoder Round #3)

Problem Description: Mr Potato is a coder.Mr Potato is the BestCoder. One night, an amazing sequence appeared in his dream. Length of this sequence is odd, the median number is M, and he named this sequence as Bestcoder Sequence. As the best coder, M

hdu 5204 Rikka with sequence(BestCoder Round #37)

Rikka with sequence                                                        Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 378    Accepted Submission(s): 75 Problem Description As we know, Rikk

HDU 5063 Operation the Sequence(BestCoder Round #13)

Problem Description: You have an array consisting of n integers: a1=1,a2=2,a3=3,…,an=n. Then give you m operators, you should process all the operators in order. Each operator is one of four types:Type1: O 1 call fun1();Type2: O 2 call fun2();Type3:

HDU 5805 - NanoApe Loves Sequence (BestCoder Round #86)

先找相邻差值的最大,第二大,第三大 删去端点会减少一个值, 删去其余点会减少两个值,新增一个值,所以新增和现存的最大的值比较一下取最大即可 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 using namespace std; 5 #define LL long long 6 const int N = 100005; 7 int t, n, p1, p2, p3; 8 LL a[N]; 9

HDU 5806 - NanoApe Loves Sequence Ⅱ (BestCoder Round #86)

若 [i, j] 满足, 则 [i, j+1], [i, j+2]...[i,n]均满足 故设当前区间里个数为size, 对于每个 i ,找到刚满足 size == k 的 [i, j], ans += n - j + 1 . i++ 的时候看看需不需要size-- 就可以更新了. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 #define

HDU 4932 Miaomiao&#39;s Geometry(BestCoder Round #4)

Problem Description: There are N point on X-axis . Miaomiao would like to cover them ALL by using segments with same length. There are 2 limits: 1.A point is convered if there is a segments T , the point is the left end or the right end of T.2.The le

HDU-5086-Revenge of Segment Tree (BestCoder Round #16)

Revenge of Segment Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 205    Accepted Submission(s): 83 Problem Description In computer science, a segment tree is a tree data structure for st

HDU-5108-Alexandra and Prime Numbers (BestCoder Round #19)

Alexandra and Prime Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 319    Accepted Submission(s): 120 Problem Description Alexandra has a little brother. He is new to programming. One

HDU 5067 Harry And Dig Machine(BestCoder Round #14)

Problem Description: As we all know, Harry Porter learns magic at Hogwarts School. However, learning magical knowledge alone is insufficient to become a great magician. Sometimes, Harry also has to gain knowledge from other certain subjects, such as