区间DP || HDU 6249 Alice’s Stamps
题意:标号为1-n的n种邮票,m个邮票集,每个集里有标号从Li到Ri的邮票,要从中选K个邮票集,使这K个邮票集能覆盖最多种的邮票,问最多能覆盖多少种邮票
思路:区间DP (我:???
f[i][j]表示从1 - i 位置选择 j 个集合的覆盖种数
首先它可以从f[i][j] = max(f[i][j], max(f[i-1][j], f[i][j-1]))转移来
其次考虑它能转移到的点
用up[i] 记录覆盖i点的线段最右的点,如果要在 i 后面加一条线段,那么肯定优先取这个(因为它最右,同样加一条线段得到的覆盖长度更长
所以f[i][j]可以转移到f[up[i]][j], if(up[i]) f[up[i]][j] = max(f[i-1][j-1]+up[i]-i+1, f[up[i]][j]);
嘤嘤嘤的哭了
区间DP的话,把区间排个序,然后去掉有完全包含的区间~
#include <map>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = ;
const int maxm = ;
struct node
{
int l, r;
}a[maxn];
bool cmp(node x, node y)
{
if(x.l != y.l) return x.l<y.l;
return x.r<y.r;
}
int up[maxn], f[maxn][maxn];
int main()
{
int T, tt=;
scanf("%d", &T);
while(T--)
{
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
memset(up, , sizeof(up));
memset(f, , sizeof(f));
for(int i = ; i <= m; i++)
{
scanf("%d%d", &a[i].l, &a[i].r);
for(int j = a[i].l; j <= a[i].r; j++)
up[j] = max(up[j], a[i].r);
}
sort(a+, a++m, cmp);
for(int i = ; i <= n; i++)
{
for(int j = ; j <= k; j++)
{
f[i][j] = max(f[i][j], max(f[i-][j], f[i][j-]));
if(up[i]) f[up[i]][j] = max(f[up[i]][j], f[i-][j-]+up[i]-i+);
}
}
/*for(int i = 1; i <= n; i++)
for(int j = 1; j <= k; j++)
printf("f[%d][%d] = %d\n", i, j, f[i][j]);*/
printf("Case #%d: %d\n", ++tt, f[n][k]);
}
return ;
}
区间DP || HDU 6249 Alice’s Stamps的更多相关文章
- HDU 6249 Alice’s Stamps(2017 CCPC-Final G题,DP)
题目链接 HDU 6249 题意 给定$m$个区间,在这些区间中选出不超过$k$个,求被覆盖的点的数量的最大值. 设$f[i][j]$表示选到第$i$个点并选了$j$个区间的时候能得到的最大答案. 处 ...
- HDU 6249 Alice’s Stamps(dp)
http://acm.hdu.edu.cn/showproblem.php?pid=6249 题意: 给出n个区间,求选k个区间的最大区间并. 思路: 可能存在左端点相同的多个区间,那么此时我们肯定选 ...
- HDU 6249 Alice’s Stamps
[题目链接] 题目大意: 说有$m$个区间,要求选出不超过$k$个区间,使这些区间覆盖的长度最长,问最长长度是多少. 题解: 所有区间按$R$从小到大排序之后可以进行$dp$. $dp[i][j]$表 ...
- 区间DP HDU 4283
t个数据 n个权值 1->n 可以入栈调整顺序 花费 第k个出来 w[i]*(k-1); 求花费最少 #include<stdio.h> #include<string.h&g ...
- 区间DP HDU 2476
两个字符串s1,s2 从s1->s2 最少刷几次 刷 i->j 都变成一样的+1 #include<stdio.h> #include<string.h> usin ...
- Alice’s Stamps HDU - 6249 (区间DP)
点击传送 Alice’s Stamps Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- hdu 4597 Play Game 区间dp
Play Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=459 ...
- HDU 4283---You Are the One(区间DP)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=4283 Problem Description The TV shows such as Y ...
- HDU 4293---Groups(区间DP)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=4293 Problem Description After the regional con ...
随机推荐
- 【旧文章搬运】改PEB中的映像路径可以这样~
原文发表于百度空间,2008-7-26========================================================================== 用常用的几个 ...
- VS2008 MFC截取整个屏幕并保存为jpg格式
void CMainFrame::OnSavejpg() { // TODO: 在此添加命令处理程序代码 HWND hwnd = this->GetSafeHwnd(); //得到窗口句柄 HD ...
- error the @annotation pointcut expression is only supported at Java 5
eclipse搭建环境后报错 the pointcut is supported at Java 5 错误意思大致是:注释切入点表达式只支持在Java 5版本以上,我就纳闷了我安装的是jdk1.8啊, ...
- Swift异常处理的try?与try!
首先要明白抛出异常后异常的运动:异常被抛出后,中断整个处理,异常不断向外层(范围)传递,直到遇到catch代码块群,会与catch代码块的条件进行匹配,匹配符合则进入此代码块处理.如果遇到没有条件的c ...
- 定位,标记,Socket通信传输位置
# -*- coding: utf- -*- """ Editor : X-POWER Date : -- Motto : talk is cheap,show me y ...
- logrotate日志转储
1 工具目录 ***系统开启selinux,logrotate会不生效*** linux默认会安装logrotate工具,自身的boot.log就是通过它分割转储的. [root@webmaster ...
- AtCoder Grand Contest 016 F - Games on DAG
题目传送门:https://agc016.contest.atcoder.jp/tasks/agc016_f 题目大意: 给定一个\(N\)点\(M\)边的DAG,\(x_i\)有边连向\(y_i\) ...
- 贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges
题目传送门 /* 题意:问最少增加多少值使变成递增序列 贪心:排序后,每一个值改为前一个值+1,有可能a[i-1] = a[i] + 1,所以要 >= */ #include <cstdi ...
- 164 Maximum Gap 最大间距
给定一个无序的数组,找出数组在排序后相邻的元素之间最大的差值.尽量尝试在线性时间和空间复杂度情况下解决此问题.若数组元素个数少于2,则返回0.假定所有的元素都是非负整数且范围在32位有符号整数范围内. ...
- webpack采坑十连跳
4.css.js一同打包进html ---------------- https://www.cnblogs.com/amiezhang/p/9723565.html 依赖内联插件 HtmlWebp ...