[PYTHON] AtCoder Beginner Contest 104 Review of past questions

Time required

スクリーンショット 2020-05-20 22.58.36.png

Impressions

It's no good that you can't solve the problem in the D level band. ** I haven't been able to squeeze my intelligence to find clues ** at all. I will try to keep my mind tight during the contest.

Problem A

You can divide it by less than 1200 or less than 2800. It's easy to write using the ternary operator.

answerA.py


r=int(input())
print("ABC" if r<1200 else "ARC" if r<2800 else "AGC")

B problem

Although it was a B problem, it was quite troublesome. The second condition can be easily processed, but the last condition is to extract only the relevant part of the character string and judge whether they are all lowercase. You can use the islower () function to determine if it is lowercase.

answerB.py


s=input()

if s[0]=="A" and s[2:-1].count("C")==1:
    if (s[1]+s[2:2+s[2:-1].index("C")]+s[s[2:-1].index("C")+3:]).islower():
        print("AC")
    else:
        print("WA")
else:
    print("WA")

C problem

This problem was decided quickly, but I made a silly mistake (** break statement was forgotten ** and ** variable was written incorrectly **).

First of all, if you think about it normally, you should choose from the ones with the highest basic score, but depending on the complete score, which point of problem you should solve depends on **.

Here, I focused on the fact that there are only D ($ \ leqq $ 10) types of problems, and thought that ** I should decide which problem should be completed for each **. In other words, you can search all the set of problems that should be completed by bit full search.

Once you have decided which questions to complete, you can choose the ones with the highest score (✳︎) among the questions you do not choose. Also, since the problem to be completed is fixed, (✳︎) cannot be completed, and if you do not get a G point or higher without completing **, you need to consider the following pattern **.

answerC.py


import math
d,g=map(int,input().split())
pc=[list(map(int,input().split())) for i in range(d)]
ans=10000000000000000000
for i in range(1<<d):
    s=0
    ans_=0
    for j in range(d):
        if (i>>j)&1:
            s+=(pc[j][1]+pc[j][0]*100*(j+1))
            ans_+=pc[j][0]
    if s>=g:
        ans=min(ans,ans_)
    else:
        for j in range(d-1,-1,-1):
            if not((i>>j)&1):
                if s+(pc[j][0]*100*(j+1))>=g:
                    h=-((s-g)//(100*(j+1)))
                    s+=(h*100*(j+1))
                    ans_+=h
                    ans=min(ans,ans_)
                    break
                else:
                    break
print(ans)

D problem

At the beginning, I thought that I was not good at character strings (maybe because there are many DP patterns), but I ended up with a tail ... ** You need to get rid of your weaknesses and tackle the problem **….

Since the number of ABCs can be determined ** in order (gradually) from the front to A, B, C **, it can be suspected that DP can be used (experiments using the $ \ because $ sample). To do). Also, since it is difficult to think about A → B → C at once, you can think that you should decide separately for A → B and B → C (✳︎).

Therefore, you can use (✳︎) while using ** DP if you divide it into the state where ** A is decided, the state where AB is decided, and the state where ABC is decided. The discussion so far has been pretty good, but I couldn't fully consider the processing of a character?

In fact,? Can be simply ** A, B, C by trying all three patterns and adding them together **. Also, in fact, ** a state where nothing has been decided ** is also necessary as a state, so add this.

From the above consideration, we can see that the following dp should be defined.

IMG_0382.PNG

(When I defined DP, I felt it was important to verbalize what each state was.)

If you decide so far **, you can think about the transition **. There are many patterns in which the transition is $ dp [i] [j] = dp [i-1] [j] $, but the pattern increases depending on whether the $ i $ th character is $ A, B, or C $. I will. That is, when the i-th character is A, it is necessary to add the pattern of $ dp [i-1] [3] $ to $ dp [i] [0] $, and when the i-th character is B, it is necessary to add the pattern. It is necessary to add the pattern of $ dp [i-1] [0] $ to $ dp [i] [1] $, and when the i-th character is C, $ dp [i] [2] $ to $ You need to add the pattern of dp [i-1] [1] $. Furthermore, if the i-th character is?, There are patterns of A, B, and C, so by adding all of them, $ dp [i] = [3 * dp [i-1] [0] + dp [i-1] [3], 3 * dp [i-1] [1] + dp [i-1] [0], 3 * dp [i-1] [2] + dp [i-1] [ It becomes 1], 3 * dp [i-1] [3]] $.

This is not enough, but the following figure can explain the example of the first sample (three are arranged vertically when? Is A and B is. If it was C.).

IMG_0383.JPG

The above is implemented and it becomes as follows. It is easy to make a mistake in what happens to dp [0], so you need to be careful.

✳︎ ** Since greedy problems and substring problems have a high probability of DP **, ** I want to calmly make a policy ** and ** Think carefully about the actual transition ** I would like to be careful.

answerD.py


mod=10**9+7
s=input()
l=len(s)
dp=[[0]*4 for i in range(l)]
for i in range(l):
    if i==0:
        dp[i][3]=1
        if s[i]=="A":
            dp[i][0]=1
        elif s[i]=="?":
            dp[i][0]=1
            dp[i][3]=3
    else:
        if s[i]=="A":
            dp[i]=[(dp[i-1][0]+dp[i-1][3])%mod,dp[i-1][1],dp[i-1][2],dp[i-1][3]]
        elif s[i]=="B":
            dp[i]=[dp[i-1][0],(dp[i-1][0]+dp[i-1][1])%mod,dp[i-1][2],dp[i-1][3]]
        elif s[i]=="C":
            dp[i]=[dp[i-1][0],dp[i-1][1],(dp[i-1][1]+dp[i-1][2])%mod,dp[i-1][3]]
        else:
            dp[i]=[(3*dp[i-1][0]+dp[i-1][3])%mod,(3*dp[i-1][1]+dp[i-1][0])%mod,(3*dp[i-1][2]+dp[i-1][1])%mod,(3*dp[i-1][3])%mod]
print(dp[-1][2])

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 069 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 049 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
AtCoder Beginner Contest 114 Review of past questions
AtCoder Beginner Contest 045 Review of past questions
AtCoder Beginner Contest 120 Review of past questions
AtCoder Beginner Contest 108 Review of past questions