codeforces1106E_Lunar New Year and Red Envelopes

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.

Examples
Input

5 0 2
1 3 4 5
2 5 5 8

Output

13

Input

10 1 6
1 1 2 4
2 2 6 2
3 3 3 3
4 4 4 5
5 5 5 7
6 6 6 9

Output

2

Input

12 2 6
1 5 5 4
4 6 6 2
3 8 8 3
2 9 9 5
6 10 10 7
8 12 12 9

Output

11

bob现在过年想要抢红包,每个红包有自己的出现时间 [s,t] ,和它自己的金额 w 。但是如果bob抢了这个红包,那么他在时间点 d 前都不能在抢别的红包。bob抢红包的原则是 有红包就抢,如果在同一时间点上有多个红包,那么他会去抢那个金额最大的,如果还有多个红包金额最大,那么他会去抢 d 最大的(关于 d 的说明见上)。
但是他可爱的女儿Alice觉得这样不好,就不想要他抢这么多红包,所以Alice有 m 次机会干扰bob抢红包。如果Alice在时间点 i 干扰bob抢红包,那么bob在时间点 i 将无法抢红包,只能从时间点 i+1 开始抢红包。
现求bob能最少能抢到多少钱

由题意可知,对于每个时间点,Alice都有两个选择,如果即数组dp[i][j]表示在时间点 j 上干扰 i 次最少能得到的金额,数组select[i]为在时间点 i 上,bob将选择的红包编号

  1. Alice在这个时间点干扰bob
    dp[i+1][j+1]=min(dp[i+1][j+1],dp[i][j])
  2. Alice在这个时间不干扰bob
    dp[i][select[j].d+1]=min(dp[i-1][select[j].d+1],dp[i+1][j]+select[j].w)

因此,我们可以用递推的方法去求解。

那么,现在就需要去求bob在时间点 i 上选择的红包编号。
可以建立一个集合,记录在当前时间点上所有可以选择的红包,并按照题意对集合中元素进行从大到小排序,这样集合中第一个元素即为bob在时间点 i 上将要选择的红包。集合在进入下一个时间点时,注意根据红包出现与消失的时间,向集合中添加或删除元素。

此外,观察递推式可知,对于干扰了 n 次的情况,只有干扰了 n-1 次 和 n 次才会对其产生影响,即

计算dp[n][]不会调用除dp[n][] 与 dp[n-1][]以外的数据

所以根据这一点,可以进行内存上的优化,只记录当前干扰次数下的情况与上一次干扰次数下的情况,并注意即时清除无关数据。


#include <stdio.h>
#include <climits>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f3f3f3f3fLL
#define ll long long
#define re return
#define Pair pair<int,int>
#define Make(a,b) make_pair(a,b)
#define Push(num) push_back(num)
#define rep(index,star,finish) for(register int index=star;index<finish;index++)
#define drep(index,finish,star) for(register int index=finish;index>=star;index--)
using namespace std;
struct Enve{
    int d,w;
    bool status;

    bool operator < (const Enve& a)const{
        if(w==a.w)
            re d>a.d;
        re w>a.w;
    }
};
class enveSet{
private:
    map<Enve,int> mapping;
public:
    Enve top(int now){
        if(mapping.size()==0)
            re (Enve){now,0,false};
        else
            re (*mapping.begin()).first;
    }
    void insert(const Enve& a){
        if(mapping.find(a)!=mapping.end())
            mapping[a]++;
        else
            mapping[a]=1;
    }
    void erase(const Enve& a){
        mapping[a]--;
        if(mapping[a]==0)
            mapping.erase(a);
    }
};

int n,m,k;
vector<Enve> store[100024];
Enve select[100024];    //chose envelop select[i] at time point i
ll dp[2][100024];
int main(){
    ios::sync_with_stdio(false);

    cin>>n>>m>>k;
    rep(i,0,k){
        int s,t,d,w;
        cin>>s>>t>>d>>w;
        store[s].push_back((Enve){d,w,true});
        store[t+1].push_back((Enve){d,w,false});
    }

    enveSet enveset;
    rep(time,1,n+1){
        rep(i,0,store[time].size()){
            if(store[time][i].status){
                enveset.insert(store[time][i]);
            }else
                enveset.erase(store[time][i]);
        }

        select[time]=enveset.top(time);
    }

    ll ans=INF;
    memset(dp,INF,sizeof(dp));
    dp[0][1]=0;
    rep(i,0,m+1){
        memset(dp[(i^1)&1],INF,sizeof(dp[(i^1)&1]));
        rep(j,1,n+1){
            //decide to disturb in time point j
            dp[(i^1)&1][j+1]=min(dp[(i^1)&1][j+1],dp[i&1][j]);
            //not decide to disturb in time point j
            dp[i&1][select[j].d+1]=min(dp[i&1][select[j].d+1],dp[i&1][j]+select[j].w);
        }

        ans=min(ans,dp[i&1][n+1]);
    }

    cout<<ans<<endl;
    re 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值