Description
Background There are a lot of monkeys in a mountain. every one wants to be the monkey king. they keep arguing with each other about that for policyears. it is your task to help them solve this problem.
Problem Monkeys live in different places of the mountain. let a point (x, y) in the X-Y plane denote the location where a monkey lives. there are no two monkeys living at the same point. if a monkey lives at the point (x0, y0), he can be the king only if there is no monkey living at such point (x, y) that x> = x0 and y> = y0.for example, there are three monkeys in the mountain: (2, 1), (1, 2), (3, 3 ). only the monkey that lives at the point (3, 3) can be the king. in most cases, there are a lot of possible kings. your task is to find out all of them.
Input
The input consists of several test cases. in the first line of each test case, there are one positive integers N (1 <=n <= 50000), indicating the number of monkeys in the mountain. then there are N pairs of integers in the following N lines indicating the locations of N monkeys, one pair per line. two integers are separated by one blank. in a point (x, y), the values of x and y both lie in the range of signed 32-bit integer. the test case starting with one zero is the final test case and has no output.
Output
For each test case, print your answer, the total number of the monkeys that can be possible the king, in one line without any redundant spaces.
Sample Input
32 11 23 330 11 00 040 01 00 11 10
Sample Output
121
Coordinate (x0, y0) does not exist any (x, y) so that =, "x> x0 & y> y0, then it is KING and records the number of KING;
Code 1 (Time Limit Exceeded ):
1 # include <stdio. h> 2 struct position // record the monkey's position 3 {4 int x, y; 5} moky [50010]; 6 int main () 7 {8 int N; 9 while (scanf ("% d", & N )! = EOF) {10 if (N = 0) 11 break; 12 int I, king = 0, j, flag_x, flag_y; 13 for (I = 0; I <N; I ++) 14 scanf ("% d", & moky [I]. x, & moky [I]. y); 15 for (I = 0; I <N; I ++) {16 for (j = 0; j <N; j ++) {17 if (I = j) 18 continue; 19 flag_x = 1, flag_y = 1; // Mark whether Monkey coordinates meet the requirement 20 if (moky [I]. x <= moky [j]. x) 21 flag_x = 0; 22 if (moky [I]. y <= moky [j]. y) 23 flag_y = 0; 24 if (flag_x = 0 & flag_y = 0) 25 break; 26} 27 if (flag_x = 1 | flag_y = 1) // if there is no coordinate greater than or equal to it, it can be KING28 king ++; 29} 30 printf ("% d \ n", king); 31} 32 return 0; 33}
Lab data:
Source code 2: (Wrong answer ):
# Include <stdio. h ># include <algorithm> using namespace std; struct monkey // record monkey coordinates {int x, y;} moky [50000]; bool cmp (monkey a, monkey B) // sort the monkey coordinates {if (. x! = B. x) return. x> B. x; return. y> B. y;} int comp (monkey a, monkey B) // determines whether the condition of monkey King is met. {if (. x> = B. x &. y> = B. y) return 0; else return 1;} int main () {int N; while (scanf ("% d", & N )! = EOF) {if (N = 0) return 0; int I, king = 0; for (I = 0; I <N; I ++) scanf ("% d", & moky [I]. x, & moky [I]. y); sort (moky, moky + N, cmp); // sort the monkey coordinates for (I = 0; I <N; I ++) {if (I = 0) king ++; else if (comp (moky [0], moky [I]) = 1) // Determine whether the Monkey king's requirements are met. king ++; else break;} printf ("% d \ n", king);} return 0 ;}
Test data screenshot:
Monkey king