题意:一条直线上有n个点,标号从1到n,起点为0,终点为N+1,给定m个限制关系(ci,di),访问ci点前必须先访问di点,每两个点之间是单位距离,求在限制条件下,从起点到终点访问完所有的点的最短距离。

分析:画图模拟一下可知,从起点到终点的N+1这段距离是必须的,若想距离最短,必须去重。

   比如以下样例,

10 3
3 7
8 9
2 5
试模拟一下按部就班的访问,从0开始,先访问5,再访问2,->7->3->9->8>11,最后结束,因为要满足限制条件,会重走一些路,有些重走是不可避免的,比如限制条件8 9,为了先访问9再访问8,必须将8~9这一段走2遍,但是限制条件2 5和3 7则不然,不必要将2~5和3~7都重走2遍,画图显然可知,只需将2~7重走2遍就可同时满足这两个限制条件,也就是说,如果两个限制条件走过的路有交叉(可能是包含)的话,那么取并集即可。

 #include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, , -, };
const int dc[] = {-, , , };
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
struct P
{
int x, y;
bool operator < (const P& a)const
{
return x < a.x || (x == a.x && y < a.y);
}
};
int main()
{
int N, m;
while(scanf("%d%d", &N, &m) == )
{
if(m == )
{
printf("%d\n", N + );
continue;
}
int ans = N + ;
P num[];
for(int i = ; i < m; ++i)
scanf("%d%d", &num[i].x, &num[i].y);
sort(num, num + m);
int s = num[].x;
int e = num[].y;
for(int i = ; i < m; ++i)
{
if(num[i].x > num[i].y) continue;
if(num[i].x >= s && num[i].y <= e) continue;
else if(num[i].x >= s && num[i].x <= e && num[i].y >= e)
e = num[i].y;
else
{
ans += * (e - s);
s = num[i].x;
e = num[i].y;
}
}
ans += * (e - s);
printf("%d\n", ans);
}
return ;
}
 

UVaLive6834 Shopping的更多相关文章

  1. Shopping(山东省第一届ACM省赛)

    Shopping Time Limit: 1000MS Memory limit: 65536K 题目描述 Saya and Kudo go shopping together.You can ass ...

  2. sdutoj 2154 Shopping

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2154 Shopping Time Limit: ...

  3. Shopping(SPFA+DFS HDU3768)

    Shopping Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  4. eclipse中 起动tomcat时报Multiple Contexts have a path of "/shopping"

    eclipse中 启动tomcat时报Multiple Contexts have a path of "/shopping". 这个是由于你的server服务器中的server. ...

  5. 洛谷P2732 商店购物 Shopping Offers

    P2732 商店购物 Shopping Offers 23通过 41提交 题目提供者该用户不存在 标签USACO 难度提高+/省选- 提交  讨论  题解 最新讨论 暂时没有讨论 题目背景 在商店中, ...

  6. UVALive - 6572 Shopping Malls floyd

    题目链接: http://acm.hust.edu.cn/vjudge/problem/48416 Shopping Malls Time Limit: 3000MS 问题描述 We want to ...

  7. Codeforces Gym 100803C Shopping 贪心

    Shopping 题目连接: http://codeforces.com/gym/100803/attachments Description Your friend will enjoy shopp ...

  8. Codeforces Round #332 (Div. 2) A. Patrick and Shopping 水题

    A. Patrick and Shopping Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

  9. poj 1170 Shopping Offers

    Shopping Offers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4696   Accepted: 1967 D ...

随机推荐

  1. Oracle 生成随机密码

    需求:需要定期更改密码.要求是1.密码位数11位.2.必须包含大小写字母.数字.特殊字符.3.排除一些特殊字符如().@.& oracle数据库中有可已生成随机密码包dbms_random,但 ...

  2. 解决IE下jquery ajax无法获得最新数据的问题(IE缓存)

    今天修改一个bug,利用ajax查询数据,在谷歌浏览器下可以获取到最新数据,而在IE中获得是旧数据,无法获得最新的数据,经查资料,才发现时IE缓存再作怪. 发现此ajax请求用的get方式,每次请求的 ...

  3. 让你的PHP程序真正的实现多线程(PHP多线程类)(转)

    通过WEB服务器来实现PHP多线程功能. 当然,对多线程有深入理解的人都知道通过WEB服务器实现的多线程只能模仿多线程的一些效果,并不是真正意义上的多线程. 但不管怎么样,它还是能满足我们的一些需要的 ...

  4. ASP.NET获取URL

    //获取完整url (协议名+域名+站点名+文件名+参数) string fullUrl = Request.Url.ToString(); //获取客户端请求的URL信息(不包括主机和端口) str ...

  5. 纯JS操作服务器绑定控件(Repeat)实现表头升降排序

    JS实现功能 var obj = function (id) { return "string" == typeof id ? document.getElementById(id ...

  6. TextFiled 中输入金额

    要求: 输入的金额不能超过六位, 小数点后面只能输入两位小数 如果 textFIled  中第一位输入的是0 ,后面必须输入小数点,否则禁止输入 用到 textfiled代理方法 #pragma ma ...

  7. 开发技巧01——改变Toast显示位置

    1.获得Toast对象——Toast toast = Toast.makeText(this, "Top Left!", Toast.LENGTH_SHORT); 2.Toast对 ...

  8. Hibernate的几种主键生成策略

    主键类型: 业务主键(natural key):业务主键的值是来源于一个业务数据. 代理主键(surrogate key):代理主键需要采用一种方式来生成某个唯一值. 代理主键的生成策略: 1.hib ...

  9. [改善Java代码]推荐在复杂字符串操作中使用正则表达式

    一.分析  字符串的操作,诸如追加.合并.替换.倒序.分隔等,都是在编码过程中经常用到的,而且Java也提供了append.replace.reverse.split等方法来完成这些操作,它们使用起来 ...

  10. hdu 4619 最大匹配问题

    思路:把所有涉及到的点按(x+y)的奇偶分成两部分点,对所有的1*2的骨牌,都有(x+y)为偶数的建到奇数的边.求一次最大匹配,就是答案. #include<iostream> #incl ...