题意:一条直线上有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. UVA 1557 - Calendar Game(博弈dp)

    UVA 1557 - Calendar Game 题目链接 题意:给定一个日期,两个人轮流走,每次能够走一月或者一天,问最后谁能走到2001.11.4这个日子 思路:记忆化搜索,对于每一个日期,假设下 ...

  2. [AngularJS] ui-router: Abstract States

    ui-router has the powerful ability to define abstract states, or states that can't be navigated to, ...

  3. ios开发——实用技术篇OC篇&获取设备唯一标识

    获取设备唯一标识 WWDC 2013已经闭幕,IOS7 Beta随即发布,界面之难看无以言表...,简直就是山寨Android. 更让IOS程序猿悲催的是,设备唯一标识的MAC Address在IOS ...

  4. nandsim ubi nand nor

    nandsim模拟mtd测试UBI模块 利用nandsim挂载ubi文件系统 MTD设备及JFFS2, UBIFS文件系统的使用简介 首先需要安装mtd_utils工具: sudo apt-get i ...

  5. org.apache.hadoop.fs-BufferedFSInputStream

    封装了FSInputStream package org.apache.hadoop.fs; import java.io.BufferedInputStream; import java.io.IO ...

  6. cocos2d-x lua 使用http(下载图片, POST JSON)

    cocos2d-x lua 使用http(下载图片, POST JSON) version: cocos2d-x 3.6 1.使用http post json与服务器交互 require(" ...

  7. linux 安装GCC

    研究生阶段已经开始了一段时间了,选了LINUX深入分析,之前没怎么接触过,感觉还是有点难度的.不,好像是很难. 从学校借了一台电脑,安装了UBUNTU12.04的系统,可是不知道怎么地,这个系统里,没 ...

  8. Linux系统上安装软件(JDK以及tomcat服务器)

    一:安装jdk linux系统上面如果运行java程序,就需要安装java的运行环境(jdk) 1:下载linux版本的jdk 地址:http://www.oracle.com/technetwork ...

  9. poj 1987 树的分治

    思路:1741的A1送 1. #include<iostream> #include<cstring> #include<algorithm> #include&l ...

  10. hdu 3622 二分+2-SAT判定

    思路:如题 #include<iostream> #include<algorithm> #include<cstring> #include<cstdio& ...