题意:给定 n 个商店,然后有 m个限制,去 c 之前必须先去d,问你从0到n+1,最短路程是多少。

析:我们我们要到c,必须要先到d,那么举个例子,2 5, 3 7,如果我们先到5再到2,再到7再到3,那么3-5这个区间我们走了4次,如果我们先到7再到2,

那么就只走了3次,这很明显是最优的,所以我们把能合并的区间都合并起来,然后再一块计算。

代码如下:

#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>
#define debug puts("+++++")
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 2e5 + 5;
const LL mod = 1e9 + 7;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
inline int gcd(int a, int b){ return b == 0 ? a : gcd(b, a%b); }
inline int lcm(int a, int b){ return a * b / gcd(a, b); }
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 int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
struct Node{
int l, r;
bool operator < (const Node &p) const{
return l < p.l || (l == p.l && r < p.r);
}
};
Node a[505]; int main(){
while(scanf("%d %d", &n, &m) == 2){
for(int i = 0; i < m; ++i) scanf("%d %d", &a[i].l, &a[i].r);
if(!m){ printf("%d\n", n+1); continue; }
sort(a, a+m);
int l = a[0].l, r = a[0].r;
int ans = l;
for(int i = 1; i < m; ++i){
if(a[i].l <= r) r = Max(r, a[i].r);
else{ ans += 3 * (r-l) + a[i].l - r; l = a[i].l, r = a[i].r; }
}
ans += 3 * (r-l) + n - r + 1;
printf("%d\n", ans);
}
return 0;
}

最新文章

  1. WCF分布式开发必备知识(2):.Net Remoting
  2. nyoj19 全排列
  3. bzoj2038
  4. jQuery中$.each的用法
  5. DataGridView的Validating事件注册后删除操作的处理
  6. THUSC2015
  7. IOS开发-Swift新语言初见
  8. Linux启动新进程的几种方法及比较[转]
  9. c++ 自动应用类型转换
  10. 详解 anjularjs的ui-route(多视图、视图嵌套、视图传参)
  11. 媲美jQuery的JS框架——AngularJS(一)
  12. 张高兴的 UWP 开发笔记:应用内启动应用 (UWP Launch UWP)
  13. Azure Web连接到Azure MySql Db
  14. SQLserver查询库中包含某个字段的表
  15. 在Debian9服务器上安装最新版Python
  16. (python基础 函数)
  17. swift 实践- 10 -- UIProgressView
  18. 编译Uboot——错误记录
  19. 在JS文件中,不需要&lt;script&gt;标签
  20. Django之模型层-了解ORM

热门文章

  1. 51NOD 2368 珂朵莉的旅行
  2. 杭电 1009 FatMouse&#39; Trade (贪心)
  3. poj2325 大数除法+贪心
  4. java 通过反射机制调用某个类的方法
  5. *lucene索引_创建_域选项
  6. FZU Problem 2132 LQX的作业 (数学题)
  7. 18.10.7 POIN 模拟赛
  8. operamasks—omMessageBox的使用
  9. 采用jmeter和泛化测试dubbo服务接口
  10. uva 1411 Ants (权值和最小的完美匹配---KM算法)