A. Phone Desktop
https://codeforces.com/contest/1974/problem/A
签到题,贪心
一个屏幕最多能装两个大方格,先放完大的,剩下的空隙放小的,不够再补
t = int(input())
for _ in range(t):
x, y = map(int, input().split())
res = 0
if y % 2 == 0:
res += y//2
las = 7*res
else:
res += (y+1)//2
las = res*7+4
if x > las:
res += (x-las+14)//15
print(res)
B. Symmetric Encoding
https://codeforces.com/contest/1974/problem/B
排序
存下出现的字符,排序后第i个对应第m-i-1个字符
from collections import Counter
t = int(input())
for _ in range(t):
n = int(input())
b = list(input())
lis = [0]*150
for bi in b:
lis[ord(bi)] = 1
lis = [i for i in range(150) if lis[i] == 1]
lis.sort()
dic = [0]*150
m = len(lis)
for i in range(m):
dic[lis[i]] = m-i-1
for bi in b:
print(chr(lis[dic[ord(bi)]]), end="")
print()
C. Beautiful Triple Pairs
求只有一个固定位置不同,其余相同的三元组对数
哈希表存一下不同的分别为第1,2,3位时,不同取值对应的剩下元素值
最后计算该位置取值个数与,其他位置相同但该位置不同的个数的乘积
避免重复计算,每次计算后可以将该位置字符个数减掉
from collections import *
M = int(1e6+10)
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
c = [defaultdict(lambda: Counter()) for _ in range(3)]
for i in range(1, n-1):
x, y, z = a[i-1], a[i], a[i+1]
c[0][(y, z)][x] += 1
c[1][(x, z)][y] += 1
c[2][(x, y)][z] += 1
res = 0
for i in range(3):
for ci in c[i]:
num = sum(c[i][ci].values())
for x in c[i][ci]:
ct = c[i][ci][x]
res += (num-ct)*ct
num -= ct
print(res)
D. Ingenuity-2
模拟
要使两者能走到同一点,那么把所有操作累加起来最终x,y一定能平分,否则不行
将最终x,y除二,即可得到他们的终点,然后模拟其中一者走到终点的过程即可
注意,若以(0, 0)为终点时,要手动使其完成一次来回的操作,防止出现未行动的可能
最后判断终点是否正确,是否都进行过行动
def solve():
x, y = 0, 0
for si in s:
x, y = x+dxy[si][0], y+dxy[si][1]
if (x % 2 == 1) or (y % 2 == 1):
return "NO"
if x == 0 and y == 0:
flag = 1
else:
flag = 0
x, y = x//2, y//2
res = [0]*n
for i in range(n):
si = s[i]
# print(dxy[si], x, y, end="-->")
if si == "E" or si == "W":
dx = dxy[si][0]
if abs(x-dx) < abs(x):
x -= dx
res[i] = 1
else:
dy = dxy[si][1]
if abs(y-dy) < abs(y):
y -= dy
res[i] = 1
# print(x, y)
if x == 0 and y == 0:
if flag == 1:
flag = 0
x, y = x-dxy[si][0], y-dxy[si][1]
res[i] = 1
else:
break
if x != 0 or y != 0:
return "NO"
res = ["R" if i == 1 else "H" for i in res]
R = res.count("R")
# print(x, y, res)
if R != 0 and R != n:
return "".join(res)
return "NO"
dxy = {"N": (0, 1), "S": (0, -1), "E": (1, 0), "W": (-1, 0)}
t = int(input())
for _ in range(t):
n = int(input())
s = input()
res = solve()
print(res)
E. Money Buys Happiness
动态规划,贪心
观察数据,发现c很大但m和h很小,考虑以当前的幸福度为容量进行dp,
哈希表存储当前幸福的所需花费的最小值,遍历每一月,若钱数符合则更新哈希表取最小花费,最后找能得到的最大幸福度
t = int(input())
for _ in range(t):
m, x = map(int, input().split())
ch = [list(map(int, input().split())) for _ in range(m)]
money = 0
w = {0: 0}
for i in range(m):
c, h = ch[i]
nw = w.copy()
for j in w:
if money-w[j] >= c:
if nw.get(j+h, -1) == -1:
nw[j+h] = int(1e18)
nw[j+h] = min(nw[j+h], w[j]+c)
money += x
w = nw
res = max(w.keys())
print(res)