周六晚上吃完晚飯后,和家人寒暄了幾句,看了會兒視頻,不知不覺就到了9點40。正好ABC結(jié)束了,于是我又和往常一樣,再一次repeat ABC。11月26日忍不住看了一眼第四題,真是難得,哈希表就可以做,于是在這里補個檔。至于EFG什么時候能做,估計得到50歲,哈哈。
AtCoder Beginner Contest 433
A - Happy Birthday! 4
Time Limit: 2 sec / Memory Limit: 1024 MiB
Score : 100 points
Problem Statement
You are given positive integers X,Y,Z.Takahashi and Aoki are currently X years old and Y years old, respectively.Takahashi and Aoki age by 1 year simultaneously on every January 1st.
Determine whether there is a moment in the future (including this year) when Takahashi's age becomes exactly Z times Aoki's age.
Constraints
1≤X,Y≤100
2≤Z≤10
All input values are integers.
Input
The input is given from Standard Input in the following format:
X Y Z
Output
Output Yes if there is a moment in the future when Takahashi's age becomes exactly
Z times Aoki's age, and No otherwise.
My solution
x, y, z = gets.split(" ").map(&:to_i)
if x >= z * y && (x - z * y) % (z - 1) == 0
puts "Yes"
else
puts "No"
end
B - Nearest Taller
Time Limit: 2 sec / Memory Limit: 1024 MiB
Score : 200 points
Problem Statement
There are N people standing in a row from left to right. The i-th person from the left (1≤i≤N) is called person i. The height of person i (1≤i≤N) is Ai .For each i=1,2,…,N, determine whether there exists a person to the left of person i who is taller than person i, and if so, find the person standing closest to person i among them.
Constraints
1≤N≤100
1≤Ai≤100
All input values are integers.
Input
The input is given from Standard Input in the following format:
N
A1 A2… AN
Output
Output N lines.The i-th line (1≤i≤N) should contain ?1 if there is no person to the left of person i who is taller than person i, and otherwise, the number representing the person standing closest to person i among such people.
My solution
n = gets.to_i
a = gets.split(" ").map(&:to_i)
ans = [-1]
for i in 1...n
flag = false
(i-1).downto(0) do |j|
if a[j] > a[i]
ans << j+1
flag = true
break
end
end
unless flag
ans << -1
end
end
puts ans
C - 1122 Substring 2
Time Limit: 2 sec / Memory Limit: 1024 MiB
Score : 300 points
Problem Statement
You are given a string S consisting of digits.A string T is called a 1122-string if it satisfies all of the following conditions. (The definition is the same as in Problem F.)T is a non-empty string consisting of digits.∣T∣ is even, where ∣T∣ denotes the length of string T.All characters from the 1-st through the 2∣T∣ -th character of T are the same digit.All characters from the ( 2∣T∣ +1)-th through the ∣T∣-th character of T are the same digit.Adding 1 to the digit of the 1-st character of T gives the digit of the ∣T∣-th character.For example, 1122, 01, and 444555 are 1122-strings, but 1222 and 90 are not 1122-strings.Find the number of substrings of S that are 1122-strings.Two substrings are counted separately if they are extracted from different positions, even if they are identical as strings.
Constraints
S is a string consisting of digits with length between 1 and 10**6 , inclusive.
Input
The input is given from Standard Input in the following format:
S
Output
Output the number of non-empty substrings of S that are 1122-strings.
My solution
s = gets.chomp
h = { "01" => [], "12" => [], "23" => [], "34" => [], "45" => [], "56" => [], "67" => [], "78" => [], "89" => [] }
i = 0
s.chars.each_cons(2) do |x, y|
if h.key?([x, y].join)
h[[x, y].join] << i
end
i += 1
end
cnt = 0
h.each_pair do |k, v|
if v.length > 0
cnt += v.length
v.each do |v1|
i = 1
while true
if v1 - i >= 0 && v1 - i <= s.length - 1 && v1 + i + 1 >= 0 && v1 + i + 1 <= s.length - 1
if s[v1 - i] == k[0] && s[v1 + i + 1] == k[1]
cnt += 1
else
break
end
else
break
end
i += 1
end
end
end
end
puts cnt
D - 183183
Time Limit: 2 sec / Memory Limit: 1024 MiB
Score : 400 points
Problem Statement
For positive integers x,y, define f(x,y) as follows:
The value obtained by interpreting x,y in decimal notation without leading zeros as strings, concatenating them in this order to obtain a string S, and then interpreting S as an integer in decimal notation.
For example, f(12,3)=123 and f(100,40)=10040.You are given positive integers N,M and a sequence of N positive integers A=(A1 ,A2 ,…,AN ).Find the number of pairs of integers (i,j) that satisfy all of the following conditions.1≤i,j≤N f(Ai ,Aj) is a multiple of M.
Constraints
1≤N≤2×10**5
1≤M≤10**9
1≤Ai ≤10**9
All input values are integers.
Input
The input is given from Standard Input in the following format:
N M
A1 A2 … AN
Output
Output the number of pairs of integers (i,j) that satisfy all the conditions.
My solution
n, m = gets.split(" ").map(&:to_i)
num = gets.split(" ").map(&:to_i)
h = {}
h1 = (1..10).to_h { |j| [j, Hash.new(0)] }
num.each do |x|
for i in 1..10
unless h.key?(i)
h[i] = {}
t = (x * (10 ** i)) % m
unless h[i].key?(t)
h[i][t] = 1
else
h[i][t] += 1
end
else
t = (x * (10 ** i)) % m
unless h[i].key?(t)
h[i][t] = 1
else
h[i][t] += 1
end
end
end
y = x % m
h1[x.to_s.length][y] += 1
end
cnt = 0
h1.each_pair do |k, v|
v.each_pair do |k1, v1|
if k1 == 0
if h[k].key?(0)
cnt += h[k][0] * v1
end
else
if h[k].key?(m - k1)
cnt += h[k][m - k1] * v1
end
end
end
end
puts cnt