【动态规划】【预处理】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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值