Last time We will do Boot camp for Beginners.
#37 ARC091-C
** Thoughts **
Since the number of operations is an even number, the edge will always be a table at the end. Similarly, elements other than edges are manipulated an odd number of times, so they are flipped at the end. Based on the above idea, the number of sheets on the back at the end is $ (n-2) * (m-2) $. However, this does not hold for $ n \ leq2, m \ leq2
n, m = map(int,input().split())
if n == 1 and m == 1:
print(1)
elif n == 1 or m == 1:
print(max(max(n,m)-2,0))
else:
print((n-2)*(m-2))
** Thoughts ** If the multiple of 4 is more than half of all the elements, you can create a sequence that satisfies the condition. What we have to think about here is the process of 2. Unlike other non-multiples of 4, 2 satisfies the condition if it is continuous. Therefore, by subtracting the number of c2 from n and adding 1 to it, the number of 2 can be ignored in the calculation.
n = int(input())
a = list(map(int,input().split()))
c1 = 0
c2 = 0
c4 = 0
for i in range(n):
if a[i] % 4 == 0:
c4 += 1
elif a[i] % 2 == 0:
c2 += 1
elif a[i] == 1:
c1 += 1
n -= c2
c2 %= 2
n += c2
f = n // 2
if c4 >= f:
print('Yes')
quit()
else:
print('No')
** Thoughts ** Just pick up s one by one with for and add it to the string. 'B' was implemented as a slice.
s = input()
ans = ''
for i in range(len(s)):
if s[i] == 'B':
ans = ans[:-1]
elif s[i] == '0':
ans += '0'
else:
ans += '1'
#print(ans)
print(ans)
** Thoughts ** I think it's an integer problem. Explanation of formula
import fractions
a, b, c, d = map(int,input().split())
l = int(fractions.gcd(c,d) * c / fractions.gcd(c,d) * d / fractions.gcd(c,d))
#print(l)
x = b // c - a // c
y = b // d - a // d
z = b // l - a // l
if a % c == 0 or a % d == 0:
print(int(b-a-(x+y-z)))
else:
print(int(b-a-(x+y-z))+1)
#print(x,y,z)
** Thoughts ** The answer is the greatest common divisor of the differences between the elements, so just calculate
import fractions
n, y = map(int,input().split())
x = list(map(int,input().split()))
x.append(y)
x.sort()
d = []
for i in range(n):
d.append(x[i+1]-x[i])
if len(d) !=1:
ans = fractions.gcd(d[0],d[1])
for i in range(n-1):
ans = fractions.gcd(ans,d[i])
print(ans)
else:
ans = d[0]
print(ans)
** Thoughts ** It is the same to change the height of all flowers from 0 to $ h $ and to change the height of all flowers from $ h $ to 0. Adjacent flowers that have not reached each $ h $ will be watered less often. Also, do not water flowers that have reached $ h $. Therefore, we check whether the number of flowers is 0 and calculate the maximum range that can be watered at one time.
n = int(input())
h = list(map(int,input().split()))
ans = 0
for i in range(max(h)):
c = 0
for j in range(n):
if h[j] != 0:
h[j] -= 1
c += 1
else:
if c != 0:
ans += 1
c = 0
if j == n-1 and c != 0:
ans += 1
print(ans)
I have to finish my school homework ... see you.
Recommended Posts