Solving with Ruby and Python AtCoder Tenka1 Programmer Contest C Cumulative sum

Introduction

This theme

Tenka1 Programmer Contest C - Stones Difficulty: 946

This theme, cumulative sum

From the problem, the final arrangement of stones is all white ........, all black ########, or left white right black..... # Change to one of ##. A little annoying is a pattern like . # .... #. #., The left . #. Is ... and the right #. #. Is #### The minimum value is to change it to . Therefore, take the cumulative sum of . and#from left to right, check the number of changes at that position, and find the minimum value. Ruby

ruby.rb


n = gets.to_i
s = gets.chomp.bytes
dot = '.'.ord
a = Array.new(n){Array.new(2, 0)}
if s[0] == dot
  a[0][0] = 1
else
  a[0][1] = 1
end
1.upto(n - 1) do |i|
  if s[i] == dot
    a[i][0] = a[i - 1][0] + 1
    a[i][1] = a[i - 1][1]
  else
    a[i][0] = a[i - 1][0]
    a[i][1] = a[i - 1][1] + 1
  end
end
min = [a[-1][0], a[-1][1]].min
n.times do |i|
  min = a[-1][0] - a[i][0] + a[i][1] if min > a[-1][0] - a[i][0] + a[i][1]
end
puts min

rui.rb


1.upto(n - 1) do |i|
  if s[i] == dot
    a[i][0] = a[i - 1][0] + 1
    a[i][1] = a[i - 1][1]
  else
    a[i][0] = a[i - 1][0]
    a[i][1] = a[i - 1][1] + 1
  end
end

It's anomalous, but it's a cumulative sum.

bw.rb


min = [a[-1][0], a[-1][1]].min

First, find out the number of cases when all are white and when all are black.

tugi.rb


  min = a[-1][0] - a[i][0] + a[i][1] if min > a[-1][0] - a[i][0] + a[i][1]

Next, check the number when changing to left white right black at each position.

ord.rb


dot = '.'.ord

  if s[i] == dot

It seems that it is a little faster to compare by char value. Python

python.py


from sys import stdin

def main():
    input = stdin.readline

    n = int(input())
    s = input()
    a = [[0, 0] for _ in range(n)]
    if s[0] == '.':
        a[0][0] = 1
    else:
        a[0][1] = 1
    for i in range(1, n):
        if s[i] == '.':
            a[i][0] = a[i - 1][0] + 1
            a[i][1] = a[i - 1][1]
        else:
            a[i][0] = a[i - 1][0]
            a[i][1] = a[i - 1][1] + 1
    min = a[-1][0] if a[-1][0] < a[-1][1] else a[-1][1]
    for i in range(n):
        if min > a[-1][0] - a[i][0] + a[i][1]:
            min = a[-1][0] - a[i][0] + a[i][1]
    print(min)
main()
Ruby Python
Code length(Byte) 457 631
Execution time(ms) 175 186
memory(KB) 16208 30024

Summary

Recommended Posts

Solving with Ruby and Python AtCoder Tenka1 Programmer Contest C Cumulative sum
Solving with Ruby, Perl, Java, and Python AtCoder ARC 098 C Cumulative sum
Solving with Ruby and Python AtCoder ARC 059 C Least Squares
Solving with Ruby and Python AtCoder ABC011 C Dynamic programming
Solving with Ruby and Python AtCoder ARC067 C Prime Factorization
Solving with Ruby, Perl, Java and Python AtCoder diverta 2019 Programming Contest C String Manipulation
Solve with Ruby, Python and Java AtCoder ARC104 B Cumulative sum
Solving with Ruby, Perl, Java, and Python AtCoder ABC 065 C factorial
Solving with Ruby and Python AtCoder ABC057 C Prime Factorization Bit Search
Solving with Ruby, Perl, Java and Python AtCoder CADDi 2018 C Prime Factorization
AtCoder ABC172 C Cumulative Sum Binary Search Solved by Ruby and Python
Solve with Ruby and Python AtCoder ABC084 D Cumulative sum of prime numbers
Solving with Ruby and Python AtCoder CODE FESTIVAL 2016 qual C B Priority queue
Solving with Ruby and Python AtCoder ABC178 D Dynamic programming
Solving with Ruby and Python AtCoder ABC151 D Breadth-first search
Solving with Ruby and Python AtCoder AISING2020 D Iterative Squares
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 A
Solving with Ruby and Python AtCoder ABC153 E Dynamic programming
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 B
Solving with Ruby and Python AtCoder ABC138 D Adjacency list
Solving with Ruby, Python and numpy AtCoder ABC054 B Matrix operation
Solving with Ruby, Python and networkx AtCoder ABC168 D Adjacency list
Solving with Ruby, Perl, Java and Python AtCoder ABC 107 B String Manipulation
AtCoder ABC130 D Cumulative Sum Binary Search Solved by Ruby and Python
Solving with Ruby, Perl, Java and Python AtCoder ABC 165 D Floor function
Solving with Ruby, Perl, Java and Python AtCoder ABC 131 D Array Sorting
Solve with Ruby, Perl, Java and Python AtCoder ABC 047 C Regular Expression
Solving with Ruby AtCoder ABC110 C String Manipulation
Solving in Ruby, Perl, Java, and Python AtCoder ARC 066 C Iterative Squares Hash
Scraping with Node, Ruby and Python
Solved AtCoder ABC 114 C-755 with Python3
AtCoder Beginner Contest 174 C Problem (Python)
Create AtCoder Contest appointments on Google Calendar with Python and GAS
Solving in Ruby, Python and Java AtCoder ABC141 D Priority Queuing
Encrypt with Ruby (Rails) and decrypt with Python
Easy web scraping with Python and Ruby
RaspberryPi L Chika with Python and C #
AtCoder Beginner Contest 170 B Problem "Crane and Turtle" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 177 C Problem "Sum of product of pairs" Explanation (Python3, C ++, Java)
Sorting AtCoder ARC 086 C hashes to solve in Ruby, Perl, Java and Python
Solving the Lorenz 96 model with Julia and Python
Challenge AtCoder (ABC) 164 with Python! A ~ C problem
AtCoder ARC080 D simulation solved in Ruby and Python
Benchmark for C, Java and Python with prime factorization
AtCoder Beginner Contest 176 C Problem "Step" Explanation (Python3, C ++, Java)
Comparison of CoffeeScript with JavaScript, Python and Ruby grammar
Version control of Node, Ruby and Python with anyenv
Solve AtCoder 167 with python
Ruby, Python and map
Python and Ruby split
[Python] Cumulative sum ABC179D
AtCoder JSC2019 Qual B to solve with Ruby and Python Inverse element of arithmetic progression
AtCoder Beginner Contest 166 A Explanation of Problem "A? C" (Python3, C ++, Java)
AtCoder Beginner Contest 174 B Problem "Distance" Explanation (C ++, Python, Java)
AtCoder Beginner Contest 177 B Problem "Substring" Explanation (Python3, C ++, Java)
[AtCoder explanation] Control ABC180 A, B, C problems with Python!
[AtCoder explanation] Control ABC188 A, B, C problems with Python!
Mandelbrot Benchmark (C, PHP, HHVM, Ruby, Python, PyPy, and Kinx)
AtCoder Beginner Contest 167 A Problem "Registration" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 169 B Problem "Multiplication 2" Explanation (Python3, C ++, Java)
AtCoder ABC168 A case expression solved in Ruby and Python