Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part4-

Introduction

Hello. It's chewy and chewy. We will solve the introduction to AOJ's algorithms and data structures. It's easy to keep a record of what you've learned.

It's been less than half a year since I started programming myself AtCoder is green, so I'm not a strong man. Let's work hard together.

Pacho Fasomacho Pasomacho Paso

table of contents

This time PART3: Basic data structure. I want to do my best and do it to the end.

ALDS1_4_A: Linear Search ALDS1_4_B: Binary Search ALDS1_4_C: Dictionary ALDS1_4_D : Areas on the Cross-Section Diagram

ALDS1_4_A: Linear Search

Check each element of t Computational complexity is 0 (n * q)

n = int(input())
s = list(map(int,input().split()))
q = int(input())
t = list(map(int,input().split()))
ans = 0
for t1 in t:
    if t1 in s:
        ans += 1

print(ans)

ALDS1_4_B: Binary Search Check each element of t The amount of calculation is 0 (n * q) → 0 (10 ** 9), so it is impossible to do so, so it is 0 (q * ln (n)) in the binary search. Binary search

def b_search(x, target):
    found = False
    min = 0
    max = len(x)
    mid = (min + max) // 2
    while min <= max:
        if target == x[mid]:
            found = True
            break
        elif target > x[mid]:
            min = mid + 1
        else:
            max = mid - 1
        mid = (min + max) // 2
    if found:
        return True
    else:
        return False


n = int(input())
s = sorted(list(set(map(int,input().split()))))
q = int(input())
t = list(map(int,input().split()))
ans = 0
for i in t:
    if  b_search(s, i):
        ans += 1
print(ans)

ALDS1_4_C: Dictionary

Basically calculated with deque



n = int(input())
d = {}

for i in range(n):
    demand, str_list = map(str,input().split())
    if demand == "insert":
        d[str_list] = True

    else:
        if str_list in d:
            print("yes")
        else:
            print("no")

ALDS1_3_D : Areas on the Cross-Section Diagram

Fixed binary search Create a function that can be cleared with a certain value The rest is the flow of the binary search


n,k = map(int,input().split())
w = list(int(input())for i  in range(n))
def amount(p):
    e_amount = 0
    track = 1
    for i in w:
        if i > p:
            return False
        elif e_amount + i > p:
            e_amount = i
            track += 1
        else:
            e_amount += i

    if track <= k:
        return True
    else:
        return False


ng = 0
ok = 10**10
while ng + 1 < ok :
    mid = (ok+ng)//2
    if amount(mid) :
        ok = mid
    else:
        ng = mid

print(ok)

in conclusion

I will do my best

Recommended Posts

Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part1-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part2-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part4-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part3-
[Introduction to cx_Oracle] (Part 6) DB and Python data type mapping
[Introduction to cx_Oracle] (Part 9) DB and Python data type mapping (version 8 or later)
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
[Introduction to Data Scientists] Basics of Python ♬ Functions and classes
Introduction to Effectiveness Verification Chapters 4 and 5 are written in Python
Introduction to Python scikit-learn, matplotlib, single-layer algorithm (~ towards B3 ~ part3)
[Introduction to Python] Combine Nikkei 225 and NY Dow csv data
[Introduction to Python3 Day 1] Programming and Python
Sorting algorithm and implementation in Python
Introduction to Python Hands On Part 1
Hashing data in R and Python
An introduction to statistical modeling for data analysis (Midorimoto) reading notes (in Python and Stan)
processing to use notMNIST data in Python (and tried to classify it)
[Introduction to Data Scientists] Basics of Python ♬ Conditional branching and loops
[Introduction to Data Scientists] Basics of Python ♬ Functions and anonymous functions, etc.
[Introduction to Python] How to use class in Python?
AM modulation and demodulation in Python Part 2
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.1-8.2.5)
Ant book in python: Sec. 2-4, data structures
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.3-8.3.6.1)
Web-WF Python Tornado Part 3 (Introduction to Openpyexcel)
[Introduction to Python3 Day 19] Chapter 8 Data Destinations (8.4-8.5)
[Introduction to Python3 Day 18] Chapter 8 Data Destinations (8.3.6.2 to 8.3.6.3)
Easily graph data in shell and Python
Compress python data and write to sqlite
How to use is and == in Python
"Introduction to data analysis by Bayesian statistical modeling starting with R and Stan" implemented in Python
Introduction to Vectors: Linear Algebra in Python <1>
Introduction to Effectiveness Verification Chapter 1 in Python
[Introduction to Data Scientists] Basics of Python ♬
[Python] [Supplement] Chapter 04-09 Various data structures (set theory and operations in sets)
Data analysis: Easily apply descriptive and inference statistics to CSV data in Python
How to generate permutations in Python and C ++
[Introduction to Python3 Day 12] Chapter 6 Objects and Classes (6.3-6.15)
Introduction to effectiveness verification Chapter 3 written in Python
Python variables and data types learned in chemoinformatics
Receive and display HTML form data in Python
tse --Introduction to Text Stream Editor in Python
[Python] Swapping rows and columns in Numpy data
[Python] How to read data from CIFAR-10 and CIFAR-100
I wrote "Introduction to Effect Verification" in Python
[Introduction to Python3 Day 22] Chapter 11 Concurrency and Networking (11.1 to 11.3)
Send messages to Skype and Chatwork in Python
[Introduction to Python] How to handle JSON format data
[Introduction to Udemy Python3 + Application] 64. Namespace and Scope
[Introduction to Python3 Day 11] Chapter 6 Objects and Classes (6.1-6.2)
[Introduction to Algorithm] Find the shortest path [Python3]
Introduction to Effectiveness Verification Chapter 2 Written in Python
To represent date, time, time, and seconds in Python
How to plot autocorrelation and partial autocorrelation in python
[Python] How to name table data and output it in csv (to_csv method)
Introduction to Time Series Analysis ~ Seasonal Adjustment Model ~ Implemented in R and Python
[Introduction to Udemy Python3 + Application] 35. Comparison operators and logical operators
Convert timezoned date and time to Unixtime in Python2.7
Summary of tools needed to analyze data in Python
Full-width and half-width processing of CSV data in Python
[Introduction to Reinforcement Learning] part.1-Epsilon-Greedy Algorithm in Bandit Game