poj 1769 Minimizing maximizer 线段树维护dp
题目链接
给出m个区间, 按区间给出的顺序, 求出覆盖$ [1, n] $ 至少需要多少个区间。
如果先给出[10, 20], 在给出[1, 10], 那么相当于[10, 20]这一段没有被覆盖。
令dp[i]表示覆盖[1, i]需要的区间数量。 那么有状态转移方程dp[i] = $ min[dp[i], dp[j] (s_k <= j < t_k)] + 1 $
然后求 \([s_k, t_k]\) 的最小值可以用线段树来求。 复杂度 $ m\log{n} $
感觉这个题的难度在于理解题意....
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int maxn = 5e4+5;
int minn[maxn<<2];
void update(int p, int val, int l, int r, int rt) {
if(l == r) {
minn[rt] = min(val, minn[rt]);
return ;
}
int m = l+r>>1;
if(p<=m)
update(p, val, lson);
else
update(p, val, rson);
minn[rt] = min(minn[rt<<1], minn[rt<<1|1]);
}
int query(int L, int R, int l, int r, int rt) {
if(L<=l&&R>=r) {
return minn[rt];
}
int m = l+r>>1, ret = inf;
if(L<=m)
ret = min(ret, query(L, R, lson));
if(R>m)
ret = min(ret, query(L, R, rson));
return ret;
}
int main()
{
int n, m, a, b, x;
while(~scanf("%d%d", &n, &m)) {
mem2(minn);
update(1, 0, 1, n, 1);
for(int i = 0; i < m; i++) {
scanf("%d%d", &a, &b);
int x = query(a, b-1, 1, n, 1);
update(b, x+1, 1, n, 1);
}
printf("%d\n", query(n, n, 1, n, 1));
}
return 0;
}
poj 1769 Minimizing maximizer 线段树维护dp的更多相关文章
- POJ.1769.Minimizing maximizer(线段树 DP)
题目链接 /* 题意:有m个区间,问最少要多少个区间能覆盖[1,n] 注:区间要按原区间的顺序,不能用排序贪心做 设dp[i]表示最右端端点为i时的最小值 dp[e[i]]=min{dp[s[i]]~ ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake 线段树维护dp
D. Babaei and Birthday Cake 题目连接: http://www.codeforces.com/contest/629/problem/D Description As you ...
- POJ 1769 Minimizing maximizer(DP+zkw线段树)
[题目链接] http://poj.org/problem?id=1769 [题目大意] 给出一些排序器,能够将区间li到ri进行排序,排序器按一定顺序摆放 问在排序器顺序不变的情况下,一定能够将最大 ...
- Codeforces GYM 100114 D. Selection 线段树维护DP
D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...
- 【8.26校内测试】【重构树求直径】【BFS模拟】【线段树维护DP】
题目性质比较显然,相同颜色联通块可以合并成一个点,重新建树后,发现相邻两个点的颜色一定是不一样的. 然后发现,对于一条链来说,每次把一个点反色,实际上使点数少了2个.如下图 而如果一条链上面有分支,也 ...
- 2019牛客暑期多校训练营(第二场)E 线段树维护dp转移矩阵
题意 给一个\(n\times m\)的01矩阵,1代表有墙,否则没有,每一步可以从\(b[i][j]\)走到\(b[i+1][j]\),\(b[i][j-1]\),\(b[i][j+1]\),有两种 ...
- Codeforces750E. New Year and Old Subsequence (线段树维护DP)
题意:长为2e5的数字串 每次询问一个区间 求删掉最少几个字符使得区间有2017子序列 没有2016子序列 不合法输出-1 题解:dp i,p(0-4)表示第i个数匹配到2017的p位置删掉的最少数 ...
随机推荐
- sql从某不连续的数字中将其分段并找出缺失的数字并分段
首先做准备数据 )) ') ') ') ') ') ') ') ') ') ') ') ') ') ') ') ') 将数据转换成应该处理的数据格式 ),colValue INT ) ) ,LEN(c ...
- Hibernate工作原理及为什么要用?(转http://www.cnblogs.com/javaNewegg/archive/2011/08/28/2156521.html)
原理:1.通过Configuration().configure();读取并解析hibernate.cfg.xml配置文件2.由hibernate.cfg.xml中的<mapping resou ...
- 《C++ 标准库》读书笔记 - 第二章 Introduction to C++ and the Standard Library
1. History of the C++ Standards 1.1 History of the C++ Standards C++98 -> C++03 -> TR1 -> C ...
- Web QQ自动强制加好友代码
也许见过强行聊天的代码: tencent://Message/?Uin=574201314&websiteName=www.oicqzone.com&Menu=yes 但是你应该不知 ...
- django之HttpRequest对象
class HttpRequest[source] 属性 所有的属性都是只读的,除非另有说明 HttpRequest.scheme 字符串(http/https)表示http还是https请求 Htt ...
- C语言基础04
什么叫数组呢?我们的第一反应是很多数组合在一起就是数组,字面意思,当然不仅仅是数字,字符也是可以的. 数组属于构造类型 .指相同类型的若干变量组织起来. 类型说明符 数组名 [常量表达式] int ...
- C++ Primer第四版 15.9 再谈文本查询 程序实现
编程过程中发现书本中的示例程序并不完全,某些地方存在错误,现已改正并添加少许注释.. 1 #include<iostream> 2 #include<fstream> #inc ...
- C语言程序设计(翁恺)--第三周课件中的三个遗留点
刚刚写完第二周遗留点,下面写第三周的 第三周:判断 1.if和else后面也可以没有{}而是一条语句.如果if后不带{},但是后面跟了两条语句,并且后面还有else语句,那么程序会怎么执行? 在Dev ...
- jQuery学习-事件之绑定事件(七)
今天来说说事件中的handlers方法中的一个片段 1 matches[ sel ] = handleObj.needsContext ? 2 jQuery( sel, this ).in ...
- Static Function Test
public class StaticTestCls { public int x = 0; public static int y = 0; private void SetValue_Object ...