CF - 1106 E Lunar New Year and Red Envelopes DP

题目传送门

题解:

首先要处理出每个时间点会选择哪一个线段。

对于这个问题,可以用multiset去维护信息。

当时间线开始的时候,往mutiset里面插入这个信息,当时间线结束的时候,删除这个信息。

每次只要取出最大位就好了。

然后,就是状态转移,注意的就是只有转移进来过的状态才能转移出去。

代码:

/*
code by: zstu wxk
time: 2019/02/03
*/
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod =  (int)1e9+7;
const int N = 1e5 + 100;
int n, m, k;
struct Node{
    int d, w;
    bool operator < (const Node & x) const{
        if(w == x.w) return d > x.d;
        return w > x.w;
    }
};
vector<Node> in[N], out[N];
LL dp[N][210];
multiset<Node> st;
void Ac(){
    for(int i = 1; i <= k; ++i){
        int s, t, d, w;
        scanf("%d%d%d%d", &s, &t, &d, &w);
        in[s].pb({d,w});
        out[t].pb({d,w});
    }
    memset(dp, INF, sizeof dp);
    dp[1][0] = 0;
    for(int i = 1; i <= n; ++i){
        for(Node & x : in[i]){
            st.insert(x);
        for(int j = 0; j <= m; ++j){
            if(dp[i][j] == INF) continue;
            dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j]);
            if(st.empty())
                dp[i+1][j] = min(dp[i+1][j], dp[i][j]);
            else {
                Node x = *st.begin();
                dp[x.d+1][j] = min(dp[x.d+1][j], dp[i][j]+x.w);
            }
        }
        for(Node &x : out[i])
            st.erase(st.lower_bound(x));
    }
    LL ans = INF;
    for(int j = 0; j <= m; ++j) ans = min(ans, dp[n+1][j]);
    printf("%I64d\n", ans);
}
int main(){
    while(~scanf("%d%d%d", &n, &m, &k)){
        Ac();
    }
    return 0;
}

原文地址:https://www.cnblogs.com/MingSD/p/10350604.html

时间: 2024-08-01 17:03:52

CF - 1106 E Lunar New Year and Red Envelopes DP的相关文章

Codeforces Round #536 E. Lunar New Year and Red Envelopes /// 贪心 记忆化搜索 multiset取最大项

题目大意: 给定n m k:(1≤n≤1e5, 0≤m≤200, 1≤k≤1e5) 表示n个时间长度内 最多被打扰m次 k个红包 接下来k行描述红包 s t d w:(1≤s≤t≤d≤n , 1≤w≤1e9) 表示在 s 到 t 的时间内都可开始获得该红包 该红包在时间 d 时才能完成获得 红包内有w硬币 在同一时间段内只能获得一个红包 不能同时获得两个及以上 求在被打扰的情况下使获得的红包硬币最少 是多少 用in out标记各红包可被获得的区间 用multiset维护在某个时间点可获得的红包有

【Codeforces 1106E】Lunar New Year and Red Envelopes

[链接] 我是链接,点我呀:) [题意] 给你k个红包,每个红包可以在si..ti的时间范围内拿走. 抢完红包之后你得到wi元,然后你需要在di+1时刻才能继续抢红包 时间是线性的从1..n 然后某个人可以阻止你在x时刻抢红包,然后你的时间跳过1s(-1s)直接到达x+1时刻. 这个人可以阻止你m次. 请问这个人采用最优阻止策略下,你最少抢到的金额. (如果有多个可以抢的红包,那么抢wi最大的,如果仍然相同抢di最大的,再相同的话就无所谓了,因为选哪个都一样了) [题解] dp 设dp[i][j

Lunar New Year and Red Envelopes CodeForces - 1106E (dp)

大意: 总共$n$的时间, $k$个红包, 红包$i$只能在时间$[s_i,t_i]$范围内拿, 并且拿完后时间跳到$d_i+1$,Bob采用贪心策略,每个时间点若有红包能取则取钱数$w_i$最大的, $w_i$相同则取$d_i$最大的, Alice有$m$次机会让Bob跳过一个时间, 求Alice如何操作能使Bob得到钱数最少. 比较简单的DP, 记$dp[i][j]$为$i$时刻还能干扰$j$次的最小收益 $dp[i][j]=min(dp[d_i][j]+w_i,dp[i+1][j-1])$

CF 713C Sonya and Problem Wihtout a Legend(DP)

原版题意:给定一个序列,每次操作给其中一个数$+1$或$-1$,问最少需要多少操作使得整个序列为不下降序列. 题解:不下降序列最后修改完成后的各个数一定是原序列中的某一个数.关于这个的理解看到网上的一个解释:原序列,从左到右扫过去,如果左边的大于右边的,要么左边的减掉使其等于右边的,要么右边的加上使其等于左边的. $dp[i][j]$:前i个数以原序列排序后的第j个数为结尾需要的最少操作. 状态转移方程:$dp[i][j]=min(dp[i][j-1],dp[i-1][j]+abs(a[i]-b

Educational CF # 17 C 二分,字符串 D 最长路,dp

Educational Codeforces Round 17 C. Two strings 题意:两个字符串A,B,从B中删除尽可能少的子串,要使得B剩下的字符串是A的子序列,输出B剩下的字符串.(注意子串与子序列区别) 总结:看了某神犇的代码,不太理解..官方题解:不要去想从B中删掉子串,应该想,从B的左端取出一段子串,再从右端取出一段子串. // Educational Codeforces Round 17 C #include<bits/stdc++.h> using namespa

CF#301 D:Bad Luck Island (概率dp)

D:Bad Luck Island 一个岛上有r个石头,s个剪子,p个布,他们之间随机挑出两个相遇,如果不是相同物种,就会有一个消失,分别求出最后这座岛上只剩下一个物种的概率. 我们用dp[i][j][k]来存储剩下 i 个石头, j 个剪刀,k 个布时的概率,共dp三次: 如果石头与剪刀碰面,概率是 p1 = i*j / (i*j+j*k+k*i),这种情况下,剪刀会被石头吃掉,所以石头的数目减少1,表现出来是dp[i-1][j][k] = p1*dp[i][j][k]  (dp的3的返回值均

CF 908 D New Year and Arbitrary Arrangement —— 期望DP

题目:http://codeforces.com/contest/908/problem/D 首先,设 f[i][j] 表示有 i 个 a,j 个 ab 组合的期望,A = pa / (pa + pb) , B = pb / (pa + pb) 那么 f[i][j] = A * f[i+1][j] + B * f[i][i+j] 当 i+j >= k 时,再出现一个 b 就会结束,所以此时: f[i][j] = f[i][i+j] * B + f[i+1][i+j+1] * A * B + f[

CF F. Shovels Shop(前缀和预处理+贪心+dp)

F. Shovels Shop time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output There are nn shovels in the nearby shop. The ii -th shovel costs aiai bourles. Misha has to buy exactly kk shovels. Each sho

codeforces#536题解

CodeForces#536 A. Lunar New Year and Cross Counting Description: Lunar New Year is approaching, and you bought a matrix with lots of "crosses". This matrix \(M\) of size \(n \times n\) contains only 'X' and '.' (without quotes). The element in t