A. Recovering a Small String
暴力,字符串
每个位置至少分配1,剩余的优先向后分配
t = int(input())
for _ in range(t):
n = int(input())
res = [1, 1, 1]
n -= 3
i = 2
while n > 0 and i >= 0:
now = min(n, 25)
res[i] += now
n -= now
i -= 1
for i in res:
print(chr(i-1+ord("a")), end="")
print()
B. Make Equal
贪心
遍历一遍,每个位置留平均值的水量,不够则不可能
def check():
avg = sum(a)//n
now = 0
for ai in a:
if ai + now < avg:
return False
now += ai-avg
return True
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
if check():
print("YES")
else:
print("NO")
C. Make Equal Again
贪心
最后剩的一定是开头或者是末尾的一种,将其他的变成开头或者结尾取最大即可
开头结尾有可能是相同数字,这时只需改变中间的元素
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
now, i = a[0], 0
while i < n and a[i] == now:
i += 1
now, j = a[-1], n-1
while j >= 0 and a[j] == now:
j -= 1
if a[0] == a[-1]:
res = max(0, n-i-(n-j-1))
else:
res = n - max(i, n-j-1)
print(res)
D. Divisible Pairs
数学,模拟,数论
可以发现,一对符合条件时,ai%x + aj%x = x或0,ai%y - aj%y = 0
那么只要找对x取模和为x或0,对y取模值相等的ai,aj即可(当时脑抽了没想到。。。)
将数组分别对x和y取模,存储在mx和my内,因为ai%y和aj%y相等,将my存一下元素的索引然后排序,那么%y相等的就会在相邻的位置,
分别对每种对y模数看其中有多少对%x和为x或为0就能包含所有情况,
用计数器存一下%x各数值数目,符合条件取一次答案
from collections import Counter
t = int(input())
for _ in range(t):
n, x, y = map(int, input().split())
a = list(map(int, input().split()))
mx = [a[i] % x for i in range(n)]
my = [(a[i] % y, i) for i in range(n)]
my.sort()
res = 0
i = 0
while i < n:
now = my[i][0]
c = Counter()
c[mx[my[i][1]]] += 1
while i+1 < n and my[i+1][0] == now:
i += 1
mxi = mx[my[i][1]]
if mxi == 0:
res += c[0]
res += c[x-mxi]
c[mxi] += 1
i += 1
print(res)
E. Anna and the Valentine's Day Gift
博弈论,贪心
简单的博弈论,anna可以将数字逆转过来,Sasha可以使两数字任意顺序拼接,最后判断最后一个数字是否小于10^m
anna会尽可能使最后的数值更小,Sasha相反,最后只需看剩下数字的长度即可,为了让数字长度更小,anna会优先让当前末尾0最多的数字逆转,而Sasha会优先让末尾0最多的拼在另一数字前面,来保护0,0出现在中间后不会再影响数字长度
将数字按末尾0降序排序,每当到偶数索引数字会减少末尾零个位数,最后判断长度是否大于m
def z_num(s):
n = len(s)
i = 0
while s[-i-1] == "0":
i += 1
return i
t = int(input())
for _ in range(t):
n, m = map(int, input().split())
a = list(input().split())
a = [[z_num(ai), ai] for ai in a]
a.sort(reverse=True)
res = 0
for i in range(n):
res += len(a[i][1])
if i % 2 == 0:
res -= a[i][0]
if res > m:
print("Sasha")
else:
print("Anna")