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、任意酒店如果比M靠海边那么就比M贵
2、任意酒店如果比M便宜,则比M离海边远
- Any hotel which is closer to the seashore than M will be more expensive than M.
- Any hotel which is cheaper than M will be farther away from the seashore than 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;
}