I will keep a record of the problems that I made a mistake in the past questions of AtCoder and the problems that left an impression on me.
ABC102-C (Linear Approximation)
First, set $ B_i = A_i-i $, then look at the integer sequence of $ B_i $ in order from the front, and when you are between $ B_i-i and B_ {i + 1}-(i + 1) $ I thought about whether it would be the minimum value in order, but I saw the answer because the case classification of the boundary did not go well and it was WA all the time. (I would like to AC this method in the near future.) The answer stated that the minimum value of the sum of the absolute values was the median **, but I couldn't understand it well, so I understood it by drawing the figure below. (When the length of the integer sequence is 5) The red part is counted more than when the median is b, but you can see that the median is certainly the minimum.
answerC.py
n=int(input())
s=input().split()
#Paraphrasing the problem statement here is also important
a=sorted([int(s[i])-i-1 for i in range(n)])
c=0
for i in range(n):
if i<=n//2:
c+=(a[n//2]-a[i])
else:
c+=(a[i]-a[n//2])
print(c)
2WA
Since you can only swap two adjacent ones, is it clear that swapping from the front is the least frequent? (How can I prove it ... ??) When $ p_i = i $, swap $ p_i $ and $ p_ {i + 1} $ to make $ p_i ≠ i $, and repeat i = 1 ~ n-1 in order, i <= n-1 Since $ p_i ≠ i $, swap with $ p_n $ and $ p_ {n-1} $ only when the last i = n is $ p_i = i $. ** Also, since $ p_n = n $ at this time, it should be noted that even if swapped, $ p_ {n-1} ≠ n-1 $ is satisfied. ** Also, when I was solving it, I didn't notice the preamble and I was spinning the loop again with i = n-1 ~ 1. (It's clear that when you loop around, only $ p_n $ and $ p_ {n-1} $ swap occur ...)
answerD_bad.py
n=int(input())
f=list(map(int,input().split()))
c=0
for i in range(n-1):
if f[i]==i+1:
c+=1
f[i],f[i+1]=f[i+1],f[i]
for i in range(n-1,0,-1):
if f1[i]==i+1:
c+=1
f[i],f[i-1]=f[i-1],f[i]
print(c)
answerD_good.py
n=int(input())
f=list(map(int,input().split()))
c=0
for i in range(n-1):
if f[i]==i+1:
c+=1
f[i],f[i+1]=f[i+1],f[i]
if f[n-1]==n:
print(c)
3WA,2RE
It's not a difficult problem, but I made a lot of mistakes because I misunderstood that the color was only the color in the problem statement, or forgot to ask for the minimum and maximum. ** I want to read the problem statement properly, no matter how easy it is. ** ** Also, the code was verbose and terrible, so I rewrote it. ** Don't get verbose code, it's hard to read. ** **
answerC_bad.py
n=int(input())
a=sorted(list(map(int,input().split())))
c=[0]*8
k=n
for i in range(n):
if a[i]<400:
c[0]=1
elif a[i]<800:
c[1]=1
elif a[i]<1200:
c[2]=1
elif a[i]<1600:
c[3]=1
elif a[i]<2000:
c[4]=1
elif a[i]<2400:
c[5]=1
elif a[i]<2800:
c[6]=1
elif a[i]<3200:
c[7]=1
else:
k=i
break
d=0
for i in range(8):
if c[i]==1:
d+=1
if d==0:
print(1, end=" ")
else:
print(d, end=" ")
print(d+(n-k))
answerC_good.py
n=int(input())
a=sorted(list(map(int,input().split())))
c=[0]*8
k=n
for i in range(n):
for j in range(8):
if 400*j<=a[i]<400*(j+1):
c[j]=1
break
else:
k=i
break
d=c.count(1)
if d==0:
print(1, end=" ")
else:
print(d, end=" ")
print(d+(n-k))
Recommended Posts