首页 > 代码库 > poj 2726 Holiday Hotel (排序)

poj 2726 Holiday Hotel (排序)

Holiday Hotel
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 8098 Accepted: 3176

Description

Mr. and Mrs. Smith are going to the seaside for their holiday. Before they start off, they need to choose a hotel. They got a list of hotels from the Internet, and want to choose some candidate hotels which are cheap and close to the seashore. A candidate hotel M meets two requirements: 
  1. Any hotel which is closer to the seashore than M will be more expensive than M. 
  2. Any hotel which is cheaper than M will be farther away from the seashore than M.

Input

There are several test cases. The first line of each test case is an integer N (1 <= N <= 10000), which is the number of hotels. Each of the following N lines describes a hotel, containing two integers D and C (1 <= D, C <= 10000). D means the distance from the hotel to the seashore, and C means the cost of staying in the hotel. You can assume that there are no two hotels with the same D and C. A test case with N = 0 ends the input, and should not be processed.

Output

For each test case, you should output one line containing an integer, which is the number of all the candidate hotels.

Sample Input

5
300 100
100 300
400 200
200 400
100 500
0

Sample Output

2

Source

Beijing 2005

一个简单的排序题,读懂题意之后应该就差不多了,要理解题目给定的两个条件,一个排序就可以做出来;
题目的原意是,比m离海边更近的旅馆,价钱比m更贵,比m离海边更远的旅馆,价钱比m便宜;
题目意思可以转化为,两个旅馆距离不相等时,
那么任意比m距离近的旅馆都比m的价格贵的m 一定是candidate hotel 的,
两个旅馆距离相等的时候,就保留价格低的那个;
这样我们就能够得出一次排序的排序方法;
下面是代码;
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=10001;
struct edge
{
    int dis,cost;
}map[maxn];
bool cmp(edge x,edge y)//按照距离从小到大排序,距离相等的按照价格排序
{
    if(x.dis == y.dis){
<span style="white-space:pre">		</span>return x.cost < y.cost;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>return x.dis < y.dis;
}
int main()
{
    int n,i,ans,count;
    while(scanf("%d",&n)&&n)
    {
        ans=maxn;
        count=0;
        for(i=0;i<n;i++)
            scanf("%d%d",&map[i].dis,&map[i].cost);
        sort(map,map+n,cmp);
        for(i=0;i<n;i++)
        {
            if(map[i].cost<ans)
            {
                count++;
                ans=map[i].cost;
            }
        }
        printf("%d\n",count);
    }
    return 0;
}
不用排序的算法;
只要将距离相同的hotel中价格最低的存下来,数组的序号就是距离。
hotel[distance]=min(price);再统计数列中非递增的个数就是结果。
#include <cstdio>
#include <cstring>
const int maxn=10001;
int map[maxn];
int main()
{
    int n,i,dis,cost,len,j,count,min;
    while(scanf("%d",&n)&&n)
    {
        len=0;
        count=1;
        for(i=0;i<maxn;i++)
            map[i]=maxn;
        for(i=0;i<n;i++)
        {
            scanf("%d%d",&dis,&cost);
            if(map[dis]>cost) map[dis]=cost;//保存最低的价格
            if(len<dis) len=dis;
        }
        i=0;
        while(map[i]==maxn) i++;
        min=map[i];
        for(j=i+1;j<=len;j++)
        {
            if(map[j]<min)
            {
                count++;//求非递增的个数
                min=map[j];
            }
        }
        printf("%d\n",count);
    }
    return 0;
}




poj 2726 Holiday Hotel (排序)