[PYTHON] Story of trying competitive programming Part 1

Why? Competitive programming, the so-called competitive pro, was mentioned in the tutorials of Paiza and python, but honestly, the efficiency of studying is at a level where I struggle quite a bit even at C rank because I am not very good at math in the humanities. I felt bad, and after that I was doing things using the framework to make things

** "Huh? Isn't it necessary separately?" **

After that, I felt that I was not good at it and didn't touch it.

However, the other day, I had to undergo a skill check in the process of selecting a company, and I received it at a place like the so-called AtCoder, but before taking the exam, do Paiza a little more and then take the exam.

** The pain and regret that you can understand the algorithm but cannot make it into the code ** ** In the first place, making things using frameworks is a different vector from the power required in competitive programming (= what you want to acquire by doing this) **

I noticed these two things, and it will become commonplace to do this kind of skill check before that, but I think it wouldn't be a story if Paiza's C rank couldn't be solved. Feeling that, I thought that I would raise my heavy waist and reach out to this person a little from now on, I googled quickly and found this article, so I started AtCoder for the first time in a long time. I would like to record the process. For the time being, I have finished up to the third question including the similar subjects in the reference article, so I will update it as needed while also serving as output and review, and after finishing the output up to that point, I will divide it into another article.

By the way, the story that making things using a framework and the power gained in competitive programming felt like different vectors is not a story that they are not related to each other, but competitive programming mainly manipulates data efficiently.・ The emphasis is on whether to edit, and for that purpose, knowledge of built-in functions in each language and mathematical thinking and knowledge in the first place are required. Rather, if you just make things using the framework, the framework is excellent, so you can roughly do it, but if you think about handling data in earnest, I think that you will need skills like you do in competitive programming. I am. In that sense, I think it would be embarrassing if I didn't have the power of C rank ... after finishing this skill check exam.

Goal

I want to be able to get 50 points firmly at the first look of skill check, about C rank of Paiza. First of all, I want to be aware that I can handle for and while firmly, instead of narrowing down the optimum.

Standard input

How to receive the numbers given in the problem. I think it's like a method and remember it with brain death.


#If you want to receive this kind of data
1
2 3
test

#Write like this
N = int(input()) # N = 1
b,c = map(int,input().split()) # b = 2, c = 3
S = input() # S = test


The problem I did

PracticeA - Welcome to AtCoder

Code written

N = int(input())
b,c = map(int,input().split())
S = input()
sum = N + b + c

# .If you use format, what you set in the argument{}Can be replaced with where you put
print("{} {}".format(sum, S))


ABC086A - Product

Code written

a,b = map(int,input().split())

#Whether the even number judgment is 0 when the remainder divided by 0 is 0. I often come out but often forget
def Check(A,B):
  result = A * B
  if result % 2 == 0:
    print('Even')
  elif result % 2 != 0:
    print('Odd')

Check(a,b)

ABC081A - Placing Marbles

Code written

#Break down characters one by one into a list
l = list(input())
#Convert elements in list to int type
l= [int(s) for s in l]
count = 0

for i in l:
  if i == 1:
    count += 1
print(count)

ABC081B - Shift only

Code written

N = int(input())
num_list = list(map(int,input().split()))
count = 0
bool = True
#It is important to decide which loop to end at what timing while combining while and for.
while bool:
  for i in range(N):
    if num_list[i] % 2 != 0:
      print(count)
    #This time with while num_1 element in list/2 I want to continue, so I stop while itself
      bool = False
    else:
      num_list[i] = num_list[i] // 2
  count += 1

ABC087B - Coins

Code written


#How many ways are there to choose some coins and make the total amount just X Yen?

A = int(input())
B = int(input())
C = int(input())
X = int(input())
#initial value
count = 0
countA = 0
countB = 0
countC = 0

# R3
while countA <= A:
    countB = 0
    # R2
    while countB <= B:
        countC = 0
        # R1
        while countC <= C:
            if(500*countA + 100*countB + 50*countC == X):
                count += 1
            countC += 1
        countB += 1
    countA += 1
print(count)

#Nested loops are processed from the inside
#In other words, processing starts from the position of R1 first, and when R1 ends, countB+=1 is performed and R2 processing starts. At this time countC=Since it is initialized to 0, the processing of R1 is also performed again.(At this time countB=1)
#Similarly, when R2 ends, countA+=1 is performed and R3 processing starts. At this time, countA=1 and countB=0 and countB<=Since it is B, count C= 0

A - RGB Cards

Code written

#Receive input in half-width space increments with int
N = map(int,input().split())
#Convert the received N to a string and make it a list
l = [str(i) for i in N]
#Combine strings in a list
num = "".join(l)

if int(num) % 4 == 0:
  print('YES')
elif int(num) % 4 != 0:
  print('NO')


A - Infinite Coins

Code written

N = int(input())
A = int(input())

if N % 500 <= A:
  print('Yes')
else:
  print('No')

A - Duplex Printing

Code written

import math
N = int(input())

print(math.ceil(N / 2))


ABC 095 A - Something on It  

Code written

S = input()
#The string is list()Can be decomposed into characters one by one
l = list(S)
count = 0

for i in l:
  if i == 'o':
    count += 1
sum = 700 + (100*count)
print(sum)


A - Already 2018

Code written

S = input()
l = list(S)

l[0] = '2'
l[1] = '0'
l[2] = '1'
l[3] = '8'

result = ''.join(l)
print(result)


B - i18n

Code written

S = input()
l = list(S)
#Number of characters between the beginning and the end=word count- 2
num = len(l) - 2
print(l[0] + str(num) + l[-1])


B - Two Anagrams

Code written

s = input()
t = input()

l_s = list(s)
l_t = list(t)

#L in dictionary order_s < l_I want to know if t holds → l_s dictionary order minimum(ascending order) < l_Maximum in dictionary order of t(descending order)Just judge if
#sorted creates a new array and sorts it while keeping the original array, reverse=True in descending order
sample1 = sorted(l_s)
sample2 = sorted(l_t, reverse=True)

if sample1 < sample2:
  print('Yes')
else:
  print('No')


B - Break Number

Code written

N = int(input())
list = []
#Since the input value is 1 or more, set the initial value to 1 or more.
sample = 1
count1 = 0

#I want to put the numerical value from 1 to the input value in the list from the condition
for i in range(N):
  list.append(i+1)

#Search processing with the created list
for l in list:
    #Create a new initial value
    p_l = l
    count2 = 0
    #Process to keep dividing
    while l % 2 == 0 :
        l = l // 2
        count2 += 1
    #When while is finished, select whether to update the initial value at the beginning with conditional branching
    if count2 > count1:
        count1 = count2
        sample = p_l
print(sample)


Maximum Difference

Code written

N = int(input())
l = list(map(int,input().split()))
count = 0
sample1 = 1

#Repeat as many times as there are elements
for i in range(N):
    #Then apply l to the for statement
    for j in l:
        #Since the loop is processed from the inside, j=until l goes around l[i]Is fixed
        sample2 = abs(l[i] - j)
        if sample2 > sample1:
            sample1 = sample2
print(sample1)

#When not using for
N = int(input())
l = list(map(int,input().split()))

#Maximum absolute value=Subtraction between maximum and minimum of list elements
l_max = max(l)
l_min = min(l)

abs = abs(l_max - l_min)

print(abs)


B - Palace

Code written

N = int(input())
average,temp = map(int, input().split())
elevation = list(map(int, input().split()))
newlist = []

#Function for calculating average temperature
def calcurate_temp(ele):
    result = average - (ele*0.006)
    return result
#Calculate the average temperature from the data of each given altitude, calculate the absolute value with the temperature, and put it in the list.
for i in range(N):
    t = calcurate_temp(elevation[i])
    sample = abs(temp - t)
    newlist.append(sample)
    pass
# .index()You can get the specified index of the list with
#This time it's close to the temperature=I want the index with the smallest absolute value, so it looks like this:
#Also, since the index starts from 0+1 returns what you did
ans = int(newlist.index(min(newlist))) + 1
print(ans)

#A pattern to write simply without creating a function
N = int(input())
T,A = map(int,input().split())
l = list(map(int,input().split()))
new_list = []

for i in range(N):
    sample = l[i]
    test = T - (sample*0.006)
    abs_sample = abs(A - test)
    new_list.append(abs_sample)
print(new_list.index(min((new_list))) + 1)

B - OddString

Code written

S = input()
l = list(S)
new_list = []

for i in range(len(l)):
    #Since the index starts from 0, the odd number is the character when the index becomes even.
    if i % 2 == 0:
        new_list.append(l[i])
ans = "".join(new_list)
print(ans)


B - A to Z String

Code written

S = input()
l = list(S)
new_list = []
#If there are multiple applicable characters, set the initial value that does not meet the conditions because you want to supplement only at the beginning.
first_a = 200001
last_z = 0

for i in range(len(l)):
    if l[i] == "A":
        a_index = i
        if first_a >= a_index:
            first_a = a_index
    if l[i] == "Z":
        z_index = i
        if last_z <= z_index:
            last_z = z_index
for j in range(first_a - 1, last_z):
    new_list.append(l[j])
print(len(new_list))


B - Bitter Alchemy

Code written

N, X = map(int,input().split())
l = [int(input()) for _ in range(N)]
#Make at least one, so add up the elements in the list
premise = sum(l)
#Subtract the premise from X to get the remainder of the material
rest = X - premise
#Then make as much as possible from the remaining ingredients=Calculate how much you can make a product that consumes less material and add prerequisites
print((rest // min(l)) + N)

#When using the sort function
N,X = map(int,input().split())
list = [int(input()) for _ in range(N)]
list_sum = sum(list)
sample = X - list_sum
#I want to find the smallest, so I sort the elements of the list in ascending order
list.sort()
 
print((sample // list[0]) + N)


Impressions I've tried so far

I've heard that competitive programming is called muscle training, but I think I was able to understand its meaning. Actually, before writing this article, I first looked at it and solved it twice, and when I wrote this article, I solved it again, but I feel that I finally understood it properly by doing so far. What I found most difficult so far

B - Bitter Alchemy B - Coins B - Shift Only

That was 3 questions. Bitter Alchemy simply didn't come up with the logic of trying as many = minimums as possible, and the last two were difficult in terms of understanding how to use for and while nesting and in range combinations. I think.

As of this writing, the selection mentioned at the beginning has fallen brilliantly and started from 0 again, but I will continue to do as much as I can.

Reference article

What to do next after registering with AtCoder-If you solve this much, you can fight enough! Past question selection 10 questions ~ Difference between sort and sorted to sort lists in Python Break from multiple loops (nested for loops) in Python Loop processing by Python for statement (range, enumerate, zip, etc.) Loop processing by Python while statement (infinite loop, etc.) [Python] How to calculate absolute value [Python] Competitive programming basic input summary Concatenate / join strings in Python (+ operator, join, etc.) [Python] for loop processing of list Python3 division is not truncation [Python] Get the maximum and minimum values ​​of the list and their indexes Truncate / round up after the decimal point in Python

Recommended Posts

Story of trying competitive programming Part 1
Story of trying competitive programming 2
Trying normal Linux programming Part 1
The story of trying to reconnect the client
The story of trying deep3d and losing
Story of trying to use tensorboard with pytorch
The story of trying Sourcetrail × macOS × VS Code
The story of making a Line Bot that tells us the schedule of competitive programming
[Note] Beginning of programming
I tried competitive programming
Recruitment of programming masters
The story of sys.path.append ()
Competitive programming diary python 20201220
Competitive programming with python
Competitive programming diary python
Technology that supports jupyter: traitlets (story of trying to decipher)
Linear programming + hands-on of pulp
Part 1 of receiving standard input
Program-level story of libscips ① (α0.0.1)
Try normal Linux programming Part 2
Features of programming languages [Memo]
Try normal Linux programming Part 3
Try normal Linux programming Part 4
Python Competitive Programming Site Summary
Competitive programming is what (bonus)
Try normal Linux programming Part 6
The popularity of programming languages
Making sound by programming Part 2
1st month of programming learning
Basics of Python × GIS (Part 1)
Input / output method of values from standard input in competitive programming, etc.
Re: Competitive Programming Life Starting from Zero Chapter 1.2 "Python of Tears"