1. 程式人生 > >HDU GT and sequence (數的乘積最大)

HDU GT and sequence (數的乘積最大)

問題描述
給出NN個整數。你要選擇至少一個數,使得你選的數的乘積最大。
保證任意選一些數相乘的絕對值都不會大於2^{63}-12631
輸入描述
第一行讀入一個數TT表示資料組數。
對於每組資料:
第一行是一個數NN,第二行是NN個整數。

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

hack時建議輸出最後一行的行末回車;每一行的結尾不要輸出空格。
輸出描述
對於每組資料,輸出一個數表示最大的乘積。
輸入樣例
1
3
1 2 3
輸出樣例
6
<pre name="code" class="html">#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll a[111];
int main()
{
    int i,j,t,n,n1,n2,n3;
    ll ans,temp;
    cin>>t;
    while(t--) {
        cin>>n;
        n1=n2=n3=0;
        for(i=0;i<n;i++) cin>>a[i];
        sort(a,a+n);
        for(i=0;i<n;i++) {
            if(a[i]<0) n1++;
            else if(a[i]==0) n2++;
            else n3++;
        }
        if(n==1) {
            cout<<a[0]<<endl;
            continue;
        }
        ans=1;
        for(i=0;i<n;i++) {
            if(a[i]==0) continue;
            ans=ans*a[i];
        }
        if(n2==n) {
            cout<<"0"<<endl;
            continue;
        }
        if(n3>0) {
            if(n1%2) ans=ans/a[n1-1];
            cout<<ans<<endl;
            continue;
        }
        if(n1==1) {
            cout<<"0"<<endl;
            continue;
        }
        if(n1%2) {
            cout<<ans/a[n1-1]<<endl;
            continue;
        }
        cout<<ans<<endl;
    }
    return 0;
}



相關推薦

HDU GT and sequence 乘積

問題描述 給出NN個整數。你要選擇至少一個數,使得你選的數的乘積最大。 保證任意選一些數相乘的絕對值都不會大於2^{63}-12​63​​−1。 輸入描述 第一行讀入一個數TT表示資料組數。

區間DP——整數劃分使乘積

#include <iostream> #include <cstring> #include <cmath> using namespace std; #de

[HDOJ3998] SequenceDP,

好的 hdoj 一個點 include type div c++ cnblogs span 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=3998 給數字,問LIS以及每一個數字只取一次,最多能取多少個LIS。 LIS直接O(

區間dp的總結乘積

logs 數組 str 區間dp 方程 style nbsp span 乘號 #include <iostream> #include <cstdio> #include <algorithm> #include <cstrin

HDU 6071 Lazy Running同餘短路

簡述題意:給你一個由四個節點組成的環,相鄰兩點間可達,求從節點2出發,回到節點2的不小於k的最短路徑的長度。 演算法:同餘最短路 難度:NOIP 題解: 假設我們將任意一條長度大於k的迴路(從2出發回到2)為可行路徑,那麼任意一條可行路徑加上2w一定還是可行路徑,所有可行方案中,

Girls and Boys 二分匹配—獨立團

     the second year of the university somebody started a study on the romantic relations between the students. The relation “roma

LeetCode:128. Longest Consecutive Sequence找出的連續子串

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Your algorithm should run in O(n) complexity. E

CF498C Array and Operations 質因數分解+

題目連結 qwq 最近突然想做網路流相關的整理啊 QWQ其實就是之前一段時間做的網路流的題 然後拿出來整理一下(這道並不是) 首先,我們很容易發現這個題目中,對於每一種關係,一定是除一個質因數是最優秀的。因為這樣可以保證你除的次數儘可能的多。 那麼我們首先第一步就是把所有的數都質

HDU 1005 Number Sequence矩陣乘法+快速冪

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.

HDU 1005 Number Sequence基礎矩陣快速冪

//HDU 1005 15MS 1424K #include <cstdio> #include <cstring> #include <cmath> #in

HDU 5950 Recursive sequence構造矩陣+矩陣乘法——2016ACM/ICPC亞洲區瀋陽站(重現賽)

傳送門 Recursive sequenceTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 98  

hdu 3572 Task Schedule網路流

/* 題目大意:工廠有m臺機器,需要做n個任務。對於一個任務i, 你需要花費一個機器Pi天,而且,開始做這個任務的時間要>=Si,完成這個任務的時間<=Ei。 對於一個任務,只能由一個機器來完成,一個機器同一時間只能做一個任務。 但是,一個任務可以分成幾段不連續

HDU 1005 Number Sequence找規律,思維

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 153140    Accepted Submissio

HDU 1045 Fire Net二分圖匹配

#include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<

用C語言實現:將組A中的內容和數組B中的內容進行交換組一樣

image pri 之前 es2017 sys 變量 ret 只需要 題目 之前我們已經完成了對兩個變量內容進行交換的程序,這兩道題目大同小異,不過是將兩數變成了兩數組。 可能我們會想:我們是不是需要第三個數組作為中間變量進行交換操作? 答案是no,我們只需要通過一個循環體

HDU 1560 DNA sequenceDNA序列

memory nes rgb each 12px align div printf c++ p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibr

hdu 1068 Girls and Boys 二分圖的匹配

ems can http scan stream show article main ble 題目鏈接:pid=1068">http://acm.hdu.edu.cn/showproblem.php?pid=1068 #include <iostre

hdu 6047 Maximum Sequence貪心

you 貪心 string -i n+1 blog html next about Description Steph is extremely obsessed with “sequence problems” that are usually seen on mag

HDU 6071 Lazy Running 同余短路 dij

href while mission more rst sam preview ems 表示 Lazy Running Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Jav

Array And String組和字符串

每一個 重新 存儲 pack 分享 nbsp ret 數據結構 con 1.實現一個算法,確定一個字符串的所有字符是否全都不同。假使不允許使用額外的數據結構,又該怎麽處理? public class UniqueChars { public static vo