ABC188 C problem with python3

ABC188 C Problem ABC Tournament

one thing

I got an incorrect answer with tle because I used remove () in the list. From the C problem, I want to be aware of the computational complexity order.

Correct code

N=int(input())
A=list(map(int,input().split()))
survive=list(range(1,2**N+1))

    
for j in range(N-1):
    winner=[]
    for k in range(0, len(survive), 2):
        first=survive[k]
        second=survive[k+1]
        if A[first-1]>A[second-1]:
            winner.append(first)
        else:
            winner.append(second)
    survive=winner
    
first=survive[0]
second=survive[1]
if A[first-1]>A[second-1]:
    print(second)
else:
    print(first)

Commentary

First, make a list that contains data for everyone. From there, you can remove the losers in the tournament from the list and output the number of the remaining two with the lower rate. There are two ways to create a list that deletes the data of the loser. You can either make a list that contains rate data or a list that contains number data.

In the former case, the data to be output is a number, but the remaining data is the rate, so it is necessary to set aside a list that stores the rates in numerical order and check the rate index, which is troublesome. In the latter case, it is only necessary to output the remaining numbers as they are, so we will create a list that stores the number data.

The way to remove the loser's number from the list is to simply use remove () to shift the index of the data by the amount of the disappearance and it is troublesome, so store the number of the remaining person. Create an empty winner list and add only the winners from the survive list to the winner list. When the process is completed, set survive = winner. Implement by repeating this.

This method works well when you want to delete an element that meets a specific condition from the list and the index is involved in the condition to be deleted.

Simple analogy

Output list A = [2,3,1,5,6,7,3] with odd-numbered elements removed.

Answer

A=[2,3,1,5,6,7,3]
ans=[]
for i in range(len(A)):
    if i%2==0:
        ans.append(A[i])
print(ans)
#The answer is[2,1,6,3]Should be.

Summary

・ When there are two possible mounting methods, simulate to some extent and select the easier one. ・ If you want to delete an element that meets a specific condition from the list and the index is involved in the condition to be deleted, prepare an empty list and put the surviving element in it.

Recommended Posts

ABC163 C problem with python3
ABC188 C problem with python3
ABC187 C problem with python
Challenge AtCoder (ABC) 164 with Python! A ~ C problem
Solve ABC163 A ~ C with Python
ABC166 in Python A ~ C problem
Solve ABC168 A ~ C with Python
Solved AtCoder ABC 114 C-755 with Python3
Solve ABC162 A ~ C with Python
Solve ABC167 A ~ C with Python
[AtCoder] Solve ABC1 ~ 100 A problem with Python
[AtCoder commentary] Win the ABC165 C problem "Many Requirements" with Python!
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
Solve AtCoder ABC166 with python
ABC memorandum [ABC163 C --managementr] (Python)
Solve AtCoder ABC 186 with Python
ABC memorandum [ABC159 C --Maximum Volume] (Python)
Call C from Python with DragonFFI
Create Awaitable with Python / C API
ABC127 A, B, C Explanation (python)
Solve ABC166 A ~ D with Python
ABC memorandum [ABC161 C --Replacing Integer] (Python)
Solve ABC036 A ~ C in Python
ABC128 A, B, C commentary (python)
ABC126 A, B, C Explanation (python)
Solve ABC037 A ~ C in Python
AtCoder Beginner Contest 174 C Problem (Python)
[AtCoder explanation] Control ABC180 A, B, C problems with Python!
[AtCoder explanation] Control ABC188 A, B, C problems with Python!
[AtCoder explanation] Control ABC158 A, B, C problems with Python!
AtCoder ABC151 Problem D Speed comparison in C ++ / Python / PyPy
[AtCoder explanation] Control ABC164 A, B, C problems with Python!
[AtCoder explanation] Control ABC168 A, B, C problems with Python!
Beginner ABC154 (Python)
FizzBuzz with Python3
Solve ABC175 A, B, C in Python
Scraping with Python
Statistics with python
Scraping with Python
AtCoder ABC 174 Python
Twilio with Python
Integrate with Python
I wanted to solve ABC160 with Python
Python beginner Atcoder memo @ KEYENCE 2020, ABC problem
Play with 2016-Python
Japanese file enumeration with Python2 system on Windows (5C problem countermeasure)
AES256 with python
Beginner ABC155 (Python)
python starts with ()
python C ++ notes
python, openFrameworks (c ++)
Solve AtCoder ABC168 with python (A ~ D)
I wanted to solve the ABC164 A ~ D problem with Python
Bingo with python
Beginner ABC157 (Python)
Zundokokiyoshi with python
Use C ++ functions from python with pybind11
Solving with Ruby, Perl, Java, and Python AtCoder ABC 065 C factorial
AtCoder ABC 175 Python
I wanted to solve ABC172 with Python
RaspberryPi L Chika with Python and C #