A:正着写不方便,那倒着写就行了
from collections import deque
n = int(input())
s = input()
cur = n
ans = deque([cur])
cur -= 1
for i in s[::-1]:
if i == 'L':
ans.append(cur)
else:
ans.appendleft(cur)
cur -= 1
print(*ans)
B:找规律构造
n,a,b = map(int,input().split())
ans = [[''] for _ in range(n)]
for i in range(n):
for j in range(n):
if (i+j)%2 == 0:
ans[i][0] += '.'*b
else:
ans[i][0] += '#'*b
for i in ans:
for j in range(a):
print(i[0])
C:水题
n,x = map(int,input().split())
a = list(map(int,input().split()))
print(a.index(x)+1)
D:x&y=a(a二进制为1的位x和y也必须为1),x+y=s,s为剩下几位的和,即这几位不能和前面重合
for _ in range(int(input())):
def func():
a, s = map(int, input().split())
c = s - 2 * a
if c < 0:return False
for i in range(61):
if a&(1<<i) and c&(1<<i):
return False
return True
if func():print('Yes')
else:print('No')
E:标准前缀和哈希
n,k = map(int,input().split())
a = list(map(int,input().split()))
from collections import Counter
cnt = Counter([0])
ans = 0
pre = 0
for i in a:
pre += i
ans += cnt[pre-k]
cnt[pre] += 1
print(ans)
F:枚举a,b然后算出c
n = int(input())
ans = 0
for a in range(1,n+1):
if a*a*a > n:break
for b in range(a,n+1):
if a*b*b > n:break
ans += n//(a*b)+1-b
print(ans)
G:从终点开始枚举bfs,最后看哪些点是必须要遍历的,全部加上就是答案了
n = int(input())
v = []
nums = []
for _ in range(n):
lst = list(map(lambda x:int(x)-1,input().split()))
v.append(lst[0]+1)
nums.append(lst[2:])
rep = [0]*n
rep[n-1] = 1
res = deque([])
for i in nums[n-1]:
rep[i] = 1
res.append(i)
while res:
p = res.pop()
for i in nums[p]:
if rep[i]:continue
rep[i] = 1
res.append(i)
ans = 0
for i in range(n):
if rep[i]:
ans += v[i]
print(ans)
H:k为1的时候显然是inf的,其他情况枚举两个点然后找跟这两个点在同一条直线上的点,注意去重!
from collections import Counter
def func():
n, k = map(int, input().split())
a = []
for _ in range(n):
a.append(list(map(int, input().split())))
if k == 1:
return 'Infinity'
ans = 0
rep = [set() for _ in range(n)]
for i in range(n):
for j in range(i+1,n):
if j in rep[i]:continue
m = (a[i][0]-a[j][0],a[i][1]-a[j][1])
aaa = []
for x in range(j+1,n):
mm = (a[i][0]-a[x][0],a[i][1]-a[x][1])
if m[0]*mm[1] == m[1]*mm[0]:
aaa.append(x)
if len(aaa) >= k:
ans += 1
has = set(aaa)
for xx in aaa:
rep[xx].update(has)
return ans
print(func())
I:01背包
n,xx = map(int,input().split())
a = []
for _ in range(n):
a.append(list(map(int,input().split())))
pre = {0}
for i,j in a:
lin = set()
for x in pre:
if x+i <= xx:
lin.add(x+i)
if x+j <= xx:
lin.add(x+j)
pre = lin
if xx in pre:print('Yes')
else:print('No')
J:从前往后枚举,直到不能再递增为止
n = int(input())
h = list(map(int, input().split()))
pre = h[0]
for i in range(1, n):
if h[i] <= pre:
return pre
pre = h[i]
print(pre)
K:dp,中间枚举到的值必须都是k的因子,加上这个剪枝之后就很快了
from collections import Counter
n,xx = map(int,input().split())
dp = {1:1}
for _ in range(n):
lst = list(map(int,input().split()))
cnt = Counter(lst[1:])
lin = Counter()
for i,j in cnt.items():
for x,y in dp.items():
if xx%(x*i) == 0:
lin[x*i] += j*y
dp = lin
print(dp[xx])
L:从前往后枚举,减去字典里重复的就行
n = int(input())
a = list(map(int,input().split()))
a.sort()
from collections import Counter
cnt = Counter()
ans = 0
for i in range(n):
ans += i-cnt[a[i]]
cnt[a[i]] += 1
print(ans)