Solve with Python [100 past questions that beginners and intermediates should solve] (053 --055 Dynamic programming: Others)

1. Purpose

Solve 100 past questions that beginners and intermediates should solve in Python. The goal is to turn light blue by the time you finish solving everything.

This article is "053 --055 Dynamic Programming: Others".

2. Summary

As you can see from "Other", these three questions are a little different from the previous `` `dp``` in terms of orientation (preference?) I felt that.

3. Main story

053 --055 Dynamic programming: Other

053. DPL_1_D --Longest increasing subsequence

image.png

Answer


import bisect

n = int(input())
A = [int(input()) for _ in range(n)]
dp = [A[0]]

for i in range(1, n):
    if A[i] > dp[-1]:
        dp.append(A[i])
    else:
        ind = bisect.bisect_left(dp, A[i])
        dp[ind] = A[i]

print(len(dp))

It is solved by binary search and DP.

dpIn ascending orderaWe will add the elements of. In particular,

  1. For all A elements ```A [i] `` `
  2. Add anything larger than the last element of `dp``` to `dp```
  3. If it is not large, there are numbers greater than or equal to A [i] `` `in` `` dp, so replace it with ```A [i] `` `
  4. The answer is the length of the completed dp is.

054. AtCoder Beginner Contest 006 D --Trump Insertion Sort

image.png

Answer


import bisect

N = int(input())
C = [int(input()) for _ in range(N)]

dp = [C[0]] #initial value

for i in range(1, N):
    if C[i] > dp[-1]:
        dp.append(C[i])
    else:
        ind = bisect.bisect_left(dp, C[i])
        dp[ind] = C[i]

print(N - len(dp))


  1. DPL_1_D --Longest increasing subsequence is almost the same. The difference is the last ``` print (N --len (dp))` ``. The number of times you need to insert playing cards is the number of playing cards minus the length of the longest increasing subsequence.

  1. AtCoder Beginner Contest 134 E - Sequence Decomposing image.png

Answer


import bisect
from collections import deque

N = int(input())
A = [int(input()) for _ in range(N)]

dp = deque()
dp.append(A[0])

for i in range(1, N):
    ind = bisect.bisect_left(dp, A[i])

    if ind == 0:
        dp.appendleft(A[i])
    else:
        dp[ind-1] = A[i]

print(len(dp))


Let's change the way of thinking from the above two problems. In the above two problems, the elements above the maximum value of `` `dpwereappend, This time, the elements below the minimum value are appendleft```.


Recommended Posts

Solve with Python [100 past questions that beginners and intermediates should solve] (053 --055 Dynamic programming: Others)
Solve with Python [100 past questions that beginners and intermediates should solve] (028 --033 breadth-first search)
Solve with Python [100 selections of past questions that beginners and intermediates should solve] (034-038 Dynamic programming: Knapsack DP basic)
Solve with Python [100 selections of past questions that beginners and intermediates should solve] (039 --045 Dynamic programming: Knapsack DP variant)
Solve with Python [100 selected past questions that beginners and intermediates should solve] (024 --027 Depth-first search)
Solve with Python [100 selected past questions that beginners and intermediates should solve] (015 --017 Full search: Permutation full search)
Solve with Python [100 selected past questions that beginners and intermediates should solve] (010 --014 Full search: Bit full search)
[Python] I tried to solve 100 past questions that beginners and intermediates should solve [Part 5/22]
[Python] I tried to solve 100 past questions that beginners and intermediates should solve [Part7 / 22]
[Python] I tried to solve 100 past questions that beginners and intermediates should solve [Part 4/22]
[Python] I tried to solve 100 past questions that beginners and intermediates should solve [Part3 / 22]
[Python] I tried to solve 100 past questions that beginners and intermediates should solve [Part 1/22]
[Python] I tried to solve 100 past questions that beginners and intermediates should solve [Part 6/22]
Solve with Python [100 selected past questions that beginners and intermediates should solve] (056 --059 Shortest path problem: Dijkstra's algorithm)
Solve with Python [100 selected past questions that beginners and intermediates should solve] (005 --- 009 All search: All enumeration to reduce the number of streets by devising)
1st Algorithm Practical Test Solve past questions with python
Programming with Python and Tkinter
Solving with Ruby and Python AtCoder ABC178 D Dynamic programming
Solving with Ruby and Python AtCoder ABC011 C Dynamic programming
Solving with Ruby and Python AtCoder ABC153 E Dynamic programming
2nd Algorithm Practical Test Solve past questions with python (unfinished)
Tips (input / output) that you should know when programming competitive programming with Python2
Tips (control structure) that you should know when programming competitive programming with Python2
Tips (data structure) that you should know when programming competitive programming with Python2
Causal reasoning and causal search with Python (for beginners)
[For beginners in competition professionals] I tried to solve 40 AOJ "ITP I" questions with python
Try to solve the programming challenge book with python3
Python that I would like to recommend to programming beginners
Solve simultaneous ordinary differential equations with Python and SymPy.
Tips you should know when programming competitive programming with Python2
Solve AtCoder 167 with python
3. 3. AI programming with Python
Python programming with Atom
[Python] Dynamic programming ABC015D
Solve POJ 2386 with python
Programming with Python Flask
[Python] Technique to reduce dimensions with DP (Dynamic Programming) [AtCoder]
I wanted to solve the Panasonic Programming Contest 2020 with Python
Compare HTTP GET / POST with cURL (command) and Python (programming)
Solve the spiral book (algorithm and data structure) with python!
Memoization recursion and dynamic programming known by Python Fibonacci sequence
Solve AtCoder Problems Boot camp for Beginners (Medium 100) with python
This and that for using Step Functions with CDK + Python
Module summary that automates and assists WebDriver installation with Python