0.9poj1828(排序挺好的题)

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

Monkeys' Pride
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 7863 Accepted: 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

Atlas of rruucc@POJ
题意:关键的是这句 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
#include
#include
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   {
   sum[i]=-INF;
  }
  for(i=0;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
#include
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  else         //x不同按照x从小到大排序
  return a.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    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;
}
 

你可能感兴趣的:(排序)