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;
    }
  • 相关阅读:
    redis.conf配置详细解析
    laravel框架的注入
    10 个免费高清图片素材下载网站。#免版权# #设计# #图片处理#
    本地Git连接GitLab(服务器)远程仓库
    基于Docker的Mysql主从复制
    解决git本地代码推服务器每次都要输入用户名和密码的问题
    Laravel上传文件(单文件,多文件)
    php的精度计算问题(bcadd和bcsub)
    POJ 1573 Robot Motion(简单模拟)
    POJ 2996 Help Me with the Game(模拟)
  • 原文地址:https://www.cnblogs.com/TreeDream/p/5348203.html
Copyright © 2011-2022 走看看