题意:一条直线上有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. TListView列表拒绝添加重复信息

      //TListView列表拒绝添加重复信息 procedure TForm1.Button1Click(Sender: TObject);var  i: Integer;begin  if (Tr ...

  2. iOS开发——实用篇&KVO与KVC详解

    KVO与KVC详解 由于ObjC主要基于Smalltalk进行设计,因此它有很多类似于Ruby.Python的动态特性,例如动态类型.动态加载.动态绑定等.今天我们着重介绍ObjC中的键值编码(KVC ...

  3. 语法:MySQL中INSERT INTO SELECT的使用(转)

    1. 语法介绍      有三张表a.b.c,现在需要从表b和表c中分别查几个字段的值插入到表a中对应的字段.对于这种情况,可以使用如下的语句来实现: INSERT INTO db1_name (fi ...

  4. android138 360 小火箭

    package com.itheima52.rocket; import android.app.Service; import android.content.Context; import and ...

  5. SQL用法总结

    1.创建数据库语句 create table persons( 'id' INT NOT NULL AUTO_INCREMENT, ) NOT NULL, ) NOT NULL, PRIMARY KE ...

  6. Python学习 之 switch语句

    1.python并没有提供switch语句,python可以通过字典实现switch语句的功能,实现方法分为两步 —首先,定义一个字典 —其次,调用字典的get()获取相应的表达式 通过字典调用函数 ...

  7. B. Little Dima and Equation

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  8. 想学React Native?你只需要一个App!(11月5号更新)

    最近有点空闲时间,顺手研究下react-native,2013年的时候在老师的指导下使用jQuery Mobile做过手机应用,那个运行速度慢呀!让我对WebApp和PhoneGap这一类的跨平台Ap ...

  9. hadoop学习记录(二)HDFS java api

    FSDateinputStream 对象 FileSystem对象中的open()方法返回的是FSDateInputStream对象,改类继承了java.io.DateInoutStream接口.支持 ...

  10. LAMP平台搭建详解

    准备工作 安装编译工具 # yum -y install gcc # yum -y install gcc-c++ 如果系统之前已经安装有rpm包的mysql和apache,那么可以: #servic ...