zoukankan      html  css  js  c++  java
  • 寒假Day33:简单递归+简单贪心(区间相交问题变形)

    Not so Mobile

     UVA - 839 

    样例:

    Sample Input
    1
    0 2 0 4
    0 3 0 1
    1 1 1 1
    2 4 4 2
    1 6 3 2
    
    Sample Output
    YES

    思路:简单递归,判断两边是否平衡即可

    AC代码:

    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<queue>
    using namespace std;
    
    int w()
    {
        int w1,d1,w2,d2;
        //判断w1、w2为0的情况进行判断
        cin>>w1>>d1>>w2>>d2;
        if(!w1)
            w1=w();
        if(!w2)
            w2=w();
        if(w1*d1!=w2*d2)
            return -1;
        return w1+w2;
    }
    
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            int ans=w();
            if(ans==-1)
                cout<<"NO"<<endl;
            else
                cout<<"YES"<<endl;
            if(t)
                cout<<endl;
        }
        return 0;
    }

    Saruman's Army

     POJ - 3069 

    思路:贪心,判断的时候不能利用区间相交来进行判断,要和灌溉那一题差不多,先进行左边的判断,避免左边部分被浪费

    AC代码:

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<iostream>
    #include<cmath>
    #include<queue>
    using namespace std;
    typedef long long ll;
    #define inf 0x3f3f3f3f
    
    int a[1010];
    
    int main()
    {
        int r,n;
        while(cin>>r>>n)
        {
            if(r==-1&&n==-1)
                break;
            for(int i=0;i<n;i++)
                cin>>a[i];
            sort(a,a+n);
            int i=0,ans=0;
            while(i<n)
            {
                int l=a[i++];
               // while(i<n&&a[i++]<=l+r)
                while(i<n&&a[i]<=l+r)
                    i++;
                int b=a[i-1];
                while(i<n&&a[i]<=b+r)
                    i++;
                ans++;
            }
            cout<<ans<<endl;
        }
        return 0;
    }

    Radar Installation

     POJ - 1328 

    样例:

    Sample Input
    3 2
    1 2
    -3 1
    2 1
    
    1 2
    0 2
    
    0 0
    
    Sample Output
    Case 1: 2
    Case 2: 1

    思路:贪心,不要用所有的雷达去判断岛屿,要转换成利用每个岛屿的位置去确定需要的雷达范围,将雷达范围转换到x坐标轴上表示出来,每一个岛屿对应一个雷达所能达到的区间,从而判断至少多少个区间才能够覆盖所有范围。

    关键:转换成区间相交问题

    问题:源代码是被注释的部分,不知道为什么不对,改了就开一个结构体就对了。难不成是精度损失了???

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<iostream>
    #include<cmath>
    #include<queue>
    using namespace std;
    typedef long long ll;
    #define inf 0x3f3f3f3f
    
    //struct node
    //{
    //    int x,y;
    //} a[1010];
    
    struct node2
    {
        double l,r;
    } b[1010];
    
    //int cmp1(node x,node y)
    //{
    //    if(x.x!=y.x)
    //        return x.x<y.x;
    //    return x.y<y.y;
    //}
    
    int cmp2(node2 x,node2 y)
    {
        if(x.l!=y.l)
            return x.l<y.l;
        return x.r<y.r;
    }
    
    int main()
    {
        int t=1,n,d;
        while(~scanf("%d %d",&n,&d))
        {
            if(n==0&&d==0)
                break;
            int flag=0;
            /*  for(int i=1; i<=n; i++)
              {
                  scanf("%d %d",&a[i].x,&a[i].y);
                  if(a[i].y>d)
                      flag=1;
              }
            //  sort(a+1,a+n+1,cmp1);*/
    
            for(int i=1; i<=n; i++)
            {
                double x,y;
                cin>>x>>y;
                if(y*1.0>d)
                    flag=1;
                //double x=sqrt(a[i].x*a[i].x*1.0+a[i].y*a[i].y*1.0);
                // double x=sqrt(d*d*1.0-a[i].y*a[i].y*1.0);
                // b[i].l=a[i].x-d;
                // b[i].r=a[i].x+d;
                b[i].l=x-sqrt(d*d-y*y);
                b[i].r=x+sqrt(d*d-y*y);
            }
            sort(b+1,b+n+1,cmp2);
            int ans=1;
            double rr=b[1].r;
            for(int i=2; i<=n; i++)
            {
                if(b[i].l<=rr)
                    rr=min(rr,b[i].r);
                else
                {
                    rr=b[i].r;
                    ans++;
                }
            }
    //        if(n==1)
            if(flag)
                printf("Case %d: -1\n",t++);
            else
                printf("Case %d: %d\n",t++,ans);
        }
        return 0;
    }
  • 相关阅读:
    Linux文件结构
    磁盘分区
    BASH简介
    磁盘的基本概念
    Linux文件操作
    创建文件系统
    文件系统挂载
    一些常用命令
    asp.net创建PPT
    asp.net创建、删除、移动文件夹 文件
  • 原文地址:https://www.cnblogs.com/OFSHK/p/12364316.html
Copyright © 2011-2022 走看看