Atcoder ABC166 A-E in Python

It was 4 complete again. E is commentary AC. Since the code of the contest is posted as it is, there may be some unsightly parts, but please forgive me.

A. A?C Returns ARC if the input is ABC, ABC otherwise

ABC166a.py


s=input()

if s=="ABC":
    print("ARC")
else:
    print("ABC")

B. Trick or Treat For all sweets, add +1 to the index of the child you have. Finally, count and output the children who do not have any sweets.

ABC166b.py


n,k=map(int,input().split())
d=[]
a=[]
for i in range(k):
    d.append(int(input()))
    a.append(list(map(int,input().split())))
    
aa=[0]*n

for j in a:
    for k in j:
        aa[k-1]+=1 #Index of the child who has sweets+1
        
ans=0

for x in aa:
    if x==0:
        ans+=1 #The number of children who do not have any sweets is 0, so count the number of children.

print(ans)

C. Peaks Prepare an array with the same length as the number of vertices, compare the heights of the two vertices, and add +1 to the lower vertex. If the heights are the same, both are incremented by 1. Finally, the number of vertices whose value is 0 is output. By the way, one road meant only the road leading to the top, not the road that can be reached with a single stroke. I misunderstood and lost time.

ABC166c.py


import copy

n,m=map(int,input().split())
h=list(map(int,input().split()))
l=[]
for i in range(m):
    a,b=map(int,input().split())
    l.append([a,b])
    
ll=[0]*n

for j in l:
    if h[j[0]-1]>h[j[1]-1]:
        ll[j[1]-1]+=1
    elif h[j[0]-1]==h[j[1]-1]:
        ll[j[1]-1]+=1
        ll[j[0]-1]+=1
    else:
        ll[j[0]-1]+=1

ans=0
#print(ll)
for k in ll:
    if k==0:
        ans+=1
print(ans)

D. I hate factorization There are no restrictions on $ A $ or $ B $, so the round robin does not result in TLE: I prepared about 1000 pieces and calculated. Official commentary Looking at it, it seems that about 250 including negative numbers are enough. It is necessary to take abs once because the power root of a negative number is not output properly.

ABC166d.py


x=int(input())
l=[]

for i in range(300): #300**5=X less than 2430000000000^Number that can be represented by 5 (-x included) added
    a=i**5
    l.append(a)
    l.append(-a)
        
for j in l:
    for k in l:
        aa=j-k
        if j!=k and aa==x:
            ans=[]
            for z in [j,k]:
                aaa=abs(z)**(1/5) #5th root
                if z>=0:
                    ans.append(int(aaa))
                else:
                    ans.append(int(-aaa))
            print(*ans)                
            exit()

E. This Message Will Self-Destruct in 5s See Official Commentary for the approach. Prepare the sum of the index value and height and the difference between the index value and height. Then count the number of patterns where the two values match. Specifically, two arrays l and r are prepared, and the number of r having the same value for each element of l is counted. When I used count, it became TLE, so I used Counter to store it as an associative array and got to retrieve it. I haven't verified how fast it is ...

ABC166e_TLE.py


from collections import Counter

n=int(input())
a=list(map(int,input().split()))
ans=0
l=[]
r=[]
for i,j in enumerate(a):
    l.append(i+j)
    r.append(i-j)

for i in l:
    ans+=r.count(i)
    
print(ans)

ABC166e.py


from collections import Counter

n=int(input())
a=list(map(int,input().split()))
ans=0
l=[]
r=[]
for i,j in enumerate(a):
    l.append(i+j)
    r.append(i-j)

b=Counter(r)
for i in l:
    ans+=b.get(i,0)

print(ans)

If you can't solve D in about 30-40 minutes, the road to light blue will not be opened. I have to do my best

Recommended Posts

Atcoder ABC166 A-E in Python
Atcoder ABC169 A-E in Python
Atcoder ABC164 A-C in Python
Atcoder ABC167 A-D in Python
Atcoder ABC165 A-D in Python
AtCoder ABC177 A-D in python
AtCoder ABC187 Python
AtCoder ABC188 Python
AtCoder ABC 175 Python
Solve Atcoder ABC169 A-D in Python
Daily AtCoder # 36 in Python
Daily AtCoder # 2 in Python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Daily AtCoder # 53 in Python
Daily AtCoder # 33 in Python
Daily AtCoder # 7 in Python
Daily AtCoder # 24 in Python
Daily AtCoder # 37 in Python
Daily AtCoder # 8 in Python
Daily AtCoder # 42 in Python
Daily AtCoder # 17 in Python
Daily AtCoder # 54 in Python
Daily AtCoder # 11 in Python
Daily AtCoder # 47 in Python
Daily AtCoder # 13 in Python
Daily AtCoder # 45 in Python
Daily AtCoder # 30 in Python
Daily AtCoder # 40 in Python
Daily AtCoder # 10 in Python
Daily AtCoder # 5 in Python
Daily AtCoder # 28 in Python
Daily AtCoder # 39 in Python
Daily AtCoder # 19 in Python
Daily AtCoder # 52 in Python
Daily AtCoder # 3 in Python
Daily AtCoder # 14 in Python
Daily AtCoder # 50 in Python
Daily AtCoder # 26 in Python
Daily AtCoder # 4 in Python
Daily AtCoder # 43 in Python
Daily AtCoder # 29 in Python
Daily AtCoder # 22 in Python
Daily AtCoder # 49 in Python
Daily AtCoder # 27 in Python
Solve ABC169 in Python
Daily AtCoder # 25 in Python
Daily AtCoder # 16 in Python
Daily AtCoder # 12 in Python
Daily AtCoder # 48 in Python
Daily AtCoder # 23 in Python
Daily AtCoder # 34 in Python
Daily AtCoder # 51 in Python
Daily AtCoder # 31 in Python
Daily AtCoder # 46 in Python
Daily AtCoder # 35 in Python
Daily AtCoder # 9 in Python
Daily AtCoder # 44 in Python
Daily AtCoder # 41 in Python
AtCoder ABC 177 Python (A ~ E)