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.
It's a group of 3 people, so you can divide it by 3.
answerA1.py
print(int(input())//3)
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")
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)
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