【动态规划】【预处理】codeforces1106E Lunar New Year and Red Envelopes

E. Lunar New Year and Red Envelopes
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Lunar New Year is approaching, and Bob is going to receive some red envelopes with countless money! But collecting money from red envelopes is a time-consuming process itself.

Let’s describe this problem in a mathematical way. Consider a timeline from time 1 to n. The i-th red envelope will be available from time si to ti, inclusive, and contain wi coins. If Bob chooses to collect the coins in the i-th red envelope, he can do it only in an integer point of time between si and ti, inclusive, and he can’t collect any more envelopes until time di (inclusive) after that. Here si≤ti≤di holds.

Bob is a greedy man, he collects coins greedily — whenever he can collect coins at some integer time x, he collects the available red envelope with the maximum number of coins. If there are multiple envelopes with the same maximum number of coins, Bob would choose the one whose parameter d is the largest. If there are still multiple choices, Bob will choose one from them randomly.

However, Alice — his daughter — doesn’t want her father to get too many coins. She could disturb Bob at no more than m integer time moments. If Alice decides to disturb Bob at time x, he could not do anything at time x and resumes his usual strategy at the time x+1 (inclusive), which may lead to missing some red envelopes.

Calculate the minimum number of coins Bob would get if Alice disturbs him optimally.

Input
The first line contains three non-negative integers n, m and k (1≤n≤105, 0≤m≤200, 1≤k≤105), denoting the length of the timeline, the number of times Alice can disturb Bob and the total number of red envelopes, respectively.

The following k lines describe those k red envelopes. The i-th line contains four positive integers si, ti, di and wi (1≤si≤ti≤di≤n, 1≤wi≤109) — the time segment when the i-th envelope is available, the time moment Bob can continue collecting after collecting the i-th envelope, and the number of coins in this envelope, respectively.

Output
Output one integer — the minimum number of coins Bob would get if Alice disturbs him optimally.


 英语不好真的真的是原罪,今天看了题解才发现昨天理解错题意了,he can’t collect any more envelopes until time di,一开始当成了到di之后拿不了红包,没想到是拿完之后到di之间不能拿,之后可以拿。真是被chrome的自动翻译惯坏了。
 难怪昨天怎么dp都有后效性。正确理解题意之后就简单一些了。开100005*205的数组来dp,此前需要预处理一下Bob每i天的贪心选择是什么 val[i],以及选择之后转移到第nxt[i]天才能继续抢红包,这个按照定义就行了。 d p [ i ] [ j ] dp[i][j] dp[i][j]表示前i-1天Alice干扰j次产生的最小值(第i天要保证Bob是可以拿红包的),转移方程看代码
 这个dp感觉还是很有意思的。

#include<cstdio>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
using LL=long long;

struct item
{
	int w,d,pos;
	inline operator < (const item &t) const
	{
		return w>t.w||w==t.w&&d>t.d;
	}
}A[100005];

LL dp[100005][205];
multiset<item> S;
vector<int> in[100005],out[100005];
int n,m,k,s,t,d,w,nxt[100005],val[100005];

int main()
{
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=k;i++)
	{
		scanf("%d%d%d%d",&s,&t,&d,&w);
		in[s].push_back(i),out[t+1].push_back(i);
		A[i]=(item){w,d,i};
	}
	for(int i=1;i<=n;i++)
	{
		for(int &x:in[i])
			S.insert(A[x]);
		for(int &x:out[i])
			S.erase(S.lower_bound(A[x]));  //注意S.erase(A[x])会删除所有的A[x],删除迭代器的话只会删除一个
		if(!S.empty())
		{
			nxt[i]=S.begin()->d+1;  //拿红包之后跳转的天数
			val[i]=S.begin()->w;  //红包价值
		}
		else
			nxt[i]=i+1;
	}
	for(int i=2;i<=n+1;i++)
		for(int j=0;j<=m;j++)
			dp[i][j]=1E18;
	for(int i=1;i<=n;i++)
		for(int j=0;j<=m;j++)
		{
			dp[nxt[i]][j]=min(dp[nxt[i]][j],dp[i][j]+val[i]);  //不阻止
			if(j)
				dp[i+1][j-1]=min(dp[i+1][j-1],dp[i][j]);  //阻止
		}
	printf("%lld",*min_element(dp[n+1],dp[n+1]+m+1));
	return 0;
}
### 关于Codeforces平台上的动态规划问题 在Codeforces这样的编程竞赛平台上,动态规划(Dynamic Programming, DP)是一类非常重要的算法技术。这类题目通常涉及优化子结构和重叠子问题两个特性。 #### 动态规划示例解析 考虑一个典型的DP问题,在给定条件下求解最优方案的数量或具体路径等问题。例如,在某些情况下,可能需要计算达到特定状态所需的最少步数或是最大收益等[^1]。 对于具体的例子而言,假设有一个序列`a[]`,目标是从左到右遍历此序列并决定是否选取当前元素加入集合中,最终目的是让所选元素之和尽可能大而不超过某个上限值M。这个问题可以通过定义二维数组dp[i][j]表示从前i个物品里挑选若干件放入容量为j的背包可以获得的最大价值来建模: - 如果不取第i项,则`dp[i][j]=dp[i−1][j]`; - 若选择第i项且其重量w不超过剩余空间j,则更新为`max(dp[i−1][j], dp[i−1][j-w]+v)`其中v代表该项的价值; 最后的结果保存在`dp[n][m]`处(n为总项目数量,m为目标体积)[^2]。 ```cpp #include <iostream> using namespace std; const int N = 1e3 + 5; int w[N]; // weights of items int v[N]; // values of items long long f[N][N]; void knapsack(int n, int m){ for (int i = 1; i <= n; ++i) { for (int j = 0; j <= m; ++j) { f[i][j] = f[i - 1][j]; if(j >= w[i]) f[i][j] = max(f[i][j],f[i - 1][j - w[i]] + v[i]); } } } ``` 上述代码展示了如何利用记忆化搜索的方式实现简单的0/1背包问题解决方案,这同样适用于其他形式更复杂的动态规划挑战。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值