poj 1828 Monkey's Pride

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int n;
struct a{
	int x,y;
};
bool operator < (const a & c,const a& b){
		if(c.x == b.x)
			return c.y < b.y;
		return c.x < b.x;
	}
a monkey[50010];
int main(){
	int ans = 0;
	while(scanf("%d",&n)&&n){
		ans = 0;
		memset(monkey,0,sizeof(monkey));
		for(int i = 1; i <= n; ++i){
			scanf("%d%d",&monkey[i].x,&monkey[i].y);
		}
		sort(monkey+1,monkey+n+1);
		if(n == 1){
			printf("1\n");
			continue;
		}
		else{
			ans = 1; //最后一点一定是
			int j = n - 1; 
			int tempy = monkey[n].y;
			while(j > 0){
				if(monkey[j].y >tempy){
					++ans;
					tempy = monkey[j].y;
				}
				--j;
			}
			printf("%d\n",ans);
		}
	}
	return 0;
}
	


主要算法:

排序;

因为是按先x后y排序,所以从后向前找坐标(前面的点x一定小于后面,

所以只有前面的点的y坐标比后面的大时,才可以++ans),同时这样的查找方法保证了tempy是递增的~

即只要比tempy大,那么比后面所有点的y都大~

注:g++编译器中对小于号的重载要写在类的外面~否则会ce

      原因还不太清楚~求指点

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值