Ruby rbtree使用案例 (atcoder beginner contest 251)

在開頭插一個(gè)我使用紅黑樹最佳的實(shí)踐案例,ABC 431的第三題
Atcoder ABC Contest 431 (日常)

AtCoder Beginner Contest 251
C - Poem Online Judge

Time Limit: 2 sec / Memory Limit: 1024 MiB
Score : 300 points

Problem Statement
Poem Online Judge (POJ) is an online judge that gives scores to submitted strings.There were N submissions to POJ. In the i-th earliest submission, string Si was submitted, and a score of Ti was given. (The same string may have been submitted multiple times.)Note that POJ may not necessarily give the same score to submissions with the same string.
A submission is said to be an original submission if the string in the submission is never submitted in any earlier submission.
A submission is said to be the best submission if it is an original submission with the highest score. If there are multiple such submissions, only the earliest one is considered the best submission.
Find the index of the best submission.

Constraints
1≤N≤10**5
Si is a string consisting of lowercase English characters.Si has a length between 1 and 10, inclusive.
0≤Ti≤10**9
N and Ti are integers.

Input
Input is given from Standard Input in the following format:
N
S1 T1
S2 T2
?
SN TN

Output
Print the answer.
Atcoder允許使用rbtree gem來解題,且rbtree由c ext實(shí)現(xiàn),速度快
我的解

require "rbtree"

tree = RBTree.new
words = {}
n = gets.to_i
i = 0
n.times do
  s, t = gets.split
  i += 1
  unless words.key?(s)
    words[s] = 1
    unless tree.has_key?(t.to_i)
      tree[t.to_i] = i
    end
  end
end
puts tree.max[1]

建議Leetcode官方學(xué)習(xí)Atcoder允許調(diào)用rbtree,因?yàn)閍lgorithms gem里的CRBTreeMap存在bug。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • """1.個(gè)性化消息: 將用戶的姓名存到一個(gè)變量中,并向該用戶顯示一條消息。顯示的消息應(yīng)非常簡單,如“Hello ...
    她即我命閱讀 5,846評論 0 6
  • 1、expected an indented block 冒號后面是要寫上一定的內(nèi)容的(新手容易遺忘這一點(diǎn)); 縮...
    庵下桃花仙閱讀 1,159評論 1 2
  • 一、工具箱(多種工具共用一個(gè)快捷鍵的可同時(shí)按【Shift】加此快捷鍵選取)矩形、橢圓選框工具 【M】移動(dòng)工具 【V...
    墨雅丫閱讀 1,824評論 0 0
  • 跟隨樊老師和伙伴們一起學(xué)習(xí)心理知識提升自已,已經(jīng)有三個(gè)月有余了,這一段時(shí)間因?yàn)樘鞖獾脑蛐菡n,順便整理一下之前學(xué)習(xí)...
    學(xué)習(xí)思考行動(dòng)閱讀 1,189評論 0 2
  • 一臉憤怒的她躺在了床上,好幾次甩開了他抱過來的雙手,到最后還堅(jiān)決的翻了個(gè)身,只留給他一個(gè)冷漠的背影。 多次嘗試抱她...
    海邊的藍(lán)兔子閱讀 1,084評論 1 4

友情鏈接更多精彩內(nèi)容