[PYTHON] AtCoder Past Question Review (12 / 6,7)

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)

6WA, saw the answer

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) IMG_0092.PNG 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)

ABC72-D(Derangement)

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)

ABC064-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

AtCoder Past Question Review (12 / 6,7)
AtCoder Past Question Review (12/5)
AtCoder Beginners Contest Past Question Challenge 6
AtCoder Beginners Contest Past Question Challenge 4
AtCoder Grand Contest Past Question Challenge 2
AtCoder Beginners Contest Past Question Challenge 9
AtCoder Beginners Contest Past Question Challenge 7
AtCoder Grand Contest Past Question Challenge 1
AtCoder Beginners Contest Past Question Challenge 10
AtCoder Beginner Contest 066 Review past questions
AtCoder Beginners Contest Past Question Challenge 5
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 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