Things to keep in mind when using Python with AtCoder

In Educational DP Contest B --Frog 2 , there is a case where the following code inevitably becomes TLE, so I submitted it with PyPy. By the way, it becomes AC easily. When I investigated later, it was found that the same algorithm may be AC depending on the language used ( reference ). Python users should choose PyPy, except in cases where they use libraries that are not implemented in PyPy. However, be careful as it consumes a lot of memory. When I tried it with Frog1, it consumes about 4 times more memory with the same source.

language memory
Python(3.82) 20524 KB
PyPy3 (7.3.0) 84904 KB
import sys

sys.setrecursionlimit(250000)
input = sys.stdin.readline

def main():
    n, k = map(int, input().split())
    h = list(map(int,input().split()))

    cost = [sys.maxsize]*n
    cost[0] = 0
    for i in range(0,n):
        for j in range(1, k + 1):
            if (i+ j>= n):
                break
            abs_ = abs(h[i] - h[i+j])
            if cost[i+j] >cost[i] + abs_ :
                cost[i+j] = cost[i] + abs_
    print(cost[n-1])
main()

Recommended Posts

Things to keep in mind when using Python with AtCoder
Things to keep in mind when using cgi with python.
Things to keep in mind when developing crawlers in Python
Things to keep in mind when copying Python lists
Things to keep in mind when processing strings in Python2
Things to keep in mind when processing strings in Python3
Things to keep in mind when using Python for those who use MATLAB
Things to keep in mind when converting row vectors to column vectors with ndarray
Things to keep in mind when building automation tools for the manufacturing floor in Python
Things to watch out for when using default arguments in Python
Things to keep in mind when deploying Keras on your Mac
Things to note when initializing a list in Python
How to exit when using Python in Terminal (Mac)
Three things I was addicted to when using Python and MySQL with Docker
Things to keep in mind when doing Batch Prediction on GCP ML Engine
Precautions when using pit in Python
When using MeCab with virtualenv python
Precautions when using six with Python 2.5
When using regular expressions in Python
How to log in to AtCoder with Python and submit automatically
I know? Data analysis using Python or things you want to use when you want with numpy
Summary of points to keep in mind when writing a program that runs on Python 2.5
How to not escape Japanese when dealing with json in python
Try logging in to qiita with Python
How to work with BigQuery in Python
Log in to Slack using requests in Python
To work with timestamp stations in Python
How to deal with OAuth2 error when using Google APIs from Python
Precautions when dealing with control structures in Python 2.6
How to deal with SessionNotCreatedException when using Selenium
Character encoding when dealing with files in Python 3
How to use tkinter with python in pyenv
Error when trying to install psycopg2 in Python
Materials to read when getting started with Python
[Fenwick_Tree] AtCoder Library-Reading with Green Coder-Implementation in Python-
What are you using when testing with Python?
When using PyQtGraph with Python Pyside, pay attention to the order of import
To automatically send an email with an attachment using the Gmail API in Python
Daily AtCoder # 2 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Daily AtCoder # 53 in Python
Daily AtCoder # 33 in Python
Daily AtCoder # 24 in Python
Daily AtCoder # 37 in Python
Daily AtCoder # 8 in Python
Daily AtCoder # 42 in Python
Daily AtCoder # 21 in Python
Daily AtCoder # 17 in Python
Daily AtCoder # 38 in Python
Daily AtCoder # 54 in Python
Daily AtCoder # 11 in Python
Daily AtCoder # 15 in Python
Daily AtCoder # 47 in Python
Daily AtCoder # 13 in Python
Daily AtCoder # 45 in Python
Daily AtCoder # 30 in Python
Daily AtCoder # 40 in Python
Daily AtCoder # 10 in Python
Daily AtCoder # 5 in Python
Daily AtCoder # 28 in Python