题目大意:

给定n m

接下来给定m个在n范围内的段的左右端 l r

求选取m-2段 最多能覆盖多少格

#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
#define mem(i,j) memset(i,j,sizeof(i))
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,r,l) for(int i=r;i>=l;i--)
const int N=5e3+; int n, m, L[N], dp[N];
// L[i] 表示 所有画到i的段中 l最左的一段的左端
// dp[i] 表示到i为止 选出当前k段 的最优解 int main()
{
while(~scanf("%d%d",&n,&m)) {
inc(i,,n) L[i]=i+, dp[i]=;
// L[]置为i+1是为了表示没有被画过的状态
inc(i,,m) {
int l,r; scanf("%d%d",&l,&r);
inc(j,l,r) L[j]=min(L[j],l);
}
inc(k,,m-) {
dec(i,n,) dp[i]=max(dp[L[i]-]+i-L[i]+,dp[i]);
// L[i]~i的一段都被画了 长度为 i-L[i]+1
// 所以 dp[L[i]-1] 加上 L[i]到i被画的一段 更新dp[i]
inc(i,,n) dp[i]=max(dp[i],dp[i-]);
// 短的段存在更优的方案 自然可以更新长的段
}
printf("%d\n",dp[n]);
} return ;
}

eduCF#61 C. Painting the Fence /// DP 选取k段能覆盖的格数的更多相关文章

  1. Educational Codeforces Round 61 Editorial--C. Painting the Fence

    https://codeforces.com/contest/1132/problem/C 采用逆向思维,要求最大的覆盖,就先求出总的覆盖,然后减去删除两个人贡献最少的人 #include<io ...

  2. eduCF#61 F. Clear the String /// 区间DP 消除连续一段相同字符 全部消完的最少次数

    题目大意: 给定字符串 每次消除可消除连续的一段相同的字符的子串 求消除整个字符串的最少消除次数 #include <bits/stdc++.h> using namespace std; ...

  3. SGU 183. Painting the balls( dp )

    dp..dp(i, j)表示画两个点为i-j, i的最优答案. dp(i, j) = min{ dp(i-j, k) } + cost[i] (1≤k≤M-j) 令f(i, j) = min{dp(i ...

  4. [luogu P2205] [USACO13JAN]画栅栏Painting the Fence

    [luogu P2205] [USACO13JAN]画栅栏Painting the Fence 题目描述 Farmer John has devised a brilliant method to p ...

  5. 洛谷 画栅栏Painting the Fence 解题报告

    P2205 画栅栏Painting the Fence 题目描述 \(Farmer\) \(John\) 想出了一个给牛棚旁的长围墙涂色的好方法.(为了简单起见,我们把围墙看做一维的数轴,每一个单位长 ...

  6. Painting The Fence(贪心+优先队列)

    Painting The Fence(贪心+优先队列) 题目大意:给 m 种数字,一共 n 个,从前往后填,相同的数字最多 k 个在一起,输出构造方案,没有则输出"-1". 解题思 ...

  7. 洛谷——P2205 [USACO13JAN]画栅栏Painting the Fence

    题目描述 Farmer John has devised a brilliant method to paint the long fence next to his barn (think of t ...

  8. CodeForce - 1187 E. Tree Painting (换根dp)

    You are given a tree (an undirected connected acyclic graph) consisting of nn vertices. You are play ...

  9. poj1821 Fence(dp,单调队列优化)

    题意: 由k(1 <= K <= 100)个工人组成的团队应油漆围墙,其中包含N(1 <= N <= 16 000)个从左到右从1到N编号的木板.每个工人i(1 <= i ...

随机推荐

  1. 元类,sqlalchemy查询

    import sqlalchemy from sqlalchemy.ext.declarative import declarative_base #创建连接实例 db = sqlalchemy.cr ...

  2. neo4j简单学习

    阅读更多 背景 最近在一些论坛或者新闻里看到了neo4j,一种擅长处理图形的数据库. 据说非常适合做一些join关系型的查询,所以抽空也看了下相关文档,给自己做个技术储备. 过程 深入学习之前,先在网 ...

  3. Pytest -断言、跳过及运行

    基本断言方法: Pytest框架assert断言使用 • 断言:支持显示最常见的子表达式的值,包括调用,属性,比较以及二元和一元运算 符. • 包含,相等,不等,大于 小于运算,assertnot 假 ...

  4. MySql进行批量插入时的几种sql写法

    insert into:插入数据,如果主键重复,则报错 insert repalce:插入替换数据,如果存在主键或unique数据则替换数据 insert ignore:如果存在数据,则忽略. INS ...

  5. codeforces 724G - Xor-matic Number of the Graph 线性基+图

    题目传送门 题意:给出衣服无向带权图,问有多少对合法的$<u,v,s>$,要求$u$到$v$存在一条路径(不一定是简单路径)权值异或和等于$s$,并且$u<v$.求所有合法三元组的s ...

  6. html浮动小问题

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. node js实战:带数据库,加密的注册登录表单

    demo 注册效果: 登陆效果:        数据库截图: 数据库操作 db.js //这个模块里面封装了所有对数据库的常用操作 var MongoClient = require('mongodb ...

  8. vue filters 日期

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. The 'with' and 'as' Keywords

    Programming is all about getting the computer to do the work. Is there a way to get Python to automa ...

  10. Windows下生成目录结构树

    1.进入需要生成目录结构的文件主目录 2.在当前目录打开命令行 3.输入命令 tree /f > list.txt 4.即在当前目录下生成了list.txt文件