选猴王

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

<span style="font-size: 10px;">3
2 1
1 2
3 3
3
0 1
1 0
0 0
4
0 0
1 0
0 1
1 1
0
</span>

Sample Output

<span style="font-size: 10px;">1
2
1</span>



题目大意:每个猴子都有一个唯一的坐标.如果一个猴子要成为猴王,那么它的坐标要满足其他任意一个猴子的坐标的x,y都不同时大于等于它的x,y.

第一排输入一个整数n表示下面有n个猴子

接下来n排表示n个猴子的坐标.

输出可能为猴王的个数.

]样例:

#include<stdio.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct zb        //声明定义结构体
{
	int  x,y;  //坐标(x,y
}t[100001];
bool cmp(zb p1,zb p2)      //比较坐标
{
	if(p1.x!=p2.x)  //如 果横坐标不想等
	{
		return p1.x<p2.x;       //按照横坐标的升序
	}
		else                   // 如果相等按照y坐标的升序排列
		{
			return p1.y<p2.y;
		}
}
int main()                  //主函数
{
	int n,i;
	while(scanf("%d",&n)!=EOF)     //多组输入.当n==0时结束
	{
		if(n==0)
		{
			return 0;
		}
		for(i=0;i<n;i++)                 
		{
			scanf("%d%d",&t[i].x,&t[i].y);        // 循环输入坐标
		}
		sort(t,t+n,cmp);  //调用排序函数
		struct zb max=t[n-1];    定义结构体变量
		int k=1;
		for(i=n-2;i>=0;i--)
		{
			if(t[i].x<max.x&&t[i].y>max.y)    //如果最大的一个坐标的x,y大于前一个则猴王数加一
			{
				k++;    //k来统计猴王的数量
			max=t[i];    //循环,再从到最大的下一个开始比较
			}
		}
		printf("%d\n",k);  //输出猴王数;
	}return 0;
}
重点:在于对坐标点排序问题,从最大与最大的下一个比较开始循环,利于这样计算猴王数,整个思路只需要一边排序就好了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值