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;
}

  

时间: 2024-11-05 22:48:35

Gym 101201H Paint (离散化+DP)的相关文章

HDU6447 YJJ&#39;s Salesman-2018CCPC网络赛-线段树求区间最值+离散化+dp

目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门 ?原题目描述在最下面. ?1e5个点,问从(0,0)走到(1e9,1e9)的最大收益. ?当你从(u-1,v-1)走到(u,v)时,你可以获得点(u,v)的权值. Solution: ?十分详细了. ?直接线段树区间最值.当然也可以树状数组,不能st表. ?\(dp[i] = max(query\_max(0,dp[i]-1,1)+val[i] ,

hdu 5009 Paint Pearls (dp)

Problem Description Lee has a string of n pearls. In the beginning, all the pearls have no color. He plans to color the pearls to make it more fascinating. He drew his ideal pattern of the string on a paper and asks for your help. In each operation,

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

区间dp.. dp( l , r ) 表示让 [ l , r ] 这个区间都变成目标颜色的最少涂色次数. 考虑转移 : l == r 则 dp( l , r ) = 1 ( 显然 ) s[ l ] == s[ l + 1 ] 则 dp( l , r ) = dp( l + 1 , r )     s[ r ] == s[ r - 1 ] 则 dp( l , r ) = dp( l , r - 1 )  因为只要在涂色时多涂一格就行了, 显然相等 , 所以转移一下之后更好做 s[ l ] == s

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

Reference:http://www.cnblogs.com/wuyiqi/archive/2012/03/28/2420916.html 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2836 题目大意:计算序列有多少种组合,每个组合至少两个数,使得组合中相邻两个数之差不超过H,序列有重复的数.MOD 9901. 解题思路: 朴素O(n^2) 以样例1 3 7 5为例,如果没有重复的数. 设dp[i]前n个数的方案数,初始化为1. 那么fo

【BZOJ-1260】涂色paint 区间DP

1260: [CQOI2007]涂色paint Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 1147  Solved: 698[Submit][Status][Discuss] Description 假设你有一条长度为5的木版,初始时没有涂过任何颜色.你希望把它的5个单位长度分别涂上红.绿.蓝.绿.红色,用一个长度为5的字符串表示这个目标:RGBGR. 每次你可以把一段连续的木版涂成一个给定的颜色,后涂的颜色覆盖先涂的颜色.例如第一次把木版涂成

hdu5009 离散化+dp+优化

西安网络赛C题.先对大数据离散化,dp优化 #include<iostream> //G++ #include<vector> #include<cstdio> #include<algorithm> #include<cstring> #include<string> #include<queue> #include<cmath> using namespace std; const int maxn=512

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

All Possible Increasing Subsequences Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Appoint description: Description An increasing subsequence from a sequence A1, A2 ... An is defined by Ai1, Ai2 ... Aik, where the followi

hdu 3450 离散化+dp+线段树优化

链接:http://acm.hdu.edu.cn/showproblem.php?pid=3450 题意: 给你一串长度为n的序列,给一个d,要求找出有几个子序列能够满足两个相邻的元素之间差值不超过d. 思路: dp.定义dp[i]表示以第i个为结束的满足条件的子序列的个数. 转移方程:dp[i]=(∑i?1j=1dp[j])+1(abs(num[i]?num[j])<=d) 答案就是dp数组的总和最后扣掉n就可以了. 此时会发现更新的时间复杂度是O(n2),这个显然是过不了的. 转移的复杂度是

hdu 4833 离散化+dp ****

先离散化,然后逆着dp,求出每个点能取到的最大利益,然后看有没有钱,有钱就投 想法好复杂 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 #include <set> 8 #include <map> 9