0
0

More than 1 year has passed since last update.

RubyでAtCoder ABC293(A, B, C)を解いてみた

Posted at

はじめに

Webエンジニアを目指して、RubyやRailsをいじってます。
今回は、RubyでAtCoder ABC293のA, B, Cを解きました。備忘録として解き方をまとめていきたいと思います。

A - Order Something Else

a-293.rb
n, a, b = gets.split.map(&:to_i)
d = gets.split.map(&:to_i).sort

discounted_price = d[0] + b
puts a <= discounted_price ? a : discounted_price

B - Strictly Superior

b-293.rb
n, _ = gets.split.map(&:to_i)
array = Array.new(n){ gets.split.map(&:to_i) }

array.permutation(2) do |pi, pj|
  next if pi[0] < pj[0]
  next if (pi[2..] - pj[2..]).size > 0

  if pi[0] > pj[0] || (pj[2..] - pi[2..]).size > 0
    puts "Yes"
    exit
  end 
end
puts "No"

解説

3つの条件について全探索することで判定することができます。

C - Reversible

c-293.rb
n = gets.to_i
array = Array.new(n){ gets.chomp.chars }.uniq

hash = {}
array.each do
  next if hash[_1] || hash[_1.reverse]
  hash[_1] = true
end
puts hash.size

解説

連想配列を用意して、arrayの各要素についてその前までにそれ自身または反転させたものがあったかどうかを判定し、その結果で適宜要素をhashに追加していくことで答えが求まります。

combinationメソッドとcountメソッドを使ってもいけるか試しましたが、2つTLEしてしまいました...

0
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
0
0