A. Soccer
若x2 != x1且两队优劣较之前无有变化,则有可能从未相同
t = int(input())
for _ in range(t):
x1, y1 = map(int, input().split())
x2, y2 = map(int, input().split())
if x2 != y2 and ((x1 > y1 and x2 > y2) or (x1 < y1 and x2 < y2)):
print("YES")
else:
print("NO")
B. Collatz Conjecture
模拟,数学
每次加到能整除k,然后除到不能整除为止,剩下不够加到能整除,x直接加上剩余k,打印答案
若x在过程中变为1,则每k-1轮会再次变成1,退出单独判断
t = int(input())
for _ in range(t):
x, y, k = map(int, input().split())
while k > 0:
nd = y - (x % y)
if k > nd:
x += nd
k -= nd
else:
break
while x % y == 0:
x //= y
if x == 1:
break
if k >= (y-1) and x == 1:
k %= (y-1)
if k > 0:
x += k
while x % y == 0:
x //= y
print(x)
C. Boring Day
数据结构,双端队列,贪心
用双端队列维护当前轮的牌,记录总点数,按顺序拿牌,每次能获胜则直接结束,否则若小于l则继续拿,大于r则将最前面的牌移出,直到当前牌空或者不再大于r,以模拟该牌在之前被放弃的情况
from collections import *
def check(x):
global s, d, res
if l <= x <= r:
res += 1
s = 0
d = deque()
return True
return False
t = int(input())
for _ in range(t):
n, l, r = map(int, input().split())
a = list(map(int, input().split()))
res = 0
d, s = deque(), 0
for i in range(n):
d.append(a[i])
s += a[i]
if check(s):
continue
elif s > r:
while s > r:
s -= d.popleft()
if check(s):
continue
print(res)
D. Beauty of the mountains
暴力枚举,前缀和,数学
要使两种山的高度和相同,只需要消除山高度的总差值,
每次可以使k*k的子矩阵同时加减任意数,只需计算每个子矩阵0,1的数量差,每次操作可使山的差距变化差值的整数倍
当存在一些差值的gcd是山总差值的因子时,则可通过重复多次固定的操作将山的总差值消除
from math import gcd
def check():
c = 0
for i in range(n):
for j in range(m):
if mp[i][j] == 1:
c += a[i][j]
else:
c -= a[i][j]
c = abs(c)
if c == 0:
return True
s = [[0] * (m + 1) for _ in range(n + 1)]
for i in range(1, n + 1):
for j in range(1, m + 1):
s[i][j] = s[i][j - 1] + s[i - 1][j] - s[i - 1][j - 1] + mp[i - 1][j - 1]
k2 = k * k
now = -1
for i in range(1, n - k + 2):
for j in range(1, m - k + 2):
x, y = i + k - 1, j + k - 1
one = s[x][y] - s[i - 1][y] - s[x][j - 1] + s[i - 1][j - 1]
zero = k2 - one
b = abs(one - zero)
if b == 0:
continue
elif now == -1:
now = b
else:
now = gcd(b, now)
if c % now == 0:
return True
return False
t = int(input())
for _ in range(t):
n, m, k = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
mp = [list(map(int, list(input()))) for _ in range(n)]
if check():
print("YES")
else:
print("NO")