状压dp+二分

Problem Description

Finally, Little Q gets his weapon at level 10^5105 in the RPG game, now he is trying to beat the boss as soon as possible. The boss has HH units of health point (HP), Little Q needs to cause at least HH units of damage to beat the boss.

Little Q has learnt nn skills, labeled by 1,2,\dots,n1,2,…,n. Each skill can not be used multiple times, because there is not enough time for Little Q to wait for the skill to cool down. Assume Little Q uses the ii-th skill at the xx-th frame, the actor controlled by him will take t_iti​ frames to perform, which means Little Q will not be allowed to use other skills until the (x+t_i)(x+ti​)-th frame. The length of the damage sequence of the ii-th skill is len_ileni​, which means the skill will cause d_{i,j}di,j​ (0\leq j < len_i0≤j<leni​) units of damage at the (x+j)(x+j)-th frame if Little Q uses the ii-th skill at the xx-th frame. Note that len_ileni​ can be greater than t_iti​, for example, the burning skill can burn the boss for a long period, but takes a little time to cast the fire.

The game starts at the 00-th frame. Your task is to help Little Q beat the boss as soon as possible, or determine Little Q can't beat the boss using all the skills at most once.

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long ll;
const int N=19,M=1e5+5;
ll f[1<<N];
ll d[N][M];
int t[N],len[N];
int p[1<<N];
int n;ll h;
bool check(int x)
{
	for(int i=0;i<(1<<n);i++)
	{
		int sum=0;
		f[i]=0;
		for(int j=0;j<n;j++)
		{	
			if((i>>j)&1) 
			{
				int now=p[i]-t[j+1];
				if(x-now+1>=1&&x-now+1<=len[j+1])
				f[i]=max(f[i],f[i^(1<<j)]+d[j+1][x-now+1]);
				else if(x-now+1>len[j+1])f[i]=max(f[i],f[i^(1<<j)]+d[j+1][len[j+1]]);
				else f[i]=max(f[i],f[i^(1<<j)]);
			}
		}
	}
	return (f[(1<<n)-1]>=h);
}
void solve()
{
	scanf("%d%lld",&n,&h);
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d",&t[i],&len[i]);
		for(int j=1;j<=len[i];j++) 
		{
			scanf("%lld",&d[i][j]);
			d[i][j]+=d[i][j-1];
		}
	}
	for(int i=0;i<(1<<n);i++)
	{
		p[i]=0;
		for(int j=0;j<n;j++)
		{
			if((i>>j)&1) p[i]+=t[j+1];
		}
	}
	int l=0,r=2e6;
	int ans=-1;
	while(l<=r)
	{
		int m=(l+r)/2;
		if(check(m))
		{
			ans=m;
			r=m-1;
		}
		else l=m+1;
	}
	printf("%d\n",ans);
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		solve();
	}

<think>嗯,用户想了解不同类型的动态规划问题及其Python实现,还要详细的解释和Markdown格式的输出。首先,我需要整理动态规划的常见类型,比如背包DP、树形DPDP、数位DP,以及优化方法。用户提到的引用中有LIS的例子,可以作为基础DP的示例,不过用户的问题更偏向于其他类型,所以可能需要补充其他例子。 接下来,每个DP类型需要给出定义、适用场景、Python实现示例,以及可能的优化点。比如背包DP中的0-1背包问题,态转移方程是关键,代码要简洁明了。树形DP可能需要递归或后序遍历,比如节点选择问题。DP涉及位运算,数位DP则处理数字各位上的问题,比如统计不含4的数字数量。 还要考虑用户提到的优化方法,如滚动数组、记忆化搜索、缩等。每个优化方法需要简单说明原理,并举例说明如何应用。 注意用户要求使用中文回答,行内公式用$...$,独立公式用$$...$$,并且正确使用LaTeX语法。比如态转移方程可能需要用独立公式,如$dp[i][j] = \max(dp[i-1][j], dp[i-1][j-w] + v)$。同时,用户希望生成相关问题,这部分需要根据内容提出相关的问题,比如各种DP的应用场景、优化技巧等。 需要检查引用部分,用户提供的引用中有LCS的问题,但当前主题是动态规划的不同类型,可能需要调整引用标注的位置。例如,在提到LIS问题时,可以引用用户提供的引用[1],其他引用可能不太相关,但根据用户提供的引用内容,引用[2]和引用[3]可能涉及Python实现和算法专栏,可以适当提及。 最后,确保回答结构清晰,分点说明,代码块用正确的语法高亮,问题部分使用Markdown的列表格式。同时,用户需要Markdown格式的输出,所以整个回答要符合规范,避免格式错误。</think># 动态规划专题及Python实现 ## 一、基础动态规划分类与实现 ### 1. 背包DP #### 0-1背包问题 **定义**:给定$n$个物品(重量$w_i$,价值$v_i$)和容量$C$的背包,求最大价值 **态转移方程**: $$dp[i][j] = \max(dp[i-1][j], dp[i-1][j-w_i] + v_i)$$ ```python def knapsack_01(weights, values, capacity): n = len(weights) dp = [0]*(capacity+1) for i in range(n): for j in range(capacity, weights[i]-1, -1): dp[j] = max(dp[j], dp[j-weights[i]] + values[i]) return dp[capacity] ``` ### 2. 树形DP #### 二叉树节点选择问题 **场景**:选择节点时相邻节点不可同时选中[^3] **态定义**: - $dp[u][0]$:不选节点u的最大值 - $dp[u][1]$:选择节点u的最大值 ```python class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def tree_dp(root): def dfs(node): if not node: return (0, 0) left = dfs(node.left) right = dfs(node.right) selected = node.val + left[0] + right[0] not_selected = max(left) + max(right) return (not_selected, selected) return max(dfs(root)) ``` ### 3. DP #### 旅行商问题(TSP) **定义**:访问所有城市且总路程最短 **态表示**:$dp[mask][u]$表示经过城市集合mask,最后到达城市u的最短路径 ```python def tsp(graph): n = len(graph) dp = [[float('inf')]*n for _ in range(1<<n)] dp[1][0] = 0 # 起点为0号城市 for mask in range(1<<n): for u in range(n): if mask & (1<<u): for v in range(n): if not mask & (1<<v): new_mask = mask | (1<<v) dp[new_mask][v] = min(dp[new_mask][v], dp[mask][u] + graph[u][v]) return min(dp[-1][u] + graph[u][0] for u in range(n)) ``` ### 4. 数位DP #### 数字限制统计 **问题**:统计区间$[L,R]$内不包含数字4的数的个数 **态定义**:$dp[pos][tight]$表示处理到第pos位,前驱是否紧贴上界 ```python def count_special_numbers(n_str): from functools import lru_cache @lru_cache(maxsize=None) def dfs(pos, tight, has_four): if pos == len(n_str): return 0 if has_four else 1 limit = int(n_str[pos]) if tight else 9 total = 0 for d in range(0, limit+1): new_tight = tight and (d == limit) new_has_four = has_four or (d == 4) if new_has_four: continue total += dfs(pos+1, new_tight, new_has_four) return total return dfs(0, True, False) ``` ## 二、动态规划优化技巧 ### 1. 滚动数组优化 将二维数组缩为两个一维数组,空间复杂度从$O(n^2)$降为$O(n)$ ```python # 0-1背包滚动数组实现 dp = [0]*(capacity+1) # 只需单层数组 ``` ### 2. 记忆化搜索 使用@lru_cache装饰器实现递归记忆化(示例见数位DP代码) ### 3. 缩 通过位运算减少态维度(示例见DP的mask处理) ### 3. 四边形不等式 优化区间类DP的时间复杂度,例如最优二叉搜索树问题 ## 三、应用场景对比 | 类型 | 典型问题 | 时间复杂度 | 空间复杂度 | |----------|---------------------------|--------------|--------------| | 背包DP | 物品选择问题 | $O(nC)$ | $O(C)$ | | 树形DP | 树结构决策问题 | $O(n)$ | $O(n)$ | | DP | 态组合优化问题 | $O(n2^n)$ | $O(2^n)$ | | 数位DP | 数字特性统计问题 | $O(logR)$ | $O(logR)$ |
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值