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.
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,y都不同时大于等于它的x,y.
第一排输入一个整数n表示下面有n个猴子
接下来n排表示n个猴子的坐标.
输出可能为猴王的个数.
样例:
#include<stdio.h>
#include<algorithm>
using namespace std;
struct monkey //定义存放猴子坐标的结构体
{
int x;
int y;
}a[99999];
bool com(monkey a,monkey b) //定义sort函数需要的比较函数
{
if(a.x==b.x) //先把x升序,当x相同的时候y升序
return a.y<b.y;
return a.x<b.x;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==0)break;
int i;
for(i=0;i<n;i++)
scanf("%d %d",&a[i].x,&a[i].y);
sort(a,a+n,com); //对猴子坐标排序
int X,Y,sum=1; //最后一个肯定是猴王,sum记录可能为猴王的个数
X=a[n-1].x; //X,Y分别记录最后一个的x,y
Y=a[n-1].y;
for(i=n-1;i>=0;i--)
{
if(a[i].y>Y) //因为后面的猴子的x恒比前面x大,如果前面的猴子y大于当前Y那么这个猴子一定是猴王
{
Y=a[i].y;
sum++;
}
}
printf("%d\n",sum);
}
return 0;
}
这道题难点是在x,y不同时大于等于,那么就有两个变量,我们先可以对其中x升序排序(注意当x相同的时候y也要升序),然后就可以从最大的x降序搜索找y,只要当前的猴子y最大那么,对于已经搜过的猴子没有猴子比当前的猴子y大,因为x排过序,对于没搜的猴子没有一个猴子的x比当前x大.所以当前猴子是猴王.然后整个思路就只需要排一次序,然后遍历一遍就OK了