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 ...
随机推荐
- 自动化工具 Python 调 Jmeter 执行参数化 jmx 脚本
烦透了jmeter输入如下鬼命令: Jmeter -n -t {tmpjmxfile} -l {csvfilename} -e -o {htmlreportpath} 尤其是{htmlreportpa ...
- LINQ to SQL 系列 如何使用LINQ to SQL插入、修改、删除数据 (转)
http://www.cnblogs.com/yukaizhao/archive/2010/05/13/linq_to_sql_1.html LINQ和 LINQ to SQL 都已经不是一个新事物了 ...
- 【python】Beautiful Soup的使用
1. Beautiful Soup的简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.pyt ...
- JSP的taglib示例
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2 ...
- 匹配ip等的正则式
- linux下创建具有root权限的账户
http://blog.chinaunix.net/uid-24631445-id-2981034.html
- (转存)面向切面编程(AOP)的理解
面向切面编程(AOP)的理解 标签: aop编程 2010-06-14 20:17 45894人阅读 评论(11) 收藏 举报 分类: Spring(9) 在传统的编写业务逻辑处理代码时,我们通常 ...
- Python Geoip 获取IP地址经度、纬度
简介: 除了一些免费的 API 接口,例如 http://ipinfo.io/223.155.166.172 可以得到一些信息外,还可以通过 python-geoip 库来解决这个问题. shell ...
- The web application [/zzti] registered the JDBC driver [com.microsoft.sqlserver.jdbc.SQLServerDriver
先说下题目:因为是在我进行处理项目升级时同时遇到了上面的问题,一般都会遇到,所以就一并说了 不罗嗦了,正题: 在本地服务器中提供tomcat6,然后在myeclipse中配置severs tomcat ...
- VS2008项目移植到Linux
不少人都遇到过这种情况:在Windows下用Visual Studio工具开发的程序需要移植到Linux系统中,做成Linux版本的,但程序比较大,在Linux上又离不开Make,手动编写Makefi ...