Competitive programming diary python

Tsuyotsuyo Engineers will also try to do what is called competitive programming.

AtCoder Beginner Contest 183

Implement a function that returns 0 if the input is a negative value and around that if the input is a positive value.

N = int(input())

def ReLU(x):
   if x > 0:
       return x
   else:
       return 0
       
print(ReLU(N))

Bounce back to the x-axis to play billiards. I miss it because it is a common problem in taking an examination. A bullet is shot at the point where the target location is moved to the x-axis target. The answer is the intersection with the x-axis at that time. I felt that it would be good to calculate the internal division point considering the similarity.

sx, sy, gx, gy = map(int, input().split())

print(float((gy * sx + sy * gx) / (sy + gy)))

If you have N cities and you visit all cities once, how many routes will the time be exactly K?

Since N is at most 8, there are about 8! Of combinations of routes. There seems to be no problem with the full search.

Exporting all combinations is easy with python using itertools

0 1 10 100
1 0 20 200
10 20 0 300
100 200 300 0

To receive a matrix like this with input, you can convert each column to an array and turn it by row.

code.py


N, K = map(int, input().split())

matrix = [input().split() for _ in range(N)]
print(*matrix, sep='\n')

output.py


['0', '1', '10', '100']
['1', '0', '20', '200']
['10', '20', '0', '300']
['100', '200', '300', '0']

The answer is like this. I feel like I can write better

import itertools

N, K = map(int, input().split())

matrix = [list(map(int, input().split())) for _ in range(N)]
ans =0
#Since it starts from 1 and changes to 1, insert the city 1 after finding the permutation combination without 1 (index is 0).
for v in itertools.permutations(range(1, N)):
    total_time = 0
    keiro = list(v)
    keiro.append(0)
    keiro.insert(0, 0)
    for i in range(len(keiro) -1):
        total_time += matrix[keiro[i]][keiro[i+1]]
    if total_time == K:
        ans += 1
print(ans)

Prepare an array for the length of time, and add the usage time to the usage time zone while looping for people. Out if there is time to exceed W.

I implemented it thinking that, but TLE

max_time = 2 * 10 ** 5
t = [0 for _ in range(max_time)]

n, w = map(int, input().split())
ans = 'Yes'
for i in range(n):
    start, end, usage = map(int, input().split())
    for j in range(start, end): #This is OK because end is not included
        t[j] += usage
        if t[j] > w:
            ans = 'No'
            break
print(ans)

I overlooked the constraints ... N and T were on the order of 2 * 10 ** 5

Since it is only the start and end timings that need to be judged, I was able to go to the point where I should look at the elements at that timing for each person, but I can not think of it after that,

Looking at the explanation, it seems that there is something called "potato method"

https://imoz.jp/algorithms/imos_method.html

With this I was able to solve it in time!

max_time = 2 * 10 ** 5 + 1
t = [0 for _ in range(max_time)]

n, w = map(int, input().split())
ans = 'Yes'
for i in range(n):
    start, end, usage = map(int, input().split())
    #Use the potato method. Record the timing of entry and exit
    t[start] += usage
    t[end] -= usage

for i in range(1, len(t)):
    t[i] += t[i-1]

if max(t) > w:
    print('No')
else:
    print(ans)

Recommended Posts

Competitive programming diary python 20201213
Competitive programming diary python 20201220
Competitive programming diary python
Competitive programming with python
Python Competitive Programming Site Summary
Python3 standard input for competitive programming
Python programming note
Programming in python
python challenge diary ①
Competitive programming with python Local environment settings
[Competitive programming] [Python3] Required knowledge, for myself
I made a competitive programming glossary with Python
I tried competitive programming
3. 3. AI programming with Python
Python programming with Atom
Python programming in Excel
LEGO Mindstorms 51515 Python Programming
[Python] Dynamic programming ABC015D
Programming with Python Flask
Tips you should know when programming competitive programming with Python2
Programming with Python and Tkinter
Python3 programming functions personal summary
Atcoder Acing Programming Contest Python
Python web programming article summary
Paiza Python Primer 1 Learn Programming
Python Machine Learning Programming> Keywords
Python Programming Workshop-Super Introductory Vol.4
Story of trying competitive programming 2
An introduction to Python Programming
[Python] Dynamic programming TDPC A
Network programming with Python Scapy
[Swift / Ruby / Python / Java] Object-oriented programming
Functional programming in Python Project Euler 1
[Introduction to Python3 Day 1] Programming and Python
Python
[Python] [Table of Contents Links] Python Programming
Competitive professional devotion diary 11th-14th days (7 / 5-7 / 8)
Tips you should know when programming competitive programming with Python2 (useful library)
Easy Python + OpenCV programming with Canopy
Story of trying competitive programming Part 1
Functional programming in Python Project Euler 2
python documentation reading socket programming HOWTO
Python Programming Workshop-Super Introductory Python Execution Environment
Re: Competitive Programming Life Starting from Zero Chapter 1.2 "Python of Tears"
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
Memorandum / memo about programming learning / competitive programming site
Competitive professional devotion diary 18th to 19th days (7/12 to 7/13)
Eating and comparing programming languages: Python and Ruby
"Python AI programming" starting from 0 for windows
What kind of programming language is Python?
Scientific Programming Petit Tech Collection in Python
Try text mining your diary in Python
Competitive professional devotion diary 4th to 10th days (6/28 to 7/4)
Try a functional programming pipe in Python
Competitive professional devotion diary 15th to 17th days (7/9 to 7/11)
[Python] Start diary from today Atcorder ABC058-B
Learn dynamic programming in Python (A ~ E)
Get all standard inputs used in paiza and competitive programming with int (python)