结束了一周的工作,是时候放空大脑了,让我们一起看漫画。
Atcoder beginner Contest 271 C. Manga
Time Limit: 4 sec / Memory Limit: 1024 MiB
Score : 300 points
Problem Statement
Takahashi is going to read a manga series "Snuke-kun" in 10**9 volumes.
Initially, Takahashi has N books of this series. The i-th book is Volume ai.Takahashi may repeat the following operation any number of (possibly zero) times only before he begins to read:
Do nothing if he has 1 or less books; otherwise, sell two of the books he has and buy one book of any volume instead.
Then, Takahashi reads Volume 1, Volume 2, Volume 3, …, in order. However, when he does not have a book of the next volume to read, he stops reading the series (regardless of the other volumes he has).
Find the latest volume of the series that he can read up to. If he cannot read any, let the answer be 0.
Constraints
1≤N≤3×10**5
1≤ai≤10**9
All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N
a1…aN
Output
Print the answer.
我的解
n = gets.to_i
num = gets.split(" ").map(&:to_i)
num.sort!
h = num.tally
x = n - h.length
len = h.length
y = h.keys
(1..n).each do |k|
unless h.key?(k)
if x >= 2
x -= 2
elsif x >= 1
if len >= 1
t = y.pop
h.delete(t)
len -= 1
x -= 1
else
puts k - 1
exit
end
elsif x == 0
if len >= 2
t1 = y.pop
t2 = y.pop
h.delete(t1)
h.delete(t2)
len -= 2
else
puts k - 1
exit
end
end
else
h.delete(k)
len -= 1
end
end
puts n