0.9poj1828(排序挺好的题)

http://poj.org/problem?id=1828

Monkeys' Pride
Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 7863Accepted: 2549

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

题意:关键的是这句 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. 这句话理解了就ok了。悲催的我完全把题意颠倒黑白了。正确的理解是如果有一个点x0,y0只要不满足x>x0,y>y0同时成立那么x0,y0就是一个王点。。。。
代码1:之前错误理解的,不过这样还是有个问题输入
3
1 0
0 1
0 0结果居然是3。。啊啊呵
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define INF 50010
int a[INF],b[INF];
double sum[INF];
int maxn=-100000;
int main()
{
  int n;
  while(scanf("%d",&n)!=EOF)
  {
    if(n==0)
      break;
    int  i,k=0;
    for(i=0;i<n;i++)
    {
      sum[i]=-INF;
    }
    for(i=0;i<n;i++)
    {
      scanf("%d %d",&a[i],&b[i]);
      sum[i]=((a[i]-0)*(a[i]-0)+(b[i]-0)*(b[i]-0));
    }
    sort(sum,sum+n);
    int count=0;
    for(i=n-1;i>0;i--)
    {
      if(a[i]>a[i-1])
        break;
    }
    printf("%d\n",n-i);
  }
  return 0;
}
 
代码2:AC
#include<iostream>
#include<algorithm>
using namespace std;
int N;
struct Node
{
  int x;
  int y;
}p[50010];
bool cmp(Node a,Node b) //按照x从小到大排序
{
  if(a.x==b.x )//x相同y按照从大到小排序
    return a.y <b.y ;
  else                //x不同按照x从小到大排序
    return a.x <b.x ;
}
int main()
{
  int i,total,temp;
  while(scanf("%d",&N)!=EOF)
  {
    if(N==0)
      break;
    total=1;    //最后一个猴子的x最大,所以至少有一个猴王. 往前扫描,如果出现某个猴子的y大于当前最大y,total+1
    for(i=0;i<N;i++)
      scanf("%d %d",&p[i].x,&p[i].y );
    sort(p,p+N,cmp);//按照x从小到大排序
    temp=p[N-1].y;
    for(i=N-2;i>=0;i--)    //最后一个猴子的x最大,所以至少有一个猴王. 往前扫描,如果出现某个猴子的y大于当前最大y,total+1
    {
      if(p[i].y >temp)//由于x已经按照从小到大排序,所以满足条件的只可能值y值比后面最临近的大的
      {
        total++;
        temp=p[i].y;
     
    }
    printf("%d\n",total);
  }
  return 0;
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值