https://atcoder.jp/contests/abc154 A
s, t = input().split()
a, b = map(int, input().split())
u = input()
if u == s: a -= 1
else: b -= 1
print(a, b)
Branch by comparing only s with if statement.
Submission https://atcoder.jp/contests/abc154/submissions/9973325
B
s = input()
ans = 'x' * len(s)
print(ans)
(Length of input string) *'x'
is output.
I could write it in one line. print (len (input ()) *'x')
Submission https://atcoder.jp/contests/abc154/submissions/9975951
C
def main():
n = int(input())
a = list(map(int, input().split()))
seta = set(a)
ans = 'YES' if len(a) == len(seta) else 'NO'
print(ans)
main()
Since you only have to check if there are duplicate elements, you can combine the list with set ()
to make one duplicate element.
It's OK if you compare the number of these elements and check if they are equal.
Submission https://atcoder.jp/contests/abc154/submissions/9980825
D
def main():
n, k = map(int, input().split())
p = list(map(int, input().split()))
ex = [0 for _ in range(n)]
for i in range(n):
ex[i] = (p[i]+1)/2
sumex = sum(ex[:k])
tmp = sum(ex[:k])
for i in range(n-k):
tmp = tmp + (ex[i+k] - ex[i])
sumex = max(sumex, tmp)
print(sumex)
main()
If you calculate the sum of the expected values from j = i to i + k-1 in the range of i = 1 ~ n-1, in the worst case it will take N × K = 10 to the 10th power Submission)](https://atcoder.jp/contests/abc154/submissions/9998117).
Therefore, prepare the sum of the first to k expected values in tmp
as the initial value, and the i + 1th expected value is(ith expected value)-(ith expected value) + (i By setting + k + 1st expected value)
, you can avoid the trouble of calculating (an image to make a note of?). And updated by comparing with the maximum value sumex
at that time.
(Expected value of sum of p_i) = (sum of expected value of p_i), so the calculation of expected value may be summarized at the end.
Submission (after the contest is over) https://atcoder.jp/contests/abc154/submissions/10020043
I would like to add it if I can AC.
Added comments from @ c-yan. Regarding the description of the D problem, the following part seems to work faster after correction. Before correction
ex = [0 for _ in range(n)]
Revised
ex = [0] * n
Furthermore, in this case, you can write in one line including the part below this.
ex = [(p[i]+1)/2 for i in range(n)]
There was a helpful article here as a related article. 8 small differences in processing speed that Python should know
Recommended Posts