我们一起看漫画

结束了一周的工作,是时候放空大脑了,让我们一起看漫画。
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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • “It is my belief that magic would be a greater power for ...
    zzz雪人阅读 5,486评论 0 2
  • Ray Tracing: The Rest of Your Life Peter Shirley[https://...
    lokichen阅读 1,605评论 0 0
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,160评论 0 10
  • The date was August 1, 1941. World War II had been raging...
    fei老师阅读 3,237评论 0 0
  • Overview The ccxt library is a collection of available cr...
    郭蝈儿蝈儿阅读 9,247评论 0 1