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维护在某个时间点可获得的红包有哪些 (放入in内的 去掉out内的)
multiset内部按w排序 那么在某个时间点取出w最大的就是获得红包的贪心策略 .rbegin()可获得multiset/set内的最后一项
按时间点记忆化搜索一下 dp[ 时间点 ][ 被打扰次数 ]来记忆
以上存储信息要用pair<> 用结构体会MLE
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define mem(i,j) memset(i,j,sizeof(i))
#define pb push_back
#define mp(i,j) make_pair(i,j)
#define fir first
#define sec second
#define P pair<LL,LL>
const int N=1e5+;
P best[N]; // best[i] 在i位置的最佳策略
LL n, m, k;
LL dp[N][];
// 记忆化 dp[i][j]在i位置被打扰了j次能获得的最少硬币
multiset <P> now;
// 维护当前位置所有能取的红包 pair默认按first排序
vector <P> in[N], out[N];
// in为从该位置开始可取的 out为从该位置开始不可取的 void init() {
mem(dp,-); now.clear();
for(int i=;i<=n+;i++)
in[i].clear(), out[i].clear();
}
LL DFS(int ind,int c) {
if(ind>n) return 0LL;
if(dp[ind][c]>=0LL) return dp[ind][c];
LL res=best[ind].fir+DFS(best[ind].sec+,c); // 取这个红包
if(c<m) res=min(res,DFS(ind+,c+)); // 被女儿阻止
return dp[ind][c]=res;
} int main()
{
while(~scanf("%d%d%d",&n,&m,&k)) {
init();
for(int i=;i<k;i++) {
LL l,r,d,w;
scanf("%I64d%I64d%I64d%I64d",&l,&r,&d,&w);
in[l].pb(mp(w,d));
out[r+].pb(mp(w,d));
}
for(int i=;i<=n+;i++) {
for(int j=;j<in[i].size();j++)
now.insert(in[i][j]); // 加入可取的
for(int j=;j<out[i].size();j++)
now.erase(now.find(out[i][j])); // 去掉不可取的
if(now.size()) best[i]=*now.rbegin(); // 最佳策略是w最大的
else best[i]={0LL,(LL)i};
}
printf("%I64d\n",DFS(,));
} return ;
}
Codeforces Round #536 E. Lunar New Year and Red Envelopes /// 贪心 记忆化搜索 multiset取最大项的更多相关文章
- Codeforces Round #554 (Div. 2) D 贪心 + 记忆化搜索
https://codeforces.com/contest/1152/problem/D 题意 给你一个n代表合法括号序列的长度一半,一颗有所有合法括号序列构成的字典树上,选择最大的边集,边集的边没 ...
- Codeforces Round 536 (Div. 2) (E)
layout: post title: Codeforces Round 536 (Div. 2) author: "luowentaoaa" catalog: true tags ...
- Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索
D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...
- Educational Codeforces Round 1 E. Chocolate Bar 记忆化搜索
E. Chocolate Bar Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/prob ...
- Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索
A. Berzerk 题目连接: http://codeforces.com/contest/786/problem/A Description Rick and Morty are playing ...
- Codeforces Round #427 (Div. 2) Problem D Palindromic characteristics (Codeforces 835D) - 记忆化搜索
Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th nu ...
- Codeforces Round #459 (Div. 2):D. MADMAX(记忆化搜索+博弈论)
D. MADMAX time limit per test1 second memory limit per test256 megabytes Problem Description As we a ...
- Codeforces 1106E. Lunar New Year and Red Envelopes(DP)
E. Lunar New Year and Red Envelopes 题意: 在长度为n的时间轴上,有k个红包,每个红包有领取时间段[s,t],价值w,以及领了个这个红包之后,在时间d到来之前无法再 ...
- Codeforces Global Round 23 D.Paths on the Tree(记忆化搜索)
https://codeforces.ml/contest/1746/problem/D 题目大意:一棵n节点有根树,根节点为1,分别有两个数组 s[i] 顶点 i 的魅力值 c[i] 覆盖顶点 i ...
随机推荐
- Centos7使用Python3
1.安装python3替换python2.7 [root@Python src]# wget https://www.python.org/ftp/python/3.5.4/Python-3.5.4. ...
- PAT甲级——A1151 LCA_in_a_BinaryTree【30】
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...
- 十次艳遇单例设计模式(Singleton Pattern)
1.引言 单例设计模式(Singleton Pattern)是最简单且常见的设计模式之一,在它的核心结构中只包含一个被称为单例的特殊类.通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访 ...
- android中的SQLite数据库
SQLite是android中集成的一个轻量级的数据库,该数据库支持绝大部分SQL92语法 SQLiteDatabase代表一个数据库(底层就是一个数据库文件),一旦应用程序获得了代表指定数据库的SQ ...
- JS中的迭代(数组)
啥子是迭代?可以简单地理解为按顺序访问目标(数组.对象等)中的每一项(其实和遍历概念没什么差别) 数组的迭代被我分为两种: 查找 遍历 查找: 1.indexOf(item,start) 该方法搜索指 ...
- Rendering Problems The following classes could not be found:- android.support.v7.internal.app.WindowDecorActionBar (Fix Build Path, Create Class)
如图出现如下的错误的时候,一般都是升级Androdi Studio 后导致的,引入库不全,或者其他 东西缺少 可以如下解决方案:
- 2018-10-19-C#-GUID-ToString-
title author date CreateTime categories C# GUID ToString lindexi 2018-10-19 9:4:44 +0800 2018-4-1 10 ...
- iptables 防火墙(上)
iptables 防火墙(上) 1. 防火墙概述 1.1 概念与作用 网络中的防火墙是一种将内部网络和外部网络分开的方法,是一种隔离技术.防火墙在内网与外网通信时进行访问控制,依据所设置的规则对数据包 ...
- 【Luogu】【关卡2-11】简单数学问题(2017年10月)【还差三道题】
火星人 麦森数 P1403 [AHOI2005]约数研究 f(n)表示n的约数个数,现在给出n,要求求出f(1)到f(n)的总和. 解答:有几个1做约数的个数 = n /1; 有几个2做约数的个数 = ...
- 定时器实现Promise.all()的简单使用
// 异步事件1 function time1() { const promise = new Promise(function (resolve, reject) { setTimeout(func ...