Skip to content

Instantly share code, notes, and snippets.

@whatalnk
Created July 30, 2017 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whatalnk/4c4cd32fe7763ce8f2490f3d5d4bef18 to your computer and use it in GitHub Desktop.
Save whatalnk/4c4cd32fe7763ce8f2490f3d5d4bef18 to your computer and use it in GitHub Desktop.
AtCoder Chokudai Speed Run #001
n = gets.chomp.to_i
arr = gets.chomp.split(" ").map(&:to_i)
puts arr.max
n = gets.chomp.to_i
arr = gets.chomp.split(" ").map(&:to_i)
puts arr.inject(:+)
n = gets.chomp.to_i
arr = gets.chomp.split(" ").map(&:to_i)
puts arr.join(",")
n = gets.chomp.to_i
arr = gets.chomp.split(" ").map(&:to_i)
puts arr.sort.join(" ")
n = gets.chomp.to_i
arr = gets.chomp.split(" ").map(&:to_i)
puts arr.index(1) + 1
n = gets.chomp.to_i
arr = gets.chomp.split(" ").map(&:to_i)
ret = 1
amax = arr[0]
(1...n).each do |i|
ret += 1 if amax < arr[i]
amax = [amax, arr[i]].max
end
puts ret
n = gets.chomp.to_i
arr = gets.chomp.split(" ").join("").to_i
puts arr % 1000000007
n = gets.chomp.to_i
arr = gets.chomp.split(" ").map(&:to_i)
arr2 = Array.new(n+1, 10**6)
ret = 0
arr.each do |a|
i = arr2.bsearch_index{|x| x > a}
if i.nil? then
i = 0
end
arr2[i] = a
ret = [ret, i].max
end
puts ret + 1
n = gets.chomp.to_i
arr = gets.chomp.split(" ").map(&:to_i)
cumsum = [0]
n.times do |i|
cumsum << cumsum[i] + arr[i]
end
ret = 0
n.times do |i|
((i+1)..n).each do |j|
asum = cumsum[j] - cumsum[i]
if asum > n then
break
elsif asum == n then
ret += 1
end
end
end
puts ret
n = gets.chomp.to_i
arr = gets.chomp.split(" ").map(&:to_i)
class BIT
attr_reader :bit
def initialize(n)
@n = n
@bit = Array.new(@n + 1, 0)
end
def sum(i)
s = 0
while i > 0
s += @bit[i]
i -= i & -i
end
return s
end
def add(i, x)
# i += 1
while i <= @n
@bit[i] += x
i += i & -i
end
end
end
ans = 0
bit = BIT.new(n)
n.times do |j|
ans += j - bit.sum(arr[j])
bit.add(arr[j], 1)
end
puts ans
n = gets.chomp.to_i
arr = gets.chomp.split(" ").map(&:to_i)
class BIT
attr_reader :bit
def initialize(n)
@n = n
@bit = Array.new(@n + 1, 0)
end
def sum(i)
s = 0
while i > 0
s += @bit[i]
i -= i & -i
end
return s
end
def add(i, x)
i += 1
while i <= @n
@bit[i] += x
i += i & -i
end
end
end
p_ = 10**9 + 7
perm = Array.new(n+1, 1)
(1..n).each do |i|
perm[i] = perm[i-1] * i % p_
end
result = 1
bit = BIT.new(n)
arr.reverse.each_with_index do |a, i|
result += bit.sum(a) * perm[i]
result %= p_
bit.add(a, 1)
end
puts result
n = gets.chomp.to_i
arr = gets.chomp.split(" ").map(&:to_i)
class BIT
attr_reader :bit
def initialize(n)
@n = n
@bit = Array.new(@n + 1, 0)
end
def sum(i)
s = 0
while i > 0
s += @bit[i]
i -= i & -i
end
return s
end
def add(i, x)
# i += 1
while i <= @n
@bit[i] += x
i += i & -i
end
end
end
ans = 0
bit = BIT.new(n)
n.times do |j|
ans += j - bit.sum(arr[j])
bit.add(arr[j], 1)
end
if (n - ans).even?
puts "YES"
else
puts "NO"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment