poj-Monkeys'Pride

描述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.

输入

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.

输出

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.

样例输入

3
2 1
1 2
3 3
3
0 1
1 0
0 0
4
0 0
1 0
0 1
1 1
0

样例输出

1
2
1

 

解题思路:

 

这个题本身不难,但是要好好理解。比如最高点不一定满足point[i].x+point[i].y始终是最大值,即最高的横纵坐标之和不一定相等。比如(5,0),(0,4),这两个点都是最高点。可采用先对x排序,再对y排序的思路。

# include<stdio.h>
# include<algorithm>
using namespace std;

struct E
{
	int x;
	int y;
	bool operator < (const E &b) const
	{
	    if(x<b.x) return true;
		else if(x==b.x)
			return y<b.y;//升序

		return false;
	}
}po[50001];

int main()
{
    int n,i;
	while(scanf("%d",&n)!=EOF&&n)
	{
		int cnt=1;
		int max=-1,maxX=-1,maxY=-1;
		for(i=0;i<n;i++)
		{
			scanf("%d%d",&po[i].x,&po[i].y);
		}
		sort(po,po+n);//升序

		int maxv=po[n-1].y;
        for(i=n-2;i>=0;i--)
		{
		    if(po[i].y>maxv)
			{
				cnt++;
				maxv=po[i].y;
			}
		}

        printf("%d\n",cnt);

	}
	return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值