Holiday Hotel
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 8204 | Accepted: 3211 |
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:
- 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.
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-------------------------------------------------这道题折腾了一个下午,最后还是看着网上的代码才弄明白的。二元组(D,C)作为一个候选者(candidate)的条件为:对任意不为(D,C)的二元组(D`,C`),存在以下不等式:1)若D`>D,则C`>C2)若C`>C,则D‘>D我们先按D排序,对于Di观察其对应的Ci与Ci-1,...,C1之间的关系,可以得出:当Ci为当前出现过的最小C时,满足候选者条件。因此,问题最后转化为排序后对最小值的判断,问题解决。然而,我最初的C语言版本还是WA,不知道哪里出了问题,不过C++版本是可以用的。
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> using namespace std; #define maxn 10005 struct Hotel { int c, d; } f[maxn]; int n, ans; bool operator <(const Hotel &a, const Hotel &b) { if (a.c == b.c) return a.d < b.d; return a.c < b.c; } void input() { for (int i = 0; i < n; i++) scanf("%d%d", &f[i].c, &f[i].d); } void work() { ans = 0; int maxd = 100000000; for (int i = 0; i < n; i++) if (f[i].d < maxd) { ans++; maxd = f[i].d; } } int main() { //freopen("D:\\t.txt", "r", stdin); while (scanf("%d", &n) != EOF && n != 0) { input(); sort(f, f + n); work(); printf("%d\n", ans); } return 0; }