牛客网算法题

小Q的歌单

转化为背包问题,利用动态规划解决
物品序列: i t e m s = [ A , A , , B , , B ] X + Y 个,
target: K
f [ i ] [ k ] i 个物品能组成 k 的方案数

f [ i ] [ k ] = f [ i 1 ] [ k ] + f [ i 1 ] [ k i t e m s [ i 1 ] | i t e m s [ i 1 ] <= k f [ 0 ] [ 0 ] = 1 , f [ 0 ] [ 1 ] = 0 ,

时间复杂度为 O ( ( X + Y + 1 ) K )

# -*- coding:utf-8 -*-
mod = 1000000007
K = int(input())
A, X, B, Y = list(map(int, input().split()))


f=[0]*(K+1)
f[0]=1
for i in range(1,X+Y+1):
    for k in range(K,-1,-1):
        if i<=X:
            length=A
        else:
            length=B

        if k>=length:
            f[k]=f[k-length]+f[k]

print (f[-1]%1000000007)

2、计算组合数
C [ i ] [ j ] = C [ i 1 ] [ j 1 ] + C [ i 1 ] [ j ]
C [ 0 ] [ 0 ] = 1

mod = 1000000007
K=int(input())
A,X,B,Y=list(map(int,input().split()))

comb=[[0]*101 for i in range(101)]
comb[0][0]=1
for i in range(1,101):
    for j in range(i+1):
        comb[i][j]=comb[i-1][j]
        if j>=1:
            comb[i][j]=(comb[i-1][j-1]+comb[i][j])%mod

ans=0
for i in range(X+1):
    if i*A<=K and (K-i*A)%B==0 and (K-i*A)//B<=Y:
        j=(K-i*A)//B
        ans=(ans+(comb[X][i]*comb[Y][j])%mod)%mod
print (ans)

安排机器

N,M=list(map(int,input().strip().split()))
machines=[]
for i in range(N):
    machines.append(list(map(int,input().strip().split())))

tasks=[]
for i in range(M):
    tasks.append(list(map(int,input().strip().split())))

#因为时间的收益大,先按时间降序排序,再按难度降序排序
tasks.sort(key=lambda x:(x[0],x[1]),reverse=True)
machines.sort(key=lambda x:(x[0],x[1]),reverse=True)


count=0
value=0
f=[0]*101
j=0
for taskTime,taskLevel in tasks:#对于任务列表中的每一个任务
    #找到在时间上满足要求的机器(也一定能在时间上满足后续任务)
    while j<N and machines[j][0]>=taskTime:
        level=machines[j][1]
        f[level]=f[level]+1
        j=j+1

    for lev in range(taskLevel,101):# 找到在难度上满足条件并且最接近的机器
        if f[lev]>=1:
            f[lev]=f[lev]-1
            count=count+1
            value=200*taskTime+3*taskLevel
            break
print ("{} {}".format(count,value))
#button {
    border: none;
}

参考

https://www.cnblogs.com/yanmk/p/9313580.html

猜你喜欢

转载自blog.csdn.net/XindiOntheWay/article/details/81638266