(Python) ABC162-D Consideration log and solution

I participated (virtually) in ABC162. D Problem was interesting, so I will write the first commentary article!

image.png

It wasn't a beautiful commentary, but I wrote it so that what I thought at that time would flow down.

You can see the video on YouTube.

problem

D - RGB Triplets

image.png

Understand the problem statement while receiving input

Read the problem while doing what you can do with brain death. Let's receive the input first.

n = int(input())
s = input()

Are these two conditions in the text?

Consider while writing stupidity

If you are in trouble, be honest. Think while moving your hands.

Search all combinations where $ i <j <k $, and add ʻans` if the conditions are met.

ans = 0

for i in range(n-2):
    for j in range(i+1, n-1):
        for k in range(j+1, n):
            if s[i]!=s[j] and s[j]!=s[k] and s[i]!=s[k]:
                if j-i != k-j:
                    ans += 1

print(ans)

This is obviously $ O (N ^ 3) $. $ N <= 4000 $, so no? If you set it to $ O (N ^ 2) $, it will be in time, but can you reduce the number of loops?

If you decide two, the remaining one will be decided automatically

When I saw the triple loop, I thought of Otoshidama.

Can it be a double loop? If this kind of processing can be done, can we go with $ O (N ^ 2) $?

for i in range(n-2):
    for j in range(i+1, n-1):
        if s[i] == s[j]:
            continue
        c = "(s[i],s[j]Different from(R,G,B)Any of)"

        ans += "s[j+1:n]Number of c in"

        #However, i,j,The position where k is evenly spaced is not suitable, so reduce it by 1.
        if j+(j-i)<n and s[j+(j-i)] == c:
            ans -= 1

Cumulative sum 3

"The number in the interval is the cumulative sum! (Practice swing)", so it will be the cumulative sum. At this point, all you have to do is make a cumulative sum.

ruisekiR = [0] * (n+1)
ruisekiG = [0] * (n+1)
ruisekiB = [0] * (n+1)

for i,c in enumerate(s):
    ruisekiR[i+1] = ruisekiR[i] + ( 1 if c == "R" else 0)
    ruisekiG[i+1] = ruisekiG[i] + ( 1 if c == "G" else 0)
    ruisekiB[i+1] = ruisekiB[i] + ( 1 if c == "B" else 0)

If you make this, the number of cs in s [j + 1: n] can be found by $ O (1) $.

        # c = (s[i],s[j]Different from(R,G,B)Any of)
        c = "R"
        if "R" in (s[i], s[j]):
            c = "G"
            if "G" in (s[i], s[j]):
                c = "B"

        # ans += (s[j+1:n]Number of c in)
        if c == "R":
            ans += (ruisekiR[n] - ruisekiR[j])
        if c == "G":
            ans += (ruisekiG[n] - ruisekiG[j])
        if c == "B":
            ans += (ruisekiB[n] - ruisekiB[j])

It's kind of dirty, but it's a contest so I don't care. This is AC!

Whole source

n = int(input())
s = input()

ruisekiR = [0] * (n+1)
ruisekiG = [0] * (n+1)
ruisekiB = [0] * (n+1)

for i,c in enumerate(s):
    ruisekiR[i+1] = ruisekiR[i] + ( 1 if c == "R" else 0)
    ruisekiG[i+1] = ruisekiG[i] + ( 1 if c == "G" else 0)
    ruisekiB[i+1] = ruisekiB[i] + ( 1 if c == "B" else 0)

ans = 0

for i in range(n-2):
    for j in range(i+1, n-1):
        if s[i] == s[j]:
            continue
        c = "R"
        if "R" in (s[i], s[j]):
            c = "G"
            if "G" in (s[i], s[j]):
                c = "B"

        if c == "R":
            ans += (ruisekiR[n] - ruisekiR[j])
        if c == "G":
            ans += (ruisekiG[n] - ruisekiG[j])
        if c == "B":
            ans += (ruisekiB[n] - ruisekiB[j])

        if j+(j-i)<n and s[j+(j-i)] == c:
            ans -= 1

print(ans)

Recommended Posts

(Python) ABC162-D Consideration log and solution
python log
[Python] ABC175D
[Python] DP ABC184D
[Python] UnionFind ABC177D
[At Coder] Beginner Contest 175 ABCD python Solution introduction
Output python log to both console and file
[python] Compress and decompress
Solve ABC168D in Python
Solve ABC167-D in Python
[Python] Interval scheduling ABC103D
Batch design and python
Python iterators and generators
[Python] Cumulative sum ABC186D
Python packages and modules
Vue-Cli and Python integration
Ruby, Python and map
[Python] Binary search ABC155D
python input and output
Solve ABC159-D in Python
[Python] Dynamic programming ABC015D
Python3, venv and Ansible
Compare "relationship between log and infinity" in Gauche (0.9.4) and Python (3.5.1)
Python asyncio and ContextVar
[Python] Cumulative sum ABC179D
How to log in to AtCoder with Python and submit automatically
[Python] BFS (breadth-first search) ABC168D
Encryption and decryption with Python
3-3, Python strings and character codes
Python 2 series and 3 series (Anaconda edition)
Python and hardware-Using RS232C with Python-
Python on Ruby and angry Ruby on Python
Python indentation and string format
Python real division (/) and integer division (//)
Å (Ongustromu) and NFC @ Python
Understand Python packages and modules
# 2 [python3] Separation and comment out
Python shallow copy and deep copy
Python and ruby slice memo
Python installation and basic grammar
I compared Java and Python!
Python shallow and deep copy
ABC161D Lunlun Number with python3
About Python, len () and randint ()
About Python datetime and timezone
Install Python 3.7 and Django 3.0 (CentOS)
Python environment construction and TensorFlow
Python class variables and instance variables
Ruby and Python syntax ~ branch ~
[Python] Python and security-① What is Python?
Stack and Queue in Python
Fibonacci and prime implementations (python)
Python basics: conditions and iterations
Python bitwise operator and OR
Python debug and test module
Python list and tuples and commas
Python variables and object IDs
Python list comprehensions and generators
About Python and regular expressions
python with pyenv and venv
Unittest and CI in Python