题解链接:

https://www.lucien.ink/archives/121/


题目链接:

http://exam.upc.edu.cn/problem.php?id=5572


题目:

题目描述

Farmer John has opened a swimming pool for his cows, figuring it will help them relax and produce more milk.
To ensure safety, he hires NN cows as lifeguards, each of which has a shift that covers some contiguous interval of time during the day. For simplicity, the pool is open from time t=0 until time t=1,000,000,000 on a daily basis, so each shift can be described by two integers, giving the time at which a cow starts and ends her shift. For example, a lifeguard starting at time t=4 and ending at time t=7 covers three units of time (note that the endpoints are “points” in time).

Unfortunately, Farmer John hired 1 more lifeguard than he has the funds to support. Given that he must fire exactly one lifeguard, what is the maximum amount of time that can still be covered by the shifts of the remaining lifeguards? An interval of time is covered if at least one lifeguard is present.

输入

The first line of input contains N (1≤N≤100,000). Each of the next N lines describes a lifeguard in terms of two integers in the range 0…1,000,000,000, giving the starting and ending point of a lifeguard’s shift. All such endpoints are distinct. Shifts of different lifeguards might overlap.

输出

Please write a single number, giving the maximum amount of time that can still be covered if Farmer John fires 1 lifeguard.

样例输入

3
5 9
1 4
3 7

样例输出

7

题意:

  给你nnn条线段,每条线段的覆盖区间为[l,r]" role="presentation" style="position: relative;">[l,r][l,r][l, r],数据保证每个线段的右端点坐标都是唯一的,问你删去某一条线段后,最大能覆盖多少。


思路:

  设dp[i][0]dp[i][0]dp[i][0]为处理到iii时还没有删去线段的最大覆盖值,dp[i][1]" role="presentation" style="position: relative;">dp[i][1]dp[i][1]dp[i][1]为处理到iii时已经删去的最大值,upper[i]" role="presentation" style="position: relative;">upper[i]upper[i]upper[i]记录右区间比ii<script type="math/tex" id="MathJax-Element-8">i</script>点右区间大的最近的线段的下标。


实现:

#include <bits/stdc++.h>
const int maxn = 100007;
int dp[maxn][2], upper[maxn], n;
struct Node {int l, r;bool operator < (const Node &tmp) const {return l == tmp.l ? r < tmp.r : l < tmp.l;}
} node[maxn];
int calc(int cur, int pre) {return std::max(node[cur].r - node[pre].r, 0) - std::max(node[cur].l - node[pre].r, 0);
}
int main() {scanf("%d", &n);for (int i = 1; i <= n; i++) scanf("%d%d", &node[i].l, &node[i].r);std::sort(node + 1, node + 1 + n);for (int i = 1; i <= n; i++) {if (i < n) {dp[i][0] = dp[i - 1][0] + calc(i, upper[i - 1]);upper[i] = node[upper[i - 1]].r > node[i].r ? upper[i - 1] : i;}if (i > 1) dp[i][1] = std::max(dp[i - 1][1] + calc(i, upper[i - 1]), dp[i - 2][0] + calc(i, upper[i - 2]));}printf("%d\n", std::max(dp[n - 1][0], dp[n][1]));return 0;
}

UPC-5572 - Lifeguards - 动态规划相关推荐

  1. bzoj 5185 Lifeguards - 动态规划 - 贪心

    题目传送门 传送点I 传送点II 题目大意 给定$n$个区间,问恰好删去其中$k$个,剩下的区间的并的最大总长度. 显然被包含的区间一定不优.再加上被包含的区间对计数不友好.直接把它删掉. 注意到题目 ...

  2. UPC第38,39,40场部分题解

    UPC第38,39,40场部分题解 第38场 A. Bovine Alliance Description Bessie and her bovine pals from nearby farms h ...

  3. 伍六七带你学算法 动态规划 ——不同路径

    力扣 62. 不同路径 难度 中等 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格 ...

  4. 由动态规划计算编辑距离引发的思考

    简单介绍 编辑距离算法: https://www.cnblogs.com/BlackStorm/p/5400809.html https://wizardforcel.gitbooks.io/the- ...

  5. LeetCode 10. Regular Expression Matching python特性、动态规划、递归

    前言 本文主要提供三种不同的解法,分别是利用python的特性.动态规划.递归方法解决这个问题 使用python正则属性 import reclass Solution2:# @return a bo ...

  6. 【动态规划】Part1

    1. 硬币找零 题目描述:假设有几种硬币,如1.3.5,并且数量无限.请找出能够组成某个数目的找零所使用最少的硬币数. 分析:   dp [0] = 0            dp [1] = 1 + ...

  7. 2016.4.2 动态规划练习--讲课整理

    1.codevs1742 爬楼梯  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 小明家外面有一个长长的楼梯,共N阶.小明的腿 ...

  8. 算法设计与分析第4章 动态规划(二)【DP序列问题】

    第3章 动态规划(二)[DP序列问题] 3.2 DP序列问题 (51nod的动态规划教程很不错,讲解很详细,以下分析来自51nod) 1.矩阵取数问题 给定一个m行n列的矩阵,矩阵每个元素是一个正整数 ...

  9. 算法设计与分析第4章 动态规划(一)【背包问题】

    第3章动态规划(一)[背包问题] 基本思想: 动态规划算法与分治法类似,其基本思想也是将待求解问题分解成若干个子问题,但是经分解得到的子问题往往不是互相独立的.不同子问题的数目常常只有多项式量级.在用 ...

最新文章

  1. 智能电视验收测试软件,验收测试
  2. spring ioc原理分析
  3. Hash join 和nested loop
  4. SQLSERVER和ORACLE批量处理表名和字段名大写
  5. 快速排序在最坏的情况下时间复杂度(Ω(nlgn)(算法导论第三版9.3-3))
  6. log4j 程序日志_使用log4j监视和筛选应用程序日志到邮件
  7. UML建模【转http://www.cnblogs.com/gaojun/archive/2010/04/27/1721802.html】
  8. 站怎么点都是一样_搞笑段子:都说女人是水做的,你怎么不太一样
  9. 12款响应式 Lightbox(灯箱)效果插件
  10. 【ThreeJS基础教程-高级几何体篇】2.5 加载GLTF/GLB格式文件,Draco压缩文件的获取与加载
  11. 故宫博物馆爬虫(简略版)
  12. linux判断三个数大小程序,几个shell程序设计小知识(shell常识部分)
  13. 导入下载excel(还有excel多个sheet)和txt文本的方法
  14. 小米刷机OTA、 Recovery、 FASTBOOT三种方法直接的区别和联系
  15. 电话薄程序java程序实现
  16. HLOJ 2026 猴子吃桃
  17. STB 应用手册术语 2 - CA,EPG,VOD,CDN
  18. 阿里云ECS服务器安装宝塔BT面板图文教程
  19. 解决使用layui上传文件时提示“请求上传接口出现异常”
  20. 华为电脑如何投屏到电视linux,华为mate10/mate10pro怎么投屏至电视或电脑上面?

热门文章

  1. JS:利用函数,求任意三个数最大值,任意两个数的任意运算结果,判断任意数值是否为素数。
  2. linux报cpu软锁,内核软死锁
  3. 不使用前端的pdf.js,通过pdfbox转换pdf为图片,拼接成html实现pdf前端预览
  4. 数据分析-思维分析逻辑day05
  5. 写给产品经理的第4封信:关于产品经理的十万个为什么?你为什么要做产品经理?
  6. ThreadLocal详解
  7. 备考系统集成项目管理工程师经验
  8. 软件测试基础知识 - 单元测试、集成测试、系统测试、回归测试、验收测试这几步中最重要的是哪一步
  9. 固态硬盘、机械硬盘、手机的“内存”有三种
  10. 基于微信小程序的自习室预约系统设计与实现-计算机毕业设计源码+LW文档