ACM 尋找衆多情侶堆中的兩個基佬

題目描述
When Wodex is running in his village to live down, He fainted by fumes. Fortunately, a mysterious man saved him...

Ten years later, he learned some skills from the mysterious man and have knowed who was personal enemy, just named Zero. So, he embarked on a journey of revenge.
But before it, the mysterious man wanted to test him.
Problem comes. The mysterious man prapared a maze where hided a brave sword. To get it, Wodex came in the maze.

There were many keys in the maze, each key had a number, only two keys can open the door to get brave sword. But there existed many keys' number were the same in pairs, if the number of keyA was the same as keyB, these two keys won't be the solution to the door. So, there would be left only last two different numbers of keys.

Can you help Wodex to find out these two keys' number?

輸入
Input until EOF.
First line will contain an even integer N (N < 100,000) means the number of whole keys.
And next line will contain N integers. Each integer means the number of each key.

輸出
What are they? Output the number of their keys from small to large.

樣例輸入
8
1 1 2 2 2 3 3 4
8
1 1 2 2 3 3 4 5

樣例輸出
2 4
4 5


分析

是不是想起了筷子大作戰這一題?
那題的意思就是有很多筷子,除了其中一隻,其他都是成對等長出現的。
直接異或,所有相同的數都會被抵消(異或運算中,相同位置值相等的結果爲0,就是說所有成對出現的都會最終成爲0),最後的結果自然是那個落單的數了。

然後,我們來看這題。
這題的意思是有很多情侶,每對情侶手握的牌號是一樣的,然後在這羣人中,有兩個人是搞基的,他們手裏握的牌子是不一樣的,請找出這兩個人,並從小到大輸出他們的牌號。
那麼問題稍微複雜了點,由一個單變成了兩個單。

如果是有兩個互不相同的數,我們可以把他分成兩個段,段名分別爲A,B。這裏爲了解釋方便,給出10個數,分別爲a,a,b,b,c,c,d,d,e,f;
 
現在我們知道,只要A段和B段能滿足上面的(1)所說的情形,即兩個不同的數(e,f分別在A,B段內)分別在不同的段內,其他的數成對出現在相同的段內,就可以仿照(1)得到結果。舉個例子A(a,a,b,b,e);B(c,c,d,d,f);
 
是不是覺得很難分呢?怎麼才能按照上面的形式分開成爲A,B呢?別急,注意咯,我上面說的是其他數成對出現,並不是一定要對半分,也可以是A(a,a,b,b,c,c,d,d,e);B(f);或者A(a,a,b,b,c,c,e);B(d,d,f);是不是有點頭緒了?沒有?別緊張,往下看,快搞定了
 
我們先來想想如何分e,f。首先,他們本身不相等,所以e^f的值一定不爲0,也就是說,e^f的值轉換成2進制,一定至少有一個1出現。好的,我們從右向左找第一次出現1的位置,將所有數中,這個位置是1的放進A,是0的放進B。OK,完全符合我們對AB段的要求,再從A,B中分別異或找出e,f不是問題!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章