zoukankan      html  css  js  c++  java
  • 雷达覆盖,贪心,类似活动安排(POJ1328)

    题目链接:http://poj.org/problem?id=1328

    解题报告:

    1、按照头结点排序。

    #include <cstdio>
    #include <cmath>
    #include <string.h>
    #include <algorithm>
    
    using namespace std;
    
    struct point
    {
        double x;
        double y;
    } dis[1005],pos[1005];
    
    bool cmp(const point a,const point b)
    {
        return a.x<b.x;
    }
    
    int n;
    double d;
    
    int main()
    {
        int Case=0;
        while(scanf("%d%lf",&n,&d),n!=0||d!=0)
        {
            memset(dis,0,sizeof(dis));
            bool flag=true;
    
            for(int i=0; i<n; i++)
            {
                scanf("%lf%lf",&dis[i].x,&dis[i].y);
                if(d<fabs(dis[i].y))
                {
                    flag=false;
                }
            }
    
            for(int i=0; i<n; i++)
            {
                pos[i].x=dis[i].x-sqrt(d*d*1.0-dis[i].y*dis[i].y);///计算一下左右区间
                pos[i].y=dis[i].x*1.0+sqrt(d*d*1.0-dis[i].y*dis[i].y);
            }
    
            if(flag)
            {
                sort(pos,pos+n,cmp);
    
                point tmp=pos[0];
                int ans=1;
    
                for(int i=1; i<n; i++)
                {
                    if(pos[i].x>tmp.y)//没有重合的地方
                    {
                        ans++;
                        tmp=pos[i];
                    }
                    else if(pos[i].y<tmp.y)///有重合的地方且下一个岛屿的右覆盖较远
                        tmp=pos[i];
                }
    
                printf("Case %d: %d
    ",++Case,ans);
            }
            else printf("Case %d: %d
    ",++Case,-1);
        }
        return 0;
    }

    2、按照尾节点排序。

    #include <cstdio>
    #include <cmath>
    #include <string.h>
    #include <algorithm>
    
    using namespace std;
    
    struct point
    {
        double x;
        double y;
    } dis[1005],pos[1005];
    
    bool cmp(const point a,const point b)
    {
        return a.y<b.y;///按尾节点排序
    }
    
    int n;
    double d;
    
    int main()
    {
        int Case=0;
        while(scanf("%d%lf",&n,&d),n!=0||d!=0)
        {
            memset(dis,0,sizeof(dis));
            bool flag=true;
    
            for(int i=0; i<n; i++)
            {
                scanf("%lf%lf",&dis[i].x,&dis[i].y);
                if(d<fabs(dis[i].y))
                {
                    flag=false;
                }
            }
    
            for(int i=0; i<n; i++)
            {
                pos[i].x=dis[i].x-sqrt(d*d*1.0-dis[i].y*dis[i].y);
                pos[i].y=dis[i].x*1.0+sqrt(d*d*1.0-dis[i].y*dis[i].y);
            }
    
            if(flag)
            {
                sort(pos,pos+n,cmp);
    
                point tmp=pos[0];
                int ans=1;
    
                for(int i=1; i<n; i++)
                {
                    if(pos[i].x>tmp.y)///没有重复
                    {
                        ans++;
                        tmp=pos[i];
                    }
                    else if(pos[i].x>tmp.y)
                        tmp=pos[i];
                }
    
                printf("Case %d: %d
    ",++Case,ans);
            }
            else printf("Case %d: %d
    ",++Case,-1);
        }
        return 0;
    }
  • 相关阅读:
    SSH_框架整合2—查询显示
    SSH_框架整合1
    eclipse中tomcat发布失败(Could not delete May be locked by another process)原因及解决办法
    错误:javax.servlet.jsp.PageContext can not be to a type
    链表【6】
    欢迎使用CSDN-markdown编辑器
    剑指offer系列62---两个链表的公共结点
    剑指offer系列61---数组中的逆序对
    Git常用命令大全
    vue脚手架搭建的具体步骤
  • 原文地址:https://www.cnblogs.com/TreeDream/p/5348203.html
Copyright © 2011-2022 走看看