题意:给定 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)的更多相关文章

  1. 项目安排(离散化+DP)

    题目来源:网易有道2013年校园招聘面试二面试题 题目描述: 小明每天都在开源社区上做项目,假设每天他都有很多项目可以选,其中每个项目都有一个开始时间和截止时间,假设做完每个项目后,拿到报酬都是不同的 ...

  2. 【BZOJ-1260】涂色paint 区间DP

    1260: [CQOI2007]涂色paint Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 1147  Solved: 698[Submit][Sta ...

  3. HDU 2836 (离散化DP+区间优化)

    Reference:http://www.cnblogs.com/wuyiqi/archive/2012/03/28/2420916.html 题目链接: http://acm.hdu.edu.cn/ ...

  4. hdu 4833 离散化+dp ****

    先离散化,然后逆着dp,求出每个点能取到的最大利益,然后看有没有钱,有钱就投 想法好复杂 #include <stdio.h> #include <string.h> #inc ...

  5. Codeforces Gym 100231L Intervals 数位DP

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description Start with an integer, N0, ...

  6. LightOJ 1085(树状数组+离散化+DP,线段树)

    All Possible Increasing Subsequences Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format: ...

  7. BZOJ 1260: [CQOI2007]涂色paint( 区间dp )

    区间dp.. dp( l , r ) 表示让 [ l , r ] 这个区间都变成目标颜色的最少涂色次数. 考虑转移 : l == r 则 dp( l , r ) = 1 ( 显然 ) s[ l ] = ...

  8. G - Surf Gym - 100819S -逆向背包DP

    G - Surf Gym - 100819S 思路 :有点类似 逆向背包DP , 因为这些事件发生后是对后面的时间有影响. 所以,我们 进行逆向DP,具体 见代码实现. #include<bit ...

  9. poj 3666 Making the Grade(离散化+dp)

    Description A straight dirt road connects two fields on FJ's farm, but it changes elevation more tha ...

随机推荐

  1. windows mac ssh 出国访问google等系列网站

    淘宝买ssh代理 windows:tunnelier mac:issh 浏览器:chrome  插件:switchysharp

  2. WinForm应用程序中实现自动更新功能

    WinForm应用程序中实现自动更新功能 编写人:左丘文 2015-4-20 近来在给一客户实施ECM系统,但他们使用功能并不是我们ECM制造版提供的标准功能,他们要求对系统作一些定制功能,为了避免因 ...

  3. vim配置之安装脚本

    vimConfig/install/install.sh git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle cp ...

  4. QT 控制LED实验

    1.实验准备 在PC 机D:盘下创建文件夹qt-led,将光盘qt_led_exp 文件夹下的images 文件夹拷贝到E:盘下qt-led 文件夹qt-led 内 2.新建工程 新建一个Empty ...

  5. 去掉Linux尖锐的提示音

    新装CentOS7,Tab键没有结果的时候,总是有尖锐的提示音,下面是如何去除这个提示音: # /etc/inputrc 找到 ‘#set bell-style none’,去掉注释# # reboo ...

  6. 《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #6 使用localmodconfig缩短编译时间

    HACK #6 使用localmodconfig缩短编译时间 本节介绍使用make localmodconfig生成精简的.config文件,缩短内核编译时间的方法.为了能够应对各种各样的环境,发布版 ...

  7. pm无力的话

    1. 先这样做吧, 等不行再改 2. 用户的需求不明确, 他们对于自己的业务也不明白, 现在是我们在帮助他们缕清自己的业务, 这个迭代的过程中,有很多问题,我们程序员既不能参与到业务, 也不能猜测业务 ...

  8. Spring boot with Apache Hive

      5.29.1. Maven <dependency> <groupId>org.springframework.boot</groupId> <artif ...

  9. 匹配ip等的正则式

  10. 在ubuntu14.04上搭建OpenVPN服务

    简介 在连接了不可信的网络环境后,让手机或者计算机安全的访问互联网,使用虚拟专用网络(Virtual Private Network,VPN)是一个解决办法.OpenVPN是一个SSL VPN完整解决 ...