6834 Shopping ( 贪心 )

欢迎访问https://blog.csdn.net/lxt_Lucia~~

宇宙第一小仙女\(^o^)/~~萌量爆表求带飞=≡Σ((( つ^o^)つ~ dalao们点个关注呗~~

 

----------------------------------------我只是一条可爱哒分界线-------------------------------------------

 

一、问题:

Description

       Your friend will enjoy shopping. She will walk through a mall along a straight street, where N individual shops (numbered from 1 to N) are aligned at regular intervals. Each shop has one door and is located at the one side of the street. The distances between the doors of the adjacent shops are the same length, i.e. a unit length. Starting shopping at the entrance of the mall, she visits shops in order to purchase goods. She has to go to the exit of the mall after shopping.

       She requires some restrictions on visiting order of shops. Each of the restrictions indicates that she shall visit a shop before visiting another shop. For example, when she wants to buy a nice dress before choosing heels, she shall visit a boutique before visiting a shoe store. When the boutique is farther than the shoe store, she must pass the shoe store before visiting the boutique, and go back to the shoe store after visiting the boutique.

       If only the order of the visiting shops satisfies all the restrictions, she can visit other shops in any order she likes.

      Write a program to determine the minimum required walking length for her to move from the entrance to the exit.

      Assume that the position of the door of the shop numbered k is k units far from the entrance, where the position of the exit is N + 1 units far from the entrance.

 

Input

The input file contains several test cases, each of them as described below.

        Each case input consists of a set of lines.

N m

c1 d1

....

....

....

cm dm

        The first line contains two integers N and m, where N (1 ≤ N ≤ 1000) is the number of shops, and m (0 ≤ m ≤ 500) is the number of restrictions. Each of the next m lines contains two integers ci and di (1 ≤ ci < di ≤ N) indicating the i-th restriction on the visiting order, where she must visit the shop numbered ci after she visits the shop numbered di (i = 1, . . . , m).

        There are no pair of j and k that satisfy cj = ck and dj = dk.

 

Output

For each test case, output the minimum required walking length for her to move from the entrance to the exit. You should omit the length of her walk in the insides of shops.

 

Sample Input

10 3

3 7

8 9

2 5

10 3

8 9

6 7

2 4

10 0

10 6

6 7

4 5

2 5

6 9

3 5

6 8

1000 8

3 4

6 1000

5 1000

7 1000

8 1000

4 1000

9 1000

1 2

 

Sample Output

23

19

11

23

2997

 

二、题意:

街上有 n 个店,有个人穿过整条街买东西,但其中有些前提条件,比如C1 , D1,必须先去 D1 买完才能去 C1 买,求最短总路程。(注意是从第一个店开始,即入口 = 第一个店,而最后一个店到出口距离为 1 ,即出口 = 最后一个店 + 1 )。

 

三、思路:

可以直接穿过整条街,按左端点从小到大排序,再把重复的合并起来的区间加上。

注意区间可以有两种处理,一种是把区间合并起来走,另一种是各走各的来回跑,显然只要两区间之间有交集,则第一种总路程短些,所以先把能合并的区间合并,剩下的区间单算。(注意如果没有限制条件的话,就直接穿过整条街即可)

 

四、代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define fori(a,b) for(int i=a;i<=b;i++)
using namespace std;

struct A
{
    int x,y;
}a[510];

bool cmp(A p, A q)
{
    if(p.x != q.x)
        return p.x < q.x;
    else
        return p.y < q.y;
}

int main()
{
    int n, m;
    while(~scanf("%d %d", &n, &m))
    {
        int ans = n+1;
        mem(a, 0);
        fori(0,m-1)
            scanf("%d %d", &a[i].x, &a[i].y);
        if(m==0)
        {
            printf("%d\n",n+1);
            continue;
        }
        sort(a, a+m, cmp);
        int left = a[0].x, right = a[0].y;
        fori(1,m-1)
        {
            if(right < a[i].x)
            {
                ans += 2*(right - left);
                left = a[i].x;
                right = a[i].y;
            }
            else if(right < a[i].y)
                right = a[i].y;
        }
        ans += 2*(right - left);
        printf("%d\n", ans);
    }
    return 0;
}

 

------------------------------------------我也是有底线的----------------------------------------------------

 

欢迎访问https://blog.csdn.net/lxt_Lucia~~

宇宙第一小仙女\(^o^)/~~萌量爆表求带飞=≡Σ((( つ^o^)つ~ dalao们点个关注呗~~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值