[ACM] ZOJ 3844 Easy Task (類比+雜湊),zoj3844

來源:互聯網
上載者:User

[ACM] ZOJ 3844 Easy Task (類比+雜湊),zoj3844

Easy TaskTime Limit: 2 Seconds      Memory Limit: 65536 KB

You are given n integers. Your task is very easy. You should find the maximum integer a and the minimum integer b among these n integers. And then you should replace both a and bwith a-b. Your task will not be finished unless all the integers are equal.

Now the problem come, you want to know whether you can finish you task. And if you can finish the task, you want to know the final result.

Input

The first line of the input contain an integer T(T≤ 20) indicates the number of test cases.

Then T cases come. Each case consists of two lines. The first line is an integer n(2≤ n≤ 10) as the problem described. The second line contains n integers, all of them are no less than -100000 and no more than 100000.

Output

For each case you should print one line. If you can finish your task, you should print one of the n integers. Otherwise, you should print "Nooooooo!"(without quotes).

Sample Input
231 2 325 5
Sample Output
25
Author: CHEN, Zemin
Source: ZOJ Monthly, January 2015

解題思路:

題意為給定n個數(2<=n<=10),每個數的範圍為 -100000到100000,有一種操作:找到這n個數中最大的數max,最小的數min,把這兩個數都替換為 max-min , 重複這樣的操作,直到這n個數完全相同,如果可以達到這樣的目的,就輸出最後那n個數中的其中一個(每個數都一樣),如果不能,則輸出 Nooooooo!。

n的範圍很小,直接按照題意排序類比即可,一開始覺得任何資料都可以達到目的,沒有輸出Nooooooo!的情況,寫了一發,交上去,果斷WA.後來和二師兄討論,應該選項組才對,一旦出現相同的狀態,那麼就不能達到目的,輸出Nooooooo!。方法為雜湊尋找,每一個狀態用包含這n個數的結構體來儲存,關鍵字是這n個數的和,以n個數的和對一個大素取餘作為關鍵字,10個數的最大和不超過100000*10,所以開闢這麼大的結構體類型的vector數組,當出現一個新的狀態時,就去該狀態的和取餘後的索引中的那些狀態去比較,這樣可以減少比較次數,因為一個狀態和另一個狀態相同,它們分別的元素之和取餘後相等是大前提。

注意:n個元素和可能是負數,建立索引要加上mod變為正數.

代碼:

#include <iostream>#include <stdio.h>#include <algorithm>#include <string.h>#include <stdlib.h>#include <cmath>#include <iomanip>#include <vector>#include <set>#include <map>#include <stack>#include <queue>#include <cctype>using namespace std;#define ll long longconst int mod=100000*10+1;const int inf=0x3f3f3f3f;const int maxn=12;int num[maxn];int n;struct Node{    int a[maxn];};vector<Node>vc[mod+10];int main(){    int t;    cin>>t;    while(t--)    {        for(int i=0;i<=mod;i++)            vc[i].clear();        Node tp;        cin>>n;        int sum=0;        for(int i=0;i<n;i++)        {            cin>>num[i];            sum+=num[i];        }        tp.a[n]=sum;        sort(num,num+n);        if(num[0]==num[n-1])        {            cout<<num[0]<<endl;            continue;        }        for(int i=0;i<n;i++)            tp.a[i]=num[i];        vc[(tp.a[n]+mod)%mod].push_back(tp);        int ans=inf;        while(1)        {            int cha=num[n-1]-num[0];            sum=sum-num[n-1]-num[0]+2*cha;//新的sum            num[0]=cha,num[n-1]=cha;            sort(num,num+n);            if(num[0]==num[n-1])            {                ans=num[0];                break;            }            tp.a[n]=sum;            for(int i=0;i<n;i++)                tp.a[i]=num[i];            bool ok=0;            int idx=(tp.a[n]+mod)%mod;//去相應的索引中去找看是否有相同的狀態            for(int i=0;i<vc[idx].size();i++)            {                    int j;                    for(j=0;j<n;j++)                    {                        if(tp.a[j]!=vc[idx][i].a[j])                            break;                    }                    if(j==n)                    {                        ok=1;//找到了!                        break;                    }            }            if(ok==1)                break;            vc[idx].push_back(tp);        }        if(ans!=inf)            cout<<ans<<endl;        else            cout<<"Nooooooo!"<<endl;    }    return 0;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.