Codeforces 469A I Wanna Be the Guy 我想要成為那個人

難易度 : 800

英文原文如下

There is a game called “I Wanna Be the Guy”, consisting of n levels. Little X and his friend Little Y are addicted to the game. Each of them wants to pass the whole game.

Little X can pass only p levels of the game. And Little Y can pass only q levels of the game. You are given the indices of levels Little X can pass and the indices of levels Little Y can pass. Will Little X and Little Y pass the whole game, if they cooperate each other?

中文翻譯

有一款名為「I Wanna Be the Guy」的遊戲,包含 n 個關卡。小 X 和他的朋友小 Y 都對這個遊戲入迷。他們都希望可以通過整個遊戲。

小 X 只能通過 p 個關卡,而小 Y 只能通過 q 個關卡。你已經知道了小 X 能通過的關卡索引和小 Y 能通過的關卡索引。如果他們互相合作,他們能通過整個遊戲嗎?

輸入

The first line contains a single integer n (1 ≤  n ≤ 100).

The next line contains an integer p (0 ≤ p ≤ n) at first, then follows p distinct integers a1, a2, …, ap (1 ≤ ai ≤ n). These integers denote the indices of levels Little X can pass. The next line contains the levels Little Y can pass in the same format. It’s assumed that levels are numbered from 1 to n.

第一行包含了一個整數 n (1 ≤  n ≤ 100)。

接下來包含了一個整數 p (0 ≤ p ≤ n),以及接下來的 p個獨立整數 a1, a2, …, ap (1 ≤ ai ≤ n)。 這些整數代表著小X可以通過的關卡,接下來一行則是以相同的樣式顯示小Y的。假設關卡的編號從 1 到 n。

輸出

If they can pass all the levels, print “I become the guy.”. If it’s impossible, print “Oh, my keyboard!” (without the quotes).

要是他們可以通過所有關卡,印出 “I become the guy.”。 要是不可能通過的話,則印出 “Oh, my keyboard!” (不需要引號)

範例

輸入4
3 1 2 3
2 2 4
輸出I become the guy.
輸入4
3 1 2 3
2 2 3
輸出Oh, my keyboard!

筆記

In the first sample, Little X can pass levels [1 2 3], and Little Y can pass level [2 4], so they can pass all the levels both.

In the second sample, no one can pass level 4.

在第一個範例中,小X可以通過關卡 [1 2 3],小Y 可以通過關卡 [2 4],所以他們合作可以通過所有關卡(共4關)

在第二個範例中,則沒有人可以通過關卡 4


解題思路

這一題應該從看完題目的那一刻就知道可以輕鬆的用HashSet來儲存可以通關的關卡了

利用HashSet不會添加重複值的特性,可以寫出很簡單的程式碼

C#解決方案

方案1 – HashSet

int n = int.Parse(Console.ReadLine());

HashSet<string> hSet = new HashSet<string>();
for (int k = 0; k < 2; k++)
{
    string[] arr = Console.ReadLine().Split(' ').ToArray();
    
    for (int i = 1; i < arr.Length; i++)
    {
        hSet.Add(arr[i]);
    }
}


Console.WriteLine(hSet.Count == n ? "I become the guy." : "Oh, my keyboard!");

要是能通關的數量等於關卡總數,就代表他們能通過所有關卡


結論

HashSet真的很好用,但我怎麼不記得大學教過雜湊哈哈哈

另外,最近剛寫了網站2023的年度回顧,大家有興趣可以移駕過去看看 ➡2023年度網站回顧

🧡如果文章有幫上你的忙,那是我的榮幸

🧡收藏文章或幫我點個廣告,那都是對我的支持 (幫我多多分享也大感謝!!)

✅如有任何疑問,歡迎透過留言或messenger讓我知道 !

題目連結 : Problem – 469A – Codeforces

一些其他的Codeforces文章

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *