UVALive 6834 Shopping(開箱拿鑰匙題型)

Question:
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個商店,一個人購物,但他購物有限制必須先去一些商店才能去另外的一些商店,問最短需要走多少路程
解題思路:如果一個商店必須先走的商店比它小就可忽略不計。先按第一個數升序排序,再依次遍歷,把可以合併的區間合併,直至不能合併,則先到這個點後返回最小的那個點
(http://acm.hust.edu.cn/vjudge/contest/130408#problem/C)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN=1005;
struct node
{
    int s,d;
}a[MAXN];
bool cmp(node x,node y)
{
    return x.s<y.s;
}
int main()
{
    int n,m,t;
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0;i<MAXN;i++)
            a[i].d=a[i].s=0;
        int cnt=0,res=0,t=0;
        for(int i=0;i<m;i++)
        {
            int tp1,tp2;
            scanf("%d%d",&tp1,&tp2);
            if(tp1<tp2)        //如果先走小的再走大的就可以忽略
            {
                a[cnt].s=tp1;
                a[cnt++].d=tp2;
            }
        }
        sort(a,a+cnt,cmp);
        int l=a[0].s,r=a[0].d;
        for(int i=1;i<cnt;i++)
        {
            if(a[i].s<=r)
                r=max(r,a[i].d);   //區間合併,有交集的區間合併爲一個
            else
            {
                res+=(r-t+2*(r-l));
                t=r;
                l=a[i].s;
                r=a[i].d;
            }
        }
        res+=(n+1-t+2*(r-l));
        printf("%d\n",res);
    }
}

體會:這道題是以開箱取鑰匙爲原型,還用到了區間合併

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章