POJ 1828 Monkeys' Pride

14 篇文章 0 订阅
Monkeys' Pride
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 8640 Accepted: 2854

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

Source


说好的DP,DP的思路没有想出来,用的其他思路,用的set,超时了很多次,做了点优化卡过去了。原来有其他更简单的思路先按x从小到大进行排序,x相同的按y 从小到大进行排序,然后倒着查找,维护一个y的最大值即可,大于当前最大值的+1,并更新
这里贴一下我用set写的代码
#include <iostream>
#include <set>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#define N 50010
using namespace std;
struct num
{
    int x,y;
    num(int h,int w):x(h),y(w){}
    num(){}
    bool operator <(num p1) const
    {
        if(y!=p1.y)
        {
            return y<p1.y;
        }else
        {
            return x<p1.x;
        }
    }
}a[N],b[N];
set<num>se;
bool cmp(num p1,num p2)
{
    if(p1.x!=p2.x)
    {
        return p1.x<p2.x;
    }else
    {
        return p1.y>p2.y;
    }
}
int main()
{
    //freopen("data.txt","r",stdin);
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
        {
            break;
        }
        for(int i=1;i<=n;i++)
        {
            scanf("%d %d",&a[i].x,&a[i].y);
        }
        sort(a+1,a+n+1,cmp);
        b[1].x = a[1].x;
        b[1].y = a[1].y;
        int Top = 2;
        for(int i=2;i<=n;i++)
        {
            if(a[i].x!=a[i-1].x)
            {
                b[Top].x = a[i].x;
                b[Top++].y = a[i].y;
            }
        }
        n = Top-1;
        se.clear();
        for(int i=1;i<=n;i++)
        {
            a[i].x = b[i].x;
            a[i].y = b[i].y;
            se.insert(num(a[i].x,a[i].y));
        }
        int res = 0;
        for(int i=1;i<=n;i++)
        {
            int x = a[i].x;
            int y = a[i].y;
            set<num>::iterator it = se.find(num(x,y));
            se.erase(it);
            it = se.upper_bound(num(x,y));
            if(it==se.end())
            {
                res++;
            }
        }
        printf("%d\n",res);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值