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.
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")
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")
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)
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.
(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.).
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