[PYTHON] AtCoder Beginner Contest 053 Review of past questions

Second past question

Time required

スクリーンショット 2020-01-08 15.05.48.png

Impressions

I didn't have time, so I redo the simple past questions.

Problem A

Change the output according to x.

answerA.py


x=int(input())
print("ABC" if x<1200 else "ARC")

B problem

Count from the front and from the back.

answerB.py


s=input()
n=len(s)
a,b=0,0
for i in range(n):
    if s[i]=="A":
        a=i
        break
for i in range(n-1,-1,-1):
    if s[i]=="Z":
        b=i
        break
print(b-a+1)

Here is the refactored code. (It's a basic function, but I don't remember it is bad ...)

answerB_better.py


s = input()
#Search from the front for index and from the back for rindex and return the first index
print(s.rindex('Z') - s.index('A') + 1)

C problem

Since you can freely choose the eyes, it is best to choose in the order of 6 → 5, if you think about the quotient divided by 11, it is OK

answerC.py


x=int(input())
ans=0
ans+=(x//11)*2
x-=(x//11)*11
if x==0:
    print(ans)
elif x<=6:
    print(ans+1)
else:
    print(ans+2)

D problem

It was a green problem and it was the second time, so if I wasn't careful, it took longer than I expected.

It's clear that leaving all the elements one by one is the maximum number of cards **, which motivates you to achieve this. Here, when you select 3 from the question sentence, you will eat 2 of them, so you can see that ** choosing from the ones that cover those 2 ** is the best way to achieve this. Therefore, I don't think it's a difficult idea to come up with the idea that ** the number of people covered should be considered as separate groups. For example, when there is a card "1 1 1 2 2 3 4 4 4 5 5", it is divided into "1 2 3 4 5" and "1 1 2 4 4 5", and the latter group of covers Consider eating the numbers in order. First of all, if you want to eat 1 and 1 (same number) in the latter group, you can ** select 3 together with 1 in the former group **. Next, if you try to eat 2 and 4, you can eat both 2 and 4 by ** choosing 2 or 4 of the former group **. You can eat 2 and 5 in the same way, so you can eat up the latter group of headgear and reach the maximum number of cards, 5. However, if the number of suffering groups is odd, you must eat only one from the former group due to the restriction of eating two each ** (for example, when "1 1 2 3" ). Based on the above consideration, the maximum number of cards can be obtained by counting what is covered and classifying by the total evenness and oddity. (At this time, you can sort and group by to divide them into groups neatly. ** The group by function is really convenient **.)

answerD.py


n=int(input())
def groupby(a):
    a2=[[a[0],1]]
    for i in range(1,len(a)):
        if a2[-1][0]==a[i]:
            a2[-1][1]+=1
        else:
            a2.append([a[i],1])
    return a2

b=groupby(sorted([int(i) for i in input().split()]))
l=len(b)
co=0
for i in range(l):
    co+=(b[i][1]-1)
if co%2==0:
    print(l)
else:
    print(l-1)

Recommended Posts

AtCoder Beginner Contest 102 Review of past questions
AtCoder Beginner Contest 072 Review of past questions
AtCoder Beginner Contest 085 Review of past questions
AtCoder Beginner Contest 062 Review of past questions
AtCoder Beginner Contest 113 Review of past questions
AtCoder Beginner Contest 074 Review of past questions
AtCoder Beginner Contest 051 Review of past questions
AtCoder Beginner Contest 127 Review of past questions
AtCoder Beginner Contest 119 Review of past questions
AtCoder Beginner Contest 151 Review of past questions
AtCoder Beginner Contest 075 Review of past questions
AtCoder Beginner Contest 054 Review of past questions
AtCoder Beginner Contest 110 Review of past questions
AtCoder Beginner Contest 070 Review of past questions
AtCoder Beginner Contest 105 Review of past questions
AtCoder Beginner Contest 112 Review of past questions
AtCoder Beginner Contest 089 Review of past questions
AtCoder Beginner Contest 069 Review of past questions
AtCoder Beginner Contest 079 Review of past questions
AtCoder Beginner Contest 056 Review of past questions
AtCoder Beginner Contest 087 Review of past questions
AtCoder Beginner Contest 067 Review of past questions
AtCoder Beginner Contest 093 Review of past questions
AtCoder Beginner Contest 046 Review of past questions
AtCoder Beginner Contest 123 Review of past questions
AtCoder Beginner Contest 049 Review of past questions
AtCoder Beginner Contest 078 Review of past questions
AtCoder Beginner Contest 081 Review of past questions
AtCoder Beginner Contest 060 Review of past questions
AtCoder Beginner Contest 104 Review of past questions
AtCoder Beginner Contest 057 Review of past questions
AtCoder Beginner Contest 121 Review of past questions
AtCoder Beginner Contest 126 Review of past questions
AtCoder Beginner Contest 090 Review of past questions
AtCoder Beginner Contest 103 Review of past questions
AtCoder Beginner Contest 061 Review of past questions
AtCoder Beginner Contest 059 Review of past questions
AtCoder Beginner Contest 044 Review of past questions
AtCoder Beginner Contest 048 Review of past questions
AtCoder Beginner Contest 124 Review of past questions
AtCoder Beginner Contest 116 Review of past questions
AtCoder Beginner Contest 097 Review of past questions
AtCoder Beginner Contest 088 Review of past questions
AtCoder Beginner Contest 092 Review of past questions
AtCoder Beginner Contest 099 Review of past questions
AtCoder Beginner Contest 065 Review of past questions
AtCoder Beginner Contest 053 Review of past questions
AtCoder Beginner Contest 094 Review of past questions
AtCoder Beginner Contest 063 Review of past questions
AtCoder Beginner Contest 107 Review of past questions
AtCoder Beginner Contest 071 Review of past questions
AtCoder Beginner Contest 064 Review of past questions
AtCoder Beginner Contest 082 Review of past questions
AtCoder Beginner Contest 084 Review of past questions
AtCoder Beginner Contest 068 Review of past questions
AtCoder Beginner Contest 058 Review of past questions
AtCoder Beginner Contest 043 Review of past questions
AtCoder Beginner Contest 098 Review of past questions
AtCoder Beginner Contest 114 Review of past questions
AtCoder Beginner Contest 045 Review of past questions
AtCoder Beginner Contest 120 Review of past questions