poj 2726Holiday Hotel

Holiday Hotel
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 8013 Accepted: 3139

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.
史密斯先生和太太都去海边度假。在去之前,他们需要选择一家酒店。他们在互联网上列出了酒店名单,并希望选择一些酒店,价格便宜,靠近海边。候选酒店M满足两个要求: 
1、任意酒店如果比M靠海边那么就比M贵
2、任意酒店如果比M便宜,则比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.
有多个测试用例。每个测试用例的第一行是一个整数N(1<= N <= 10000),它是宾馆的​​数量。接下来N行的每一行描述了一个酒店,包含两个整数D和C(1<= D,C<=10000)。 D指从酒店到海边的距离,C表示住在酒店的费用。你可以假设,没有两间酒店,同样的D和C。一个测试用例,N=0结束输入,并且不应该被处理。

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

额,本来想试一下基数排序,找了这么道题,还是感受不到基数排序。。。直接二维快排后,找d比前面都小的累加输出就行了。。。忘记删除测试数据了,wa了一次。。。
#include <iostream>
#include<algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>

#define MAX_NUM 11111
using namespace std;

struct Hotel{
    int dis;
    int cos;
}hotel[MAX_NUM];

void qsort(int p, int r);
int partition_q(int p, int r);


int main()
{
    int N, i;
    int mind, ans;
    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);

    while(scanf("%d", &N) && N != 0){
        for (i = 0;i < N;++ i){
            scanf("%d%d", &hotel[i].dis, &hotel[i].cos);
        }
        qsort(0, N - 1);
//        for (i = 0;i < N;++ i){
//            printf("%d %d\n", hotel[i].dis, hotel[i].cos);
//        }
        mind = 100000000;
        ans = 0;
        for (i = 0;i < N;++ i){
            if (hotel[i].dis < mind){
                ans ++;
                mind = hotel[i].dis;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

void qsort(int p, int r){
    int q;
    if (p < r){
        q = partition_q(p, r);
        qsort(p, q - 1);
        qsort(q + 1, r);
    }
}
int partition_q(int p, int r){
    int i = p - 1;
    int j;
    struct Hotel temp;
    for (j = p; j < r;++ j){
        if (hotel[j].cos < hotel[r].cos){
            i ++;
            temp = hotel[j]; hotel[j] = hotel[i];hotel[i] = temp;
        }else if (hotel[j].cos == hotel[r].cos){
            if (hotel[j].dis < hotel[r].dis){
                i ++;
                temp = hotel[j]; hotel[j] = hotel[i];hotel[i] = temp;
            }
        }
    }
    temp = hotel[i + 1]; hotel[i + 1] = hotel[r]; hotel[r] = temp;
    return i + 1;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值