C语言--Monkey`s Pride

博客介绍了C语言解决猴王选拔问题的算法,关键在于理解题意,寻找有机会成为猴王的猴子(满足特定坐标条件)的个数,通过结构体排序进行判断。AC源代码提供了实现思路,过程中遇到了挑战,但最终成功解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题目大意:讲述 的就是猴王的选拔,这里定义了一个规则,猴子的坐标(x0,y0)不存在任意(x,y)使得==x>x0&&y>y0,则他为KING,记录KING的个数


题目分析:结构体的排序判断


重点及注意事项:1.要理解题目意思不是选择猴王,而是有机会成为猴王的猴子的个数;

                                2.==x>x0&&y>y0,的判断也就是最核心的部分


AC源代码:----旨在参考与交流

#include<stdio.h>
#include<algorithm>     
using namespace std;  
struct point  
{  
    int x;  
    int y;  
}
monkey[50001];
bool cmp(point a,point b)  
{  
    if(a.x==b.x)  
		return a.y<b.y;  
    return a.x<b.x;  //对坐标点按X升序排列
}
int main()  
{
    int n;  
    while(scanf("%d",&n)!=EOF&&n!=0)  
    {
        
        int i;
        for(i=0;i<n;i++)
            scanf("%d%d",&monkey[i].x,&monkey[i].y);   
        sort(monkey,monkey+n,cmp);
        int kingX=monkey[n-1].x;  //将最大X付给kingX(必为猴王候选人) 
        int kingY=monkey[n-1].y;  //因为X以最大所以只有Y>kingY才有可能是猴王
        int sum=1;//注意sum的初值
        for(i=n-1;i>=0;i--)
		{
			if(monkey[i].x==kingX)//************
				continue;
			if(monkey[i].x<kingX)
			{
				kingX=monkey[i].x;
				if(monkey[i].y>kingY)
				{
					sum++;
					kingY=monkey[i].y;//最核心的部分,猴王判断条件
				}
			}
		}
        printf("%d\n",sum); 
    }  
    return 0;  
}  



运行结果如下


心得:当然这道题deAC并不像博客里写的哪门一帆风顺,学到了就主要的就是如何分析数据找到有效可行的方法。

                                                                                                                                                                          --------------------------------- AC终将上演


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值