LoginSignup
2
0

More than 3 years have passed since last update.

AtCoder Beginner Contest 132をRubyで解いてみた

Last updated at Posted at 2019-06-29

A - Fifty-Fifty

s=gets.chomp.chars
f=0
4.times do |i|
  if s.count(s[i])!=2 then
    f=1
  end
end
puts f==0 ? 'Yes' : 'No'

各文字について2文字ずつ存在するか評価します。
解答例

B - Ordinary Number

N=gets.chomp.to_i
P=gets.chomp.split.map(&:to_i)
c=0

(N-2).times do |i|
  if ((P[i] < P[i+1]) && (P[i+1]< P[i+2])) || ((P[i] > P[i+1]) && (P[i+1]> P[i+2])) then
    c+=1
  end
end

puts c

p(0)~p(N-2)まで
p(i)<p(i+1)<p(i+2) または p(i)>p(i+1)>p(i+2) の条件の個数を算出します。
解答例

C - Divide the Problems

N=gets.chomp.to_i
D=gets.chomp.split.map(&:to_i).sort!
a=D[D.size/2]-D[D.size/2-1]
puts a

下記の2つが同値なため、ソートして N/2 番目の要素と N/2 - 1 番目の要素の差で求められます。解説PDFより

  • N/2 番目に難しい問題が「ARC 用の問題」、N/2 − 1 番目に難しい問題が「ABC 用の問題」となる こと
  • 「ARC 用の問題」の数と「ABC 用の問題」の数が同じになること

(解答例)[https://atcoder.jp/contests/abc132/submissions/6189546]

おわりに

C問題時間内に閃かなかった。。。
D問題以降は解けるようになったら追記します。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0