Codeforces #536 div2 E (1106E)Lunar New Year and Red Envelopes (DP)
题意:过年了,Bob要抢红包。抢红包的时间段为1 - n,有m个红包,每个红包有三个属性:st(红包出现的时间), ed(红包消失的时间),d(如果抢了这个红包,能够抢下一个红包的时间),w(红包的收益)。注:结束时间为ed是指在ed + 1的时候才能抢其它的红包,d同理。Bob是一个贪心的人,如果当前时间段他可以抢红包,他会抢现在出现的红包中收益最大的红包。如果有多个收益最大的红包,他会抢d最大的那个。Alice可以打断Bob k次,每次打断可以使Bob在1秒内无法行动,下一秒恢复正常。现在问Bob可以获得的最小的收益是多少?
思路:这种题一看就知道常规方法解决不了啦,只能DP了。首先,每个时间点抢的是什么红包其实是固定的,我们只需要先把红包按开始时间排序,然后用堆或者mutiset维护这个时间点抢什么。其次,我们可以发现,如果Bob抢了某个红包,他只有在特定的时间之后才能抢下一个红包,这是明显的状态转移过程。我们设dp[i][j]为处于时间点i,还可以打扰j次的最小收益。那么我们可以执行两种转移:
1:我们抢这个红包,(设这个红包的w为wi,d为di)那么dp[di + 1][j ] = min(dp[di + 1][j], dp[i][j] + wi)
2:现在不抢,用掉一次打扰机会,那么dp[i + 1][j - 1] = min(dp[i + 1][j - 1], dp[i][j]);
代码:
#include <cstdio>
#include <algorithm>
#include <vector>
#include <iostream>
#include <cstring>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#include <cmath>
#include <string>
#define INF 0x3f3f3f3f3f3f3f3f
#define pii pair<int, int>
#define lowbit(x) (x & (-x))
#define ls(x) (x << 1)
#define rs(x) ((x << 1) | 1)
#define LL long long
using namespace std;
const int maxn = 100010;
struct node {
int st, ed, d, pos;
LL w;
bool operator < (const node& rhs) const {
if(w == rhs.w) return d < rhs.d;
return w < rhs.w;
}
};
node a[maxn];
vector<int> b[maxn],c[maxn];
priority_queue<node> q;
LL dp[maxn][210];
node re[maxn];
bool v1[maxn];
bool cmp(node x, node y) {
if(x.st == y.st) return x.ed < y.ed;
return x.st < y.st;
}
int main() {
int n, k, m;
scanf("%d%d%d", &n, &k ,&m);
for (int i = 1; i <= m; i++) {
scanf("%d%d%d%d", &a[i].st, &a[i].ed, &a[i].d, &a[i].w);
}
sort(a + 1, a + 1 + m);
for (int i = 1; i <= m; i++) {
b[a[i].st].push_back(i);
c[a[i].ed].push_back(i);
a[i].pos = i;
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < b[i].size(); j++) {
int y = b[i][j];
q.push(a[y]);
}
while(q.size() && v1[q.top().pos]) {
q.pop();
}
if(!q.empty())
re[i] = q.top();
else
re[i] = (node) {0, 0, i, 0, 0};
for (int j = 0; j < c[i].size(); j++) {
int y = c[i][j];
v1[y] = 1;
}
}
memset(dp, 0x3f, sizeof(dp));
dp[1][k] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= k; j++) {
int d = re[i].d;
if(j) dp[i + 1][j - 1] = min(dp[i + 1][j - 1], dp[i][j]);
dp[d + 1][j] = min(dp[d + 1][j], dp[i][j] + re[i].w);
}
}
LL ans = INF;
for (int i = 0; i <= k; i++) {
ans = min(ans, dp[n + 1][i]);
}
printf("%lld\n", ans);
}
Codeforces #536 div2 E (1106E)Lunar New Year and Red Envelopes (DP)的更多相关文章
- 【Codeforces 1106E】 Lunar New Year and Red Envelopes
Codeforces 1106 E 题意:有\(k\)个红包,第\(i\)个红包可以在\(s_i\)到\(t_i\)的时间内抢,同时获得\(w_i\)的钱,但是抢完以后一直到\(d_i\)都不可以继续 ...
- 【Codeforces 1106E】Lunar New Year and Red Envelopes
[链接] 我是链接,点我呀:) [题意] 给你k个红包,每个红包可以在si..ti的时间范围内拿走. 抢完红包之后你得到wi元,然后你需要在di+1时刻才能继续抢红包 时间是线性的从1..n 然后某个 ...
- CF - 1106 E Lunar New Year and Red Envelopes DP
题目传送门 题解: 首先要处理出每个时间点会选择哪一个线段. 对于这个问题,可以用multiset去维护信息. 当时间线开始的时候,往mutiset里面插入这个信息,当时间线结束的时候,删除这个信息. ...
- Codeforces 1106E. Lunar New Year and Red Envelopes(DP)
E. Lunar New Year and Red Envelopes 题意: 在长度为n的时间轴上,有k个红包,每个红包有领取时间段[s,t],价值w,以及领了个这个红包之后,在时间d到来之前无法再 ...
- Codeforces I. Producing Snow(优先队列)
题目描述: C. Producing Snow time limit per test 1 second memory limit per test 256 megabytes input stand ...
- Codeforces 1500F - Cupboards Jumps(set)
Codeforces 题面传送门 & 洛谷题面传送门 nb tea!!!111 首先很显然的一件事是对于三个数 \(a,b,c\),其最大值与最小值的差就是三个数之间两两绝对值的较大值,即 \ ...
- 【CodeForces - 651C 】Watchmen(map)
Watchmen 直接上中文 Descriptions: 钟表匠们的好基友马医生和蛋蛋现在要执行拯救表匠们的任务.在平面内一共有n个表匠,第i个表匠的位置为(xi, yi). 他们需要安排一个任务计划 ...
- [Codeforces 274E]:Mirror Room(模拟)
题目传送门 题目描述 有一个$n\times m$的格子图,其中有一些是黑色的,另一些为白色.从某个白色格子的中心点向左上($NW$),左下($SW$),右上($NE$),右下($SE$)四个方向中的 ...
- CodeForces - 1162E Thanos Nim (博弈论)
Alice and Bob are playing a game with nn piles of stones. It is guaranteed that nn is an even number ...
随机推荐
- 《Advanced Bash-scripting Guide》学习(五):检查一个可执行文件是否存在
本文所选的例子来自于<Advanced Bash-scripting Gudie>一书,译者 杨春敏 黄毅 ABS书上的例子是这样的: #!/bin/bash echo hello;ech ...
- Amazon EMR(Elastic MapReduce):亚马逊Hadoop托管服务运行架构&Hadoop云服务之战:微软vs.亚马逊
http://s3tools.org/s3cmd Amazon Elastic MapReduce (Amazon EMR)简介 Amazon Elastic MapReduce (Amazon EM ...
- Asp.net 异步调用WebService
//服务代码 [WebMethod] public string Test(int sleepTimes, int val) { Thread.Sleep(sleepTimes); var log = ...
- js比较函数
//1.//bySort函数接受一个首要比较字符串和一个可选的次要比较函数做为参数//并返回一个可以用来包含该成员的对象数组进行排序的比较函数//当o[firstName] 和 p[firstName ...
- IDEA提交Git时忽略文件【ignore文件备份】
IntellJ IDEA设置ignore文件忽略,需: 第一步:下载idea的ignore插件,链接:https://pan.baidu.com/s/14_cfq56g1s7Lc6LdiolvWA 密 ...
- OpenCV - win7+vs2013(2012)+opencv3.0.0 环境配置 (以及配置技巧)
1. opencv 3.0.0 库下载地址, 这里的版本是3.0.0,其他的版本配置可能不一样,请大家注意. http://sourceforge.net/projects/opencvlibrary ...
- ubuntu lts install licode tag pre-v5.4
1. Requirements Ubuntu 14.04 LTS 2. Clone Licode codeYou first need to clone our code from github.Yo ...
- laravel config文件的使用
好多东西 由于许多地方都要使用与将来可能发生更改 我们需要把它提取出来 作为配置文件来使用 这样将来要修改的时候 只需要修改一处即可 学习源头: https://blog.csdn.net/linyu ...
- UVA548(二叉树遍历)
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
- 机器学习:模型泛化(岭回归:Ridge Regression)
一.基础理解 模型正则化(Regularization) # 有多种操作方差,岭回归只是其中一种方式: 功能:通过限制超参数大小,解决过拟合或者模型含有的巨大的方差误差的问题: 影响拟合曲线的两个因子 ...