[Python] Search (NumPy) ABC165C

ABC165C

Since it is a condition of a linear equation, the number of calculations can be greatly reduced by using a NumPy array. The NumPy array acts like an iterable.

It can also be executed with PyPy, and the calculation speed can be increased. (Memory usage increases for some reason)

Python3 1063 ms 106372 KB PyPy3 571 ms 155196 KB

Sample code


import sys
import itertools
import numpy as np
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

N, M, Q = map(int, readline().split())
 
A = np.array(list(itertools.combinations_with_replacement(range(1, M + 1), N)))
# [[1 1 1]
#  [1 1 2]
#  [1 1 3]
#  [1 1 4]
#  [1 2 2] ...

n = len(A)
score = np.zeros(n, np.int32)
m = map(int, read().split())
for a, b, c, d in zip(m, m, m, m):
    cond = A[:, b - 1] - A[:, a - 1] == c #NumPy array
    score += d * cond
 
print(score.max())

Recommended Posts

[Python] Search (NumPy) ABC165C
[Python] Search (itertools) ABC167C
[Python] BFS (breadth-first search) ABC007C
My Numpy (Python)
#Python basics (#Numpy 1/2)
ABC146C (binary search)
#Python basics (#Numpy 2/2)
numpy index search
Python #Numpy basics
[Python] Numpy memo
Python and numpy tips
Sequential search with Python
Python Exercise 1-Breadth-first search
Solve ABC146-C in Python
Binary search in Python
Python basics 8 numpy test
Binary search (python2.7) memo
[Python] Binary search ABC155D
python numpy array calculation
Solve ABC098-C in Python
python bit full search
Linear search in Python
Binary search with python
Binary search with Python3
Search Twitter using Python
[Python] Sorting Numpy data
Binary search in Python (binary search)
Python Basic --Pandas, Numpy-
[Python] BFS (breadth-first search) ABC168D
[Python] Calculation method with numpy
Search for strings in Python
Implemented SMO with Python + NumPy
Breadth-first search / bidirectional search (Python version)
Homebrew Python --Youtube Search Program
[Python] DFS (Depth-first Search) ATC001A
Matrix multiplication in python numpy
Binary search in Python / C ++
Algorithm in Python (binary search)
Full bit search with Python
[Python] DFS (Depth-first Search) ABC157D
Create a python numpy array
[AtCoder] ABC165C Personal Note [Python]
Search engine work with python
Search twitter tweets with python
[Python] Depth-first search and breadth-first search
[Python] Numpy reference, extraction, combination
Python3 | Getting Started with numpy
Streamline web search with python
Algorithm in Python (breadth-first search, bfs)
Write a binary search in Python
Subscript access to python numpy array
Introduction to Python Numerical Library NumPy
Python
Learn search with Python # 2bit search, permutation search
Put python, numpy, opencv3 in ubuntu14
Breadth-first search (BPF) Maybe understood (python)
Self-organizing map in Python NumPy version
Master linear search! ~ Python implementation version ~
Python application: Numpy Part 3: Double array
Write a depth-first search in Python
[At Coder] ABC085C --Otoshidama's Python answer