Atcoder standard input set for beginners (python)

Introduction

I don't know how many brews anymore, but it is a summary of the standard input set for Atcoder.

I'm sloppy with Atcoder, but I haven't been devoted at all, so I can't remember the standard input forever. So, I would like to organize the standard input once and systematically organize the faster standard input.

This article summarizes the minimum standard input for solving the basic ABC-ABC problem in the basics. Standard input using sys and numpy will be covered in another article.

The pointed out part has been corrected (2020/05/06).

1 row 1 column data

input

N (string or number)

code

sample.py


#When receiving in str type
s = input() 
#When receiving as an int type
s = int(input()) 
#float type(Decimal)When receiving at
s = float(input())

As a caveat, note that input () is all str type, even if it is a number instead of a string.

(1, N) Matrix data

input

A B

If the input is a string

Input example

Alice Bob Charlie

** Code example 1 **

sample.py


#When receiving as list type
s = input().split()

#output
print(s)
>>['Alice', 'Bob', 'Charlie']
print(s[0])
>>Alice
print(s[0][0])
>>A

** Code example 2 **

sample.py


#When receiving as a string
A, B, C = input().split()

#output
print(A)    
>>Alice
print(A,B,C)
>>Alice Bob Charlie

If the input variable is an integer

Input example

1 3

** Code example 1 **

sample.py


A, B = map(int, input().split())

#output
print(A)
>>1
print(A,B)
>>1 3

When the number of input variables is N

input

A1 A2 A3...AN

Input example

1 3 4 5 6

** Code example 1 **

sample.py


#Get as list type
l = list(map(int, input().split()))

#output
print(l)
>>[1, 3, 4, 5, 6]

(N, 1) Matrix data

input

N M A1 A2 : AN

Input example

3 4 2 3 3 1

** Code example 1 (int type) ** Convert (N, 1) matrix to (1, N) matrix by generating an empty list and storing it in the list in order from the top

sample.py


N, M = map(int, input().split())
#Empty list
A = []
#List A append()Store using
for _ in range(M):
    A.append(int(input())

#output
print(A)
>>[2, 3, 3, 1]

** Code example 2 (list comprehension) **

sample.py


N, M = map(int, input().split())
#List comprehension
A = [int(input()) for _ in range(M)]

#output
print(A)
>>[2, 3, 3, 1]

(N, M) Matrix data

2 variable data

When variables are lined up in a row

input

N x1 x2 x3 .. xN y1 y2 y3 .. yN

Input example

3 1 2 3 4 5 6

** Code example **

sample.py


N = int(input())
x = list(map(int, input().split()))
y = list(map(int, input().split()))

#output
print(x)
>>[1, 2, 3]

When variables are lined up in a column

input

N x1 y1 x2 y2 : xN yN

Input example

5 1 2 3 4 5 6 7 8 9 10

** Code example 1 (store x and y independently) **

sample.py


N = int(input())
xy = [map(int, input().split()) for _ in range(N)]
x, y = [list(i) for i in zip(*xy)]

#output
print(x)
>>[1, 3, 5, 7, 9]
print(x[1]+y[1])
>>7

** Code example 2 (stored as [x i </ sub>, y i </ sub>]) **

sample.py


N = int(input())
l = [list(map(int, input().split())) for l in range(N)]

#output
print(l)
>>[[1, 2], [3, 4], [5, 6]]

** In case of mixed int type and str type **

Input example

5 1 a 3 b 5 c 7 d 9 e

sample.py


N = int(input())
list = []
for i in range(N):
    a,b=input().split()
    list.append([int(a), b])
    
#output
print(list)
>>[[1, 'a'], [3, 'b'], [5, 'c'], [7, 'd'], [9, 'e']]
print(type(list[0][0]))
>><class'int'>
print(type(list[0][1]))
>><class'str'>

It was very easy to understand, so I quoted it from this article. Standard input of python3 in competition professionals

3 variable data

input

N t1 x1 y1 t2 x2 y2 : tN xN yN

Input example

2 3 1 2 6 1 1

sample.py


#First, create a list with the length of the input variable. * N=In case of 5,t = [0,0,0,0,0]
N = int(input())
t = [0] * N
x = [0] * N
y = [0] * N
for i in range(N):
    #Substitute in order from the top
    t[i], x[i], y[i] = map(int, input().split())

#output
print(t)
>>[3, 6]

I quoted from this article Tips you should know when programming in Python3 (input)

3 Variable data (when the number of data is different for each variable)

input

X Y A B C P1 P2 P3...PA Q1 Q2 Q3...QB R1 R2 R3...RC

Problem: E --Red and Green Apples

Input example

1 2 2 2 1 2 4 5 1 3

sample.py


X, Y, A, B, C = map(int, input().split())
P = [int(i) for i in input().split()]
Q = [int(i) for i in input().split()]
R = [int(i) for i in input().split()]

#output
print(P)
>>[2, 4]

#There is no problem with this form
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
R = list(map(int, input().split()))

General matrix data

input

N M A1,1 A1,2...A1 ,N : AM,1 AM,2...AM,N

** Input example (3,3) Matrix data **

3 3 2 1 3 1 3 3 2 2 1

** Code example 1 (int type) **

sample.py


N, M = map(int,input().split()) 
#List comprehension
#Read the list in order from the top and store it in the list.
a = [list(map(int, input().split())) for l in range(M)]

#output
print(a)
>>[[2, 1, 3], [1, 3, 3], [2, 2, 1]]

print(a[0])
>>[2, 1, 3]
print(a[0][0])
>>2
print(type(a[0][0]))
>><class'int'>

Supplement -numpy-

A little introduction to ** numpy **. You can use ** numpy ** to convert a list to a ** numpy array ndarray **. By performing this conversion, not only matrix operations can be performed, but also execution speed can be increased and code can be simplified.

sample.py


import numpy as np
#Appropriate list
list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

#Convert to ndarray
list_ndarray = np.array(list)
print(list_ndarray)
>>[[1 2 3]
   [4 5 6]
   [7 8 9]]

#Array size(Element count)
print(list_ndarray.size)
>>9
#Array shape
print(list_ndarray.shape)
>>(3, 3)
#Mold
print(type(list_ndarray))
>><class'numpy.ndarray'>

#numpy array → list
list_list = list_ndarray.tolist()
print(list_list)
>>[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(type(list_list))
>><class'list'>

Articles that I used as a reference

-[Standard input in python3] (https://qiita.com/pyu666/items/6b8cfefc1ea994639683) ・ Tips you should know when programming in Python3 (input) -Standard input of python3 in competition pros, etc.Python de Competitive Programming

About standard input and standard output

See the article below for details. ・ [What are standard input and standard output? ] (https://qiita.com/angel_p_57/items/03582181e9f7a69f8168)

Make corrections as appropriate.

Continue to another article.

Recommended Posts

Atcoder standard input set for beginners (python)
[For AtCoder] Standard input memo
[Python] Standard input
[For beginners] Summary of standard input in Python (with explanation)
python textbook for beginners
[Python] About standard input
OpenCV for Python beginners
Learning flow for Python beginners
[Python3] Standard input [Cheat sheet]
Receiving standard input tips @ python
Python Input Note in AtCoder
Python #function 2 for super beginners
Basic Python grammar for beginners
100 Pandas knocks for Python beginners
Python for super beginners Python #functions 1
Solve AtCoder Problems Boot camp for Beginners (Medium 100) with python
Matrix representation with Python standard input
Python Exercise for Beginners # 2 [for Statement / While Statement]
Python for super beginners Python # dictionary type 1 for super beginners
[Python] ARC006A (set strongest theory) [AtCoder]
Python #index for super beginners, slices
<For beginners> python library <For machine learning>
Python #len function for super beginners
Notes for Python file input / output
Beginners use Python for web scraping (1)
# 2 Python beginners challenge AtCoder! ABC085C --Otoshidama
Python #Hello World for super beginners
Python for super beginners Python # dictionary type 2 for super beginners
atCoder 173 Python
Python Paiza-Various skill checks and standard input
INSERT into MySQL with Python [For beginners]
[Python] Minutes of study meeting for beginners (7/15)
Answer to AtCoder Beginners Selection by Python3
Python3 standard input I tried to summarize
AtCoder cheat sheet in python (for myself)
[Python] Read images with OpenCV (for beginners)
WebApi creation with Python (CRUD creation) For beginners
[Python] Add comments to standard input files
[For beginners] Try web scraping with Python
A textbook for beginners made by Python beginners
2016-10-30 else for Python3> for:
Memo # 4 for Python beginners to read "Detailed Python Grammar"
python [for myself]
Python set arithmetic
The fastest way for beginners to master Python
Python for super beginners Python for super beginners # Easy to get angry
Causal reasoning and causal search with Python (for beginners)
Roadmap for beginners
AtCoder ABC 174 Python
For new students (Recommended efforts for Python beginners Part 1)
Python set arithmetic
AtCoder ABC187 Python
Memo # 1 for Python beginners to read "Detailed Python Grammar"
~ Tips for Python beginners from Pythonista with love ① ~
AtCoder ABC188 Python
Easy understanding of Python for & arrays (for super beginners)
Standard input summary
Python script to get a list of input examples for the AtCoder contest
Memo # 7 for Python beginners to read "Detailed Python Grammar"
Beginners practice Python
Introduction to Programming (Python) TA Tendency for beginners