头像

生日快乐




在线 


活动打卡代码 AcWing 1245. 特别数的和

生日快乐
9小时前
n = int(input())

cnt = 0
for i in range(1, n + 1):
    tmp = str(i)
    for j in tmp:
        if j == '2' or j == '0' or j == '1' or j == '9':
            cnt += i
            break
print(cnt)


活动打卡代码 AcWing 1236. 递增三元组

生日快乐
9小时前

空间换时间

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

#a.sort()
b.sort()
c.sort()

def find(x, l, r, arr):
    while l < r:
        mid = l + r >> 1
        if arr[mid] > x: r = mid
        else: l = mid + 1
    return l

ans = 0
cnt = [0] * n
s = [0] * (n + 1)
for i in range(n):
    pos = find(b[i], 0, n, c)
    cnt[i] = n - pos
for i in range(n - 1 , -1, -1):
    s[i] = s[i + 1] + cnt[i]

for i in range(n):
    tmp = find(a[i], 0, n, b)
    ans += s[tmp]
print(ans)




生日快乐
9小时前

空间换时间

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

#a.sort()
b.sort()
c.sort()

def find(x, l, r, arr):
    while l < r:
        mid = l + r >> 1
        if arr[mid] > x: r = mid
        else: l = mid + 1
    return l

ans = 0
cnt = [0] * n
s = [0] * (n + 1)
for i in range(n):
    pos = find(b[i], 0, n, c)
    cnt[i] = n - pos
for i in range(n - 1 , -1, -1):
    s[i] = s[i + 1] + cnt[i]

for i in range(n):
    tmp = find(a[i], 0, n, b)
    ans += s[tmp]
print(ans)



生日快乐
9小时前

好的兄弟们,我还寻思 1 3 3这样的数据怎么处理

注意审题哈,是1 ~ N的排列

n = int(input())
num = list(map(int, input().split()))
cnt = 0
for i in range(n):
    mx = num[i]
    mi = num[i]
    for j in range(i + 1, n):
        mx = max(num[j], mx)
        mi = min(num[j], mi)
        if j - i + 1 == mx - mi + 1: cnt += 1
print(cnt + n)


活动打卡代码 AcWing 1210. 连号区间数

生日快乐
9小时前
n = int(input())
num = list(map(int, input().split()))
cnt = 0
for i in range(n):
    mx = num[i]
    mi = num[i]
    for j in range(i + 1, n):
        mx = max(num[j], mx)
        mi = min(num[j], mi)
        if j - i + 1 == mx - mi + 1: cnt += 1
print(cnt + n)


活动打卡代码 AcWing 1214. 波动数列

生日快乐
10小时前

N, S, A, B = map(int, input().split())

dp = [[0] * (N + 1) for i in range(N + 1)]# dp[i][j] 表示第i项选择加a或者减b余数为j的方案总数

dp[0][0] = 1

for i in range(1, N):
    for j in range(N):
        dp[i][j] = dp[i - 1][((j - A * i) % N + N) % N] + dp[i - 1][(j + B * i) % N]

print(dp[N - 1][S % N] % 100000007)


活动打卡代码 AcWing 1212. 地宫取宝

注意有价值为 0 的 宝藏

n, m, K = map(int, input().split())
mod = 1000000007
M = [[0] * m for i in range(n)]
dp = [[[[0 for i in range(15)]for j in range(15)]for k in range(m + 1)]for q in  range(n + 1)]
for i in range(n):
    M[i] = [int(i) + 1 for i in input().split()]

dp[1][1][1][M[0][0]] = 1
dp[1][1][0][0] = 1
for i in range(1, n + 1):
    for j in range(1, m + 1):
        for k in range(K + 1):
            for c in range(14):
                dp[i][j][k][c] += (dp[i - 1][j][k][c] + dp[i][j - 1][k][c]) % mod
                dp[i][j][k][c] %= mod
            if k >= 1:
                for c in range(M[i - 1][j - 1]):
                    dp[i][j][k][M[i - 1][j - 1]] += (dp[i - 1][j][k - 1][c] + dp[i][j - 1][k - 1][c]) % mod
                    dp[i][j][k][M[i - 1][j - 1]] %= mod
print(sum(dp[n][m][K]) % mod)



dp[i]表示以num[i]为结尾的最长上升子序列

n = int(input())
num = list(map(int, input().split()))

dp = [1] * n

for i in range(n):
    for j in range(i):
        if num[i] > num[j]:
            dp[i] = max(dp[i], dp[j] + 1)
print(max(dp))


活动打卡代码 AcWing 1015. 摘花生

T = int(input())

while T:
    T -= 1
    R, C = map(int, input().split())
    M = [[0] * C for i in range(R)]
    dp = [[0] * (C + 1) for i in range(R + 1)]
    for i in range(R):
        M[i] = list(map(int, input().split()))
    dp[1][1] = M[0][0]
    for i in range(1, R + 1):
        for j in range(1, C + 1):
            dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + M[i - 1][j - 1]
    print(dp[R][C])




活动打卡代码 AcWing 2. 01背包问题

n, V = map(int, input().split())

f = [-0xfffff] * (V + 1)
v = [0] * n
w = [0] * n

f[0] = 0 
for i in range(n):
    v[i], w[i] = map(int, input().split())

for i in range(n):
    for j in range(V, v[i] - 1, -1):
        f[j] = max(f[j], f[j - v[i]] + w[i])

ans = 0
for i in range(V + 1):
    ans = max(ans, f[i])
print(ans)