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 many years. 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
3 2 1 1 2 3 3 3 0 1 1 0 0 0 4 0 0 1 0 0 1 1 1 0
Sample Output
1 2 1 题目大意:求猴王的坐标。。。什么猴王的坐标不能同时被其他猴子超过。,。问有多少个猴王。。。解题思路:按X排序,然后用两个变量分别记录X和Y。。然后向下循环。。。。
1 #include<stdio.h> 2 #include<algorithm> 3 using namespace std; 4 struct monkey{ 5 int x; 6 int y; 7 }a[99999]; 8 bool com(monkey a,monkey b) 9 { 10 if(a.x==b.x) 11 return a.y<b.y; 12 return a.x<b.x; 13 } 14 int main() 15 { 16 int n; 17 while(scanf("%d",&n)!=EOF) 18 { 19 if(n==0)break; 20 int i; 21 for(i=0;i<n;i++) 22 { 23 scanf("%d %d",&a[i].x,&a[i].y); 24 } 25 sort(a,a+n,com); 26 int X,Y,sum=1; 27 X=a[n-1].x; 28 Y=a[n-1].y; 29 for(i=n-1;i>=0;i--) 30 { 31 if(a[i].x==X) 32 continue; 33 else 34 { 35 if(a[i].y>Y) 36 { 37 Y=a[i].y; 38 sum++; 39 } 40 } 41 } 42 printf("%d\n",sum); 43 } 44 return 0; 45 }
POJ 2897 Monkeys' Pride 解题报告