Gym 101201H Paint (离散化+DP)
题意:给定 n 个区间,让你选出一些,使得每个选出区间不交叉,并且覆盖区间最大。
析:最容易想到的先是离散化,然后最先想到的就是 O(n^2)的复杂度,dp[i] = max(dp[j] + a[i].r - a[i].l) 区间不相交,这个可以用线段树来维护一个最大值,因为有区间性,但是也可以不用线段树,直接进行线性DP,因为要选的区间越多越好,相比而言,有就要选,但是有更优的就选最优的。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 2e5 + 50;
const LL mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} struct Node{
LL l, r;
bool operator < (const Node &p) const{
return l < p.l || l == p.l && r < p.r;
}
};
map<LL, int> mp;
Node a[maxn];
vector<LL> v;
LL dp[maxn<<1]; int main(){
LL n;
scanf("%I64d %d", &n, &m);
for(int i = 0; i < m; ++i){
scanf("%I64d %I64d", &a[i].l, &a[i].r);
++a[i].r;
v.pb(a[i].l); v.pb(a[i].r);
} sort(a, a + m);
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for(int i = 0; i < v.sz; ++i) mp[v[i]] = i;
int idx = 0;
LL ans = 0; for(int i = 0; i < v.sz; ++i){
dp[i+1] = max(dp[i+1], dp[i]);
while(idx < m && mp[a[idx].l] == i){
int r = mp[a[idx].r];
dp[r] = max(dp[r], dp[i] + a[idx].r - a[idx].l);
++idx;
}
ans = max(ans, dp[i]);
} printf("%I64d\n", n - ans);
return 0;
}
Gym 101201H Paint (离散化+DP)的更多相关文章
- 项目安排(离散化+DP)
题目来源:网易有道2013年校园招聘面试二面试题 题目描述: 小明每天都在开源社区上做项目,假设每天他都有很多项目可以选,其中每个项目都有一个开始时间和截止时间,假设做完每个项目后,拿到报酬都是不同的 ...
- 【BZOJ-1260】涂色paint 区间DP
1260: [CQOI2007]涂色paint Time Limit: 30 Sec Memory Limit: 64 MBSubmit: 1147 Solved: 698[Submit][Sta ...
- HDU 2836 (离散化DP+区间优化)
Reference:http://www.cnblogs.com/wuyiqi/archive/2012/03/28/2420916.html 题目链接: http://acm.hdu.edu.cn/ ...
- hdu 4833 离散化+dp ****
先离散化,然后逆着dp,求出每个点能取到的最大利益,然后看有没有钱,有钱就投 想法好复杂 #include <stdio.h> #include <string.h> #inc ...
- Codeforces Gym 100231L Intervals 数位DP
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description Start with an integer, N0, ...
- LightOJ 1085(树状数组+离散化+DP,线段树)
All Possible Increasing Subsequences Time Limit:3000MS Memory Limit:65536KB 64bit IO Format: ...
- BZOJ 1260: [CQOI2007]涂色paint( 区间dp )
区间dp.. dp( l , r ) 表示让 [ l , r ] 这个区间都变成目标颜色的最少涂色次数. 考虑转移 : l == r 则 dp( l , r ) = 1 ( 显然 ) s[ l ] = ...
- G - Surf Gym - 100819S -逆向背包DP
G - Surf Gym - 100819S 思路 :有点类似 逆向背包DP , 因为这些事件发生后是对后面的时间有影响. 所以,我们 进行逆向DP,具体 见代码实现. #include<bit ...
- poj 3666 Making the Grade(离散化+dp)
Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...
随机推荐
- logstash使用分享
1.logstash时间处理函数 当业务场景需要自有的time字段覆盖@timestamp字段的情况下 需要使用 date { match => ["time", " ...
- oracle undo表空间大小修改
redhat:清空回收站 rm -rf /home/登录用户名/.Trash 例子:rm -rf /home/.Trash-root df命令可以显示目前所有文件系统的可用空间及使用情形: 例子:d ...
- Java 判断是否包含指定的子串 contains()
Java 手册 contains public boolean contains(CharSequence s) 当且仅当此字符串包含指定的 char 值序列时,返回 true. 参数: s - 要搜 ...
- [C#]反射相关
//Type type = DataHelper.Instance.GetType(); //MethodInfo[] infos = type.GetMethods(BindingFlags.Ins ...
- 灵活运用SMART原则梳理目标
1:立刻行动 执行力是老生常谈了,提高执行力不拖延,源自于对目标的充分理解和超出常人的坚持. 如何深刻理解目标?一方面要有基础的知识.能力积累,另一方面要灵活运用SMART原则从不同维度梳理目标. 2 ...
- php调用dll经验小结
最近做一个网站,需要频繁使用远程数据,数据接口已经做好.在做转换的时候遇到了性能上的问题:开始打算用php来实现转换,苦苦查了数天,都没有找到直接操作字节的方法.虽然可以使用 pack() 方法将各个 ...
- sort+函数指针、sort+比较器对象、qsort速度比较
一.上代码 #include<bits/stdc++.h> using namespace std; #define MAXN 50000000 struct TS { int a, b, ...
- PMON和SMON的功能
PMON:进程监控进程进程负责在反常中断的连接之后的清理工作.例如,如果因某些原因专用服务“故障”或被kill掉,PMON就是负责处理(恢复或回滚工作)和释放你的资源.PMON将发出未提交工作的回滚, ...
- 读java并发编程笔记
同步策略:在共享资源上面加锁 java监视器模式:class对象-与之对应的锁(内置锁)[对象锁与class锁] 执行策略: 取消策略: =============================== ...
- Python开发一个WEB聊天室
项目实战:开发一个WEB聊天室 功能需求: 用户可以与好友一对一聊天 可以搜索.添加某人为好友 用户可以搜索和添加群 每个群有管理员可以审批用户的加群请求,群管理员可以用多个,群管理员可以删除.添加. ...