题目传送门

 /*
题意:有n个商店排成一条直线,有一些商店有先后顺序,问从0出发走到n+1最少的步数
贪心:对于区间被覆盖的点只进行一次计算,还有那些要往回走的区间步数*2,再加上原来最少要走n+1步就是答案了
详细解释:http://blog.csdn.net/u013625492/article/details/45640735
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std; const int MAXN = 1e3 + ;
const int INF = 0x3f3f3f3f;
struct A
{
int l, r;
}a[MAXN];
struct B
{
int l, r;
}b[MAXN]; bool cmp(A x, A y) {return x.l < y.l;} int main(void) //UVALive 6834 Shopping
{
// freopen ("C.in", "r", stdin); int n, m;
while (scanf ("%d%d", &n, &m) == )
{
int cnt = ;
for (int i=; i<=m; ++i)
{
int u, v; scanf ("%d%d", &u, &v);
if (u > v) continue;
a[++cnt].l = u; a[cnt].r = v;
}
sort (a+, a++cnt, cmp); int l = a[].l; int r = a[].r; int tot = ;
for (int i=; i<=cnt; ++i) //区间覆盖
{
if (r < a[i].l)
{
b[++tot].l = l; b[tot].r = r;
l = a[i].l; r = a[i].r;
}
else r = max (r, a[i].r);
if (i == cnt) {b[++tot].l = l; b[tot].r = r;}
} int ans = n + ;
for (int i=; i<=tot; ++i) ans += * (b[i].r - b[i].l);
printf ("%d\n", ans);
} return ;
}

最新文章

  1. 详解Javascript的继承实现
  2. Spring学习系列(二) 自动化装配Bean
  3. Java 基础【07】线程同步锁的选择
  4. swift uiview弹出动画
  5. form表单类标签汇总
  6. ionic 嵌套view 的方法
  7. codeforces 100548F (西安现场赛F题):容斥原理
  8. juce中的BailOutChecker
  9. 添加zabbix自动发现(监控多tomcat实例)
  10. 在线教育平台搭建 预览和models
  11. Javaoop 遇到的问题
  12. [UE4]显示落地箭头
  13. Web前端学习笔记之离线安装npm
  14. animation3 背景小动画笔记
  15. Maven项目META-INF文件夹不存在的问题
  16. JQuery漂浮广告代码
  17. 有关string stringbuff stringbuild 的区别
  18. 004_ssh连接慢的问题的解决?
  19. Luogu 2831 [NOIP2016] 愤怒的小鸟
  20. 线程池ThreadPool实现异步多线程

热门文章

  1. 【网络】TCP的流量控制
  2. unix时间戳(unix timestamp)与北京时间的互转方法
  3. 1 TypeScript 简介与安装
  4. 配置server禁止全部非法域名 訪问自己的server
  5. 【iOS系列】-oc中特有的语法
  6. 【网站支付PHP篇】thinkPHP集成支付宝支付(担保交易)
  7. Eclipse 变量点击高亮显示以及自己定义高亮显示颜色
  8. Datatables 1.10.x在命名上与1.9.x
  9. (21) java web的struts2框架的使用
  10. 【Android进度条】三种方式实现自定义圆形进度条ProgressBar