The code in AtCoder Beginners Selection Experience doesn't work anymore in Python 2, so I tried to solve it again to see how much it has improved.
PracticeA - Welcome to AtCoder
8 minutes → 2 minutes. The space-separated character string was generated by%, but it changed when I realized that if I passed it to print separated by commas, it would be separated by spaces.
a = int(raw_input())
b, c = [int(e) for e in raw_input().split()]
s = raw_input()
print "%d %s" % (a + b + c, s)
↓
a = int(input())
b, c = map(int, input().split())
s = input()
print(a + b + c, s)
2 minutes → 1 minute. The order of Odd and Even was reversed.
a, b = [int(e) for e in raw_input().split()]
if a * b % 2 == 0:
print 'Even'
else:
print 'Odd'
↓
a, b = map(int, input().split())
if a * b % 2 == 1:
print('Odd')
else:
print('Even')
4 minutes → 30 seconds. It seems that I didn't know count at that time.
print len([c for c in raw_input() if c == '1'])
↓
s = input()
print(s.count('1'))
9 minutes → 3 and a half minutes. One-shot reading by open (0) .read () appeared. The policy was very different from processing one by one and repeating the check whether all were broken once.
n = int(raw_input())
a = [int(e) for e in raw_input().split()]
i = 0
while True:
if any(e % 2 == 1 for e in a):
break
i += 1
a = [e / 2 for e in a]
print i
↓
N, *A = map(int, open(0).read().split())
result = float('inf')
for a in A:
t = 0
while a % 2 == 0:
t += 1
a //= 2
result = min(result, t)
print(result)
4 minutes → 2 minutes. Brute force, there is no big difference.
a = int(raw_input())
b = int(raw_input())
c = int(raw_input())
x = int(raw_input())
result = 0
for i in range(a + 1):
for j in range(b + 1):
for k in range(c + 1):
if i * 500 + j * 100 + k * 50 == x:
result += 1
print result
↓
A, B, C, X = map(int, open(0).read().split())
result = 0
for a in range(A + 1):
for b in range(B + 1):
for c in range(C + 1):
if X == 500 * a + 100 * b + 50 * c:
result += 1
print(result)
6 minutes → 3 minutes. The policy was different, whether to calculate the sum of each digit directly or convert it to a character string.
n, a, b = [int(e) for e in raw_input().split()]
result = 0
for i in range(1, n + 1):
if a <= sum(int(e) for e in str(i)) <= b:
result += i
print result
↓
N, A, B = map(int, input().split())
result = 0
for i in range(1, N + 1):
c = 0
t = i
while t != 0:
c += t % 10
t //= 10
if A <= c <= B:
result += i
print(result)
7 minutes → 1 and a half minutes. The policy is almost the same, but it seems that I did not know the reverse option of sort at that time.
n = int(raw_input())
a = [int(e) for e in raw_input().split()]
a.sort()
a.reverse()
print sum(a[::2]) - sum(a[1::2])
↓
N, *a = map(int, open(0).read().split())
a.sort(reverse=True)
print(sum(a[::2]) - sum(a[1::2]))
3 minutes → 1 and a half minutes. I remember this.
n = int(raw_input())
d = [int(raw_input()) for i in range(n)]
print len(set(d))
↓
N, *d = map(int, open(0).read().split())
print(len(set(d)))
7 minutes → 3 minutes. Well, it's a brute force attack, so there's not much difference in policy. Only the usage of print has changed.
import sys
n, y = [int(e) for e in raw_input().split()]
for i in range(n + 1):
for j in range(n + 1 - i):
k = n - i - j
if 10000 * i + 5000 * j + 1000 * k == y:
print "%d %d %d" % (i, j, k)
sys.exit()
print "-1 -1 -1"
↓
N, Y = map(int, input().split())
for i in range(N + 1):
for j in range(N + 1 - i):
k = N - i - j
if 10000 * i + 5000 * j + 1000 * k == Y:
print(i, j, k)
exit()
print(-1, -1, -1)
About 1 hour → 12 minutes. I remembered that I should flip it. However, was it screwed down in the forward direction with the * O * (* N * 2 </ sup>) algorithm last time? The speed increased from 1223ms to 41ms.
import sys
s = raw_input()
ts = ['']
while True:
nts= []
for t in ts:
for w in ['dreamer', 'eraser', 'dream', 'erase']:
if s == t + w:
print 'YES'
sys.exit()
if s.startswith(t + w):
nts.append(t + w)
if len(nts) == 0:
print 'NO'
sys.exit()
ts = nts
↓
S = input()
S = S[::-1]
candidates = [s[::-1] for s in ['dream', 'dreamer', 'erase', 'eraser']]
i = 0
while i < len(S):
for c in candidates:
if S.startswith(c, i):
i += len(c)
break
else:
print('NO')
exit()
print('YES')
About 40 minutes → 8 and a half minutes. I switched to buffered IO, so 337ms → 131ms. It's almost the same, but the code has changed a lot because it's different whether or not the input is processed together at the beginning.
import sys
n = int(raw_input())
data = [map(int, raw_input().split()) for i in range(n)]
t = 0
x = 0
y = 0
for d in data:
duration = d[0] - t
distance = abs(x - d[1]) + abs(y - d[2])
if (distance > duration) or ((duration - distance) % 2 == 1):
print 'No'
sys.exit()
t = d[0]
x = d[1]
y = d[2]
print 'Yes'
↓
from sys import stdin
readline = stdin.readline
N = int(readline())
pt, px, py = 0, 0, 0
for _ in range(N):
t, x, y = map(int, readline().split())
d = abs(x - px) + abs(y - py)
if d > t - pt:
print('No')
break
if (t - pt - d) % 2 == 1:
print('No')
break
pt, px, py = t, x, y
else:
print('Yes')