[PYTHON] AtCoder Beginner Contest 089 Review of past questions

Time required

スクリーンショット 2020-04-01 16.39.23.png

Impressions

This is a past question that I have already solved, but I made the same mistake again in the D problem. The content of the mistake is mentioned in the D problem section.

Problem A

It's a group of 3 people, so you can divide it by 3.

answerA1.py


print(int(input())//3)

B problem

All you have to do is count how many colors there are, and use set to eliminate the duplicates.

answerB.py


n=int(input())
s=input().split()
print("Three" if len(set(s))==3 else "Four")

C problem

First, check how many names start with each letter. Also, if you select three letters that start, you can find out how many there are when you select those three letters by multiplying how many names start with each letter. And since there are $ _5C_3 $ ways to choose the starting character, you can think about how many ways to choose the three people in the subject by finding the sum.

answerC.py


import itertools
n=int(input())
s=dict()
for i in ["M","A","R","C","H"]:
    s[i]=0
for i in range(n):
    _s=input()
    if _s[0] in ["M","A","R","C","H"]:
        s[_s[0]]+=1
s=list(s.items())
ans=0
for i in range(5):
    for j in range(i+1,5):
        for k in range(j+1,5):
            ans+=(s[i][1]*s[j][1]*s[k][1])
print(ans)

D problem

Since we have to process multiple queries for the interval **, we know that we are likely to use ** cumulative sum **. Also, it is necessary to consider ** the cumulative sum of magical powers **, but since the remainder of $ R_i $ and $ L_i $ divided by D is equal, ** what happens to the cumulative sum of magical powers with the remainder of each number * Consider *. First, I thought about storing the index of each cell in the array (num) separately for each remainder written in the cell, but ** it is not necessary to store it separately ** (the remainder in the cumulative sum) I noticed after implementation that [Writer solution](see https://img.atcoder.jp/abc089/editorial.pdf) for details. Also, in this implementation, those with remainders i = 1 to d-1 are stored in the i-1st nested array, those with i = 0 are stored in the d-1th, etc. ** Index processing is performed. It was very troublesome **. Here, the information of the squares is stored in the array divided by the remainder of the numbers, so from here we will store the cumulative sum of magic power in another array (mp). The calculation of magical power is simply calculated from the Manhattan distance. If you can find this cumulative sum, then you can output the total magic power corresponding to the query. Also, the cumulative magical power required to go from `num [g] [0] ``` to num [g] [k] ``` is `` mp [g] [k]Note that it is necessary to output ``mp [g] [f] -mp [g] [e]` when ```e! = 0``` because it is. ..

answerD.py


from math import ceil
h,w,d=map(int,input().split())
x=ceil((h*w)/d)
num=[[[-1,-1] for j in range(x)] for i in range(d)]#Case by too much(1→0)
mp=[[0]*x for i in range(d)]
a=[list(map(int,input().split())) for i in range(h)]
for i in range(h):
    for j in range(w):
        k,l=a[i][j]%d,a[i][j]//d
        if k==0:
            num[d-1][l-1]=[i,j]
        else:
            num[k-1][l]=[i,j]
for i in range(d):
    for j in range(x):
        if j!=x-1:
            if num[i][j+1]!=[-1,-1]:
                mp[i][j+1]+=(mp[i][j]+abs(num[i][j+1][0]-num[i][j][0])+abs(num[i][j+1][1]-num[i][j][1]))
            else:
                break
q=int(input())
for i in range(q):
    l,r=map(int,input().split())
    if l%d!=0:
        e,f=l//d,r//d
    else:
        e,f=l//d-1,r//d-1
    g= d-1 if l%d==0 else l%d-1
    if e==0:
        print(mp[g][f])
    else:
        print(mp[g][f]-mp[g][e])

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 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 117 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 076 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 047 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 083 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