码迷,mamicode.com
首页 > 编程语言 > 详细

排序 POJ 1828

时间:2015-08-31 01:17:34      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:排序   poj1828   

题目:

题目链接

给出N个二维点,要求所有不受控制点的个数,对于<x0,y0> , 如果存在点<x,y> 使得 x>=x0&& y>=y0,这就称为<x0,y0>受控于<x,y>

  • N<5*104;
  • multi_case

解法:

这个题目还算不错,,只允许Nlog(N)的排序预处理和N的遍历。,,求解的时候,最开始想到的是二分,,结果超时。。看样子case还是挺强的~~

  • 先将所有点按照 x_y进行排序。就是先按 x 递增, x相同的再按 y 递增。
  • 去除所有 x 坐标相同的点,只剩下其中 y 值最大的那个点。
  • 然后从右往左遍历,最右边的点肯定不受控,ans++,maxy等于最右边的点的y坐标。考虑第i 个点,如果它的y 大于maxy ,则ans++,并更新maxy,否则继续往前考虑。(自己在稿纸上随便画几个点就很清楚了~~~)

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

struct Node{
  int x;
  int y;
}nodes[5*10000+5];

bool cmp_x_y(const Node &n1, const Node &n2){
     if(n1.x<n2.x) return true;
     else if(n1.x == n2.x && n1.y<n2.y) return true;
     return false;
}

int main()
{
    int N;
    while(1)
    {
        scanf("%d",&N);
        if(!N) break;
        for(int i = 0; i<N; i++){
              scanf("%d%d",&nodes[i].x,&nodes[i].y);
        }
        sort(nodes,nodes+N,cmp_x_y);
        int k = 0;
        for(int i=0; i<N; i++)
        {
            if(i<N-1 && nodes[i].x == nodes[i+1].x)
                 continue;
            else nodes[k++] = nodes[i];
        }
        int ans = 1;
        int maxy = nodes[k-1].y;
        for(int i = k-2; i>=0; i--){
            if(nodes[i].y>maxy){
                ans++;
                maxy = nodes[i].y;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

排序 POJ 1828

标签:排序   poj1828   

原文地址:http://blog.csdn.net/neu_chenguangq/article/details/48113229

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!