【模板】最长上升子序列 (nowcoder.com)
import bisect,sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
f = []
for t in a:
pos = bisect.bisect_left(f, t)
if pos == len(f):
f.append(t)
else:
f[pos] = t
print(len(f))
【模板】01背包 (nowcoder.com)
n,V = map(int,input().split())
a = [ list(map(int,input().split())) for _ in range(n)] # v, w
f = [0]*(V+1)
for t in a:
for j in range(V,t[0]-1,-1):
f[j] = max(f[j],f[j - t[0]] + t[1])
print(f[V])
f = [-float('inf')]*(V+1)
f[0] = 0
for t in a:
for j in range(V,t[0]-1,-1):
if f[j - t[0]] != -float('inf'):
f[j] = max(f[j],f[j - t[0]] + t[1])
print(f[V] if f[V] != -float('inf') else 0)
【模板】完全背包 (nowcoder.com)
n,V = map(int,input().split())
a = [ list(map(int,input().split())) for _ in range(n)] # v, w
f = [0]*(V+1)
for t in a:
for i in range(t[0], V+1):
f[i] = max(f[i], f[i-t[0]]+t[1])
print(f[V])
f = [-float('inf')]*(V+1)
f[0] = 0
for t in a:
for i in range(t[0], V+1):
if f[i - t[0]] != -float('inf'):
f[i] = max(f[i], f[i-t[0]]+t[1])
print(f[V] if f[V] != -float('inf') else 0)
【模板】差分 (nowcoder.com)
n,t = map(int, input().split())
a = [0] + list(map(int, input().split()))
b = [0] * (n+2)
for _ in range(t):
l,r,x = map(int, input().split())
b[l] += x
b[r+1] -= x
for i in range(1,n+1):
b[i] += b[i - 1]
a[i] += b[i]
print(*a[1:])
【模板】前缀和 (nowcoder.com)
n,t = map(int, input().split())
a = [0] + list(map(int, input().split()))
for i in range(n + 1):
a[i] += a[i - 1]
for _ in range(t):
l,r = map(int, input().split())
print(a[r] - a[l - 1])
【模板】二维前缀和 (nowcoder.com)
n,m,t = map(int, input().split())
a = [ [0] * (m + 1)]
a += [ [0] + list(map(int, input().split())) for _ in range(n)]
sum = [ [0] * (m + 1) for _ in range(n + 1)]
for i in range(1, n + 1):
for j in range(1, m + 1):
sum[i][j] = a[i][j] + sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1]
for _ in range(t):
x1, y1, x2, y2 = map(int, input().split())
print(sum[x2][y2] - sum[x1 - 1][y2] - sum[x2][y1 - 1] + sum[x1 - 1][y1 - 1])
【模板】快速幂 (nowcoder.com)
python中的pow()
底层是用快速幂实现的。
t = int(input())
for i in range(t):
a,b,p = map(int, input().split())
print(pow(a,b,p))