Holiday Hotel
Time limit:2000 ms |
|
Memory limit:65536 K |
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:
- 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 shoshould not be processed.
Output
For each test case, You shoshould output one line containing an integer, which is the number of all the candidate hotels.
Sample Input
5300 100100 300400 200200 400100 5000
Sample output
2
Source
Beijing 2005
A simple sorting question should be similar after reading the meaning of the question. To understand the two conditions given by the question, one sorting can be done;
The original intention of the question is that a hotel closer to the beach than m is more expensive than m. A hotel farther away from the beach is cheaper than M;
The question can be converted to, when the distance between two hotels is not equal,
So any hotel close to m is more expensive than M. m must be at candidate Hotel,
When the distance between the two hotels is equal, the low price will be retained;
In this way, we can obtain a sorting method;
The following is the code;
# 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) // sort by distance from small to large, and sort by price if the distance is equal {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) {ans = maxn; count = 0; for (I = 0; I <n; I ++) scanf ("% 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 ;}
Algorithms without sorting;
If you save the lowest price from the same hotel, the serial number of the array is the distance. Hotel [distance] = min (price); then, the number of non-incrementing columns is the result.
# 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) {Len = 0; Count = 1; for (I = 0; I <maxn; I ++) map [I] = maxn; for (I = 0; I <n; I ++) {scanf ("% d", & DIS, & cost); If (Map [dis]> Cost) map [dis] = cost; // Save the lowest price 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 ++; // calculates the number of non-incrementing min = map [J] ;}} printf ("% d \ n", count);} return 0 ;}
Poj 2726 Holiday Hotel (SORT)