HDU1029 False coin

來源:互聯網
上載者:User
                                                                                                                                        False
coin
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15838   Accepted: 4408

Description

The "Gold Bar"bank received information from reliable sources that in their last group of N coins exactly one coin is false and differs in weight from other coins (while all other coins are equal in
weight). After the economic crisis they have only a simple balance available (like one in the picture). Using this balance, one is able to determine if the weight of objects in the left pan is less than, greater than, or equal to the weight of objects in the
right pan.
In order to detect the false coin the bank employees numbered all coins by the integers from 1 to N, thus assigning each coin a unique integer identifier. After that they began to weight various groups of coins by placing equal numbers of coins in the left
pan and in the right pan. The identifiers of coins and the results of the weightings were carefully recorded.
You are to write a program that will help the bank employees to determine the identifier of the false coin using the results of these weightings.

Input

The first line of the input file contains two integers N and K, separated by spaces, where N is the number of coins (2<=N<=1000 ) and K is the number of weightings fulfilled (1<=K<=100). The following
2K lines describe all weightings. Two consecutive lines describe each weighting. The first of them starts with a number Pi (1<=Pi<=N/2), representing the number of coins placed in the left and in the right pans, followed by Pi identifiers of coins placed in
the left pan and Pi identifiers of coins placed in the right pan. All numbers are separated by spaces. The second line contains one of the following characters: '<', '>', or '='. It represents the result of the weighting:
'<' means that the weight of coins in the left pan is less than the weight of coins in the right pan,
'>' means that the weight of coins in the left pan is greater than the weight of coins in the right pan,
'=' means that the weight of coins in the left pan is equal to the weight of coins in the right pan.

Output

Write to the output file the identifier of the false coin or 0, if it cannot be found by the results of the given weightings.

Sample Input

5 32 1 2 3 4<1 1 4=1 2 5=

Sample Output

3

Source

Northeastern Europe 1998



解題思路:本題為類比邏輯題,只要想通怎麼確定硬幣真假即可解得。

在稱量中,有3種情況,> ,< =,其實,總體的情況只有兩種,天平平行還是不平行。

(1)若天平是平行的,毫無疑問,所有參與本次稱量的硬幣都是真硬幣。因為題目說了,假硬幣只有一個,且真硬幣的品質都相等。

(2)若天平不平行,就要分兩邊討論了。若硬幣在重的一邊且尚未確定真假,如果該硬幣在以前的稱量中曾經出現在輕的一邊,則該硬幣可以確定為真硬幣(狀態置為102)。因為同一個硬幣的重量是不變的,曾出現在輕的一邊,後面又出現在重的一邊,只能說,致使天平傾斜的硬幣不是它,所以,該硬幣是真的。否則記錄它曾經出現在重的一邊(狀態++,置為正值)。對於在本次稱量中處於輕的一邊的硬幣,同理處理即可。

注意:如果出現情況(2),有一個隱秘的資訊一定要挖出來,銀行說了,假硬幣只有1個。若出現情況(2),表示,那個品質和真硬幣不同的假硬幣,已經出現在稱量中了。也就是說,所有沒有參與本次稱量的硬幣,都能確定是真的。

最後,逐個檢索硬幣的狀態值,若值為102,即該硬幣已確定為真硬幣。否則,表示該硬幣尚未確定真假。若未確定真假的硬幣只有一個,則說明該硬幣就是假硬幣,輸出該硬幣的編號。否則,無法確定假硬幣,輸出“0”;


#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int main(){    int co[1005];    //記錄硬幣情況,在稱量後記錄稱量結果,    int n,k,i,j,x,bl[203];    char a[3];  //用於讀入符號    while(scanf("%d%d",&n,&k)!=EOF)    {        int no=0;        memset(co,0,sizeof(co));   //狀態記錄數組置零        for(j=0;j<k;j++)        {            scanf("%d",&x);  //讀入天平兩遍硬幣的個數            x*=2;            for(i=0;i<x;i++)                scanf("%d",&bl[i]);   //讀入所有參加稱量的硬幣號碼            scanf("%s",a);   //讀入符號,a[0]處儲存的就是所讀入夫人符號            if(a[0]=='=')   //若天平是平的,所有參與稱量的硬幣都是真的                for(i=0;i<x;i++)                    co[bl[i]]=102;    //狀態標記置為102,表示該硬幣i已經檢測過關,為真硬幣            else if(a[0]=='<')  //若天平是右傾的            {                no++;                x/=2;                for(i=0;i<x;i++)   //天平左邊的硬幣相對比較輕                {                    if(co[bl[i]]!=102)   //如果該硬幣為確定為真硬幣                    {                        if(co[bl[i]]>0)   //若該硬幣在前面的稱量中曾經重過,則它是真的硬幣(同一個硬幣品質是不會變的)                            co[bl[i]]=102;                        else co[bl[i]]--;   //否則,記錄為有有一次稱量中,硬幣在輕的一方                    }                }                x=2*x;                for(i=x/2;i<x;i++)        //天平右邊的硬幣相對比較重                {                    if(co[bl[i]]!=102)//如果該硬幣為確定為真硬幣                    {                        if(co[bl[i]]<0) //若該硬幣在前面的稱量中曾經輕過,則它是真的硬幣(同一個硬幣品質是不會變的)                            co[bl[i]]=102;                        else                            co[bl[i]]++;//否則,記錄為有有一次稱量中,硬幣在重的一方                    }                }                int u;                for(i=1;i<=n;i++)//由於假硬幣只有一枚,出現了天平不平衡的情況,所有未參與本次稱量的硬幣都是真硬幣                {                    for(u=0;u<x;u++)                    {                        if(i==bl[u])                        break;                    }                    if(u==x)                        co[i]=102;                }            }            else if(a[0]=='>')            {                no++;                for(i=x/2;i<x;i++)                {                    if(co[bl[i]]!=102)                    {                        if(co[bl[i]]>0)                            co[bl[i]]=102;                        else co[bl[i]]--;                    }                }                x/=2;                for(i=0;i<x;i++)                {                    if(co[bl[i]]!=102)                    {                        if(co[bl[i]]<0)                            co[bl[i]]=102;                        else                            co[bl[i]]++;                    }                }                x*=2;                int u;                for(i=1;i<=n;i++)                {                    for(u=0;u<x;u++)                    {                        if(i==bl[u])                        break;                    }                    if(u==x)                        co[i]=102;                }            }        }        int j=0;        for(i=1;i<=n;i++)        {            if(co[i]!=102)    //若硬幣i不能確定為真硬幣,則它可能是假的            {                if(j==0)                     j=i;                else     //若在這個不能確定真假的硬幣之前,有硬幣是不能確定真假的,則無法確定哪個是假硬幣                {                    j=0;                    break;                }            }        }        printf("%d\n",j);    }    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.