A
签到,小心精度问题
x = int(input())
a = x-5
b = x * 0.8
if abs(a - b) <= 1e-5:
print(0)
elif a < b:
print(5)
else:
print(8)
B
贪心 + 最大堆
for _ in range(int(input())):
n,m,k = map(int,input().strip().split())
a = map(int,input().strip().split())
from heapq import *
heap = []
for num in a:
heappush(heap,-abs(num-k))
if len(heap) > m:
heappop(heap)
print(-heap[0])
C
栈模拟,编号:
- 小写字母编号为
[0,25]
- 大写字母编号为
[100,125]
n = int(input())
q = []
for w in input().strip():
if w.islower():
w = ord(w) - ord('a')
else:
w = ord(w) - ord('A') + 100
if q and w + q[-1] == 25 and w >= q[-1]:
q.pop()
elif q and w + q[-1] == 225 and w <= q[-1]:
q.pop()
else:
q.append(w)
print(len(q))
D
找规律:
- 选
1
然后替换成4
,相当于加3
:
因此
1 4 7 10 13
都是能满足的,即x % 3 == 1
- 选
1
然后替换成9
,然后后面选1
替换成4
:
因此
9 12 15 18 21
都是能满足的,即x % 3 == 0 and x >= 9
- 选
1
然后替换成9
,再选1
然后替换成9
,最后后面选1
替换成4
:
因此
17 20 23 26
都是能满足的,即x % 3 == 2 and x >= 17
for _ in range(int(input())):
x = int(input())
'''
1
a*a
a*a-1+b*b
a*a+b*b-2+c*c
4+4+4+4
1 4 7 10 13 16
9 12 15 18
17 20
'''
if x % 3 == 1:
print("Yes")
elif x % 3 == 0 and x >= 9:
print("Yes")
elif x % 3 == 2 and x >= 17:
print("Yes")
else:
print("No")
E
记忆化搜索:
dfs(i,lw,f)
代表:
- 第
i
个字符 lw
上个字符f
是否翻转
'''
dp
'''
from types import GeneratorType
def bootstrap(f, stack=[]): #yield
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
import sys
for _ in range(int(input())):
n = int(sys.stdin.readline())
s = sys.stdin.readline().strip()
C = list(map(int,sys.stdin.readline().strip().split()))
mem = {}
@bootstrap
def dfs(i,lw,f):
key = (i,lw,f)
if key in mem:
return (yield mem[key])
if i == n:
return (yield 0)
w,c = int(s[i]),C[i]
if f:
w = 1 - w
i += 1
if w == lw:
res = yield dfs(i,w,f)
# 反转
if not w:
B = (yield dfs(i,1,not f)) + c
if B < res:
res = B
elif not w:
# 必须反转
res = (yield dfs(i,1,not f)) + c
else:
res = yield dfs(i,w,f)
# 反转
B = (yield dfs(i,0,not f))+c
if B < res:
res = B
mem[key] = res
return (yield res)
res = dfs(0,0,False)
print(res)
F
线性基
,先套用线性基模板,求出异或的基本集
然后从小到大判断 1 << i
能不能通过线性基
获取,若不能,答案就是1 << i
'''
线性基
'''
for _ in range(int(input())):
n = int(input())
a = map(int,input().strip().split())
level = 62
dt = {}
for num in a:
for i in range(level-1,-1,-1):
if (1 << i)&num:
if (1 << i) not in dt:
dt[(1 << i)] = num
break
else:
num ^= dt[(1 << i)]
def check(num):
cur = 0
for i in range(level-1,-1,-1):
if (1 << i)&cur == (1 << i)&num:
continue
if (1 << i) not in dt:
return False
cur ^= dt[(1 << i)]
return True
# 小到大检查 1 << i 能不能从线性基得到
for i in range(level):
if not check(1 << i):
print(1 << i)
break