zoukankan      html  css  js  c++  java
  • 51nod 1278 相离的圆

    基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题

    平面上有N个圆,他们的圆心都在X轴上,给出所有圆的圆心和半径,求有多少对圆是相离的。
    例如:4个圆分别位于1, 2, 3, 4的位置,半径分别为1, 1, 2, 1,那么{1, 2}, {1, 3} {2, 3} {2, 4} {3, 4}这5对都有交点,只有{1, 4}是相离的。
    Input
    第1行:一个数N,表示圆的数量(1 <= N <= 50000)
    第2 - N + 1行:每行2个数P, R中间用空格分隔,P表示圆心的位置,R表示圆的半径(1 <= P, R <= 10^9)
    Output
    输出共有多少对相离的圆。
    Input示例
    4
    1 1
    2 1
    3 2
    4 1
    Output示例
    1
     
    排序+二分
    #include <algorithm>
    #include <cstdio>
    #define N 50005
    
    using namespace std;
    struct node
    {
        int l,r;
        friend bool operator<(node a,node b)
        {
            return a.r<b.r;
        }
    }cir[N];
    int n,ans;
    int search(int l,int r,int x)
    {
        for(int mid;l<=r;)
        {
            mid=(l+r)>>1;
            if(cir[mid].r<x) l=mid+1;
            else r=mid-1;
        }
        while(l>0&&cir[l].r>=x) l--;
        return l;
    }
    int main(int argc,char *argv[])
    {
        scanf("%d",&n);
        for(int r,c,i=1;i<=n;++i)
        {
            scanf("%d%d",&c,&r);
            cir[i].l=c-r;
            cir[i].r=c+r;
        }
        sort(cir+1,cir+1+n);
        cir[0].l=cir[0].r=-1;
        for(int i=1;i<=n;++i) ans+=search(1,i,cir[i].l);
        printf("%d
    ",ans);
        return 0;
    }
     
    我们都在命运之湖上荡舟划桨,波浪起伏着而我们无法逃脱孤航。但是假使我们迷失了方向,波浪将指引我们穿越另一天的曙光。
  • 相关阅读:
    AS3中的xml
    HTML5 tools, Animation tools Adobe Edge Preview
    重新审视php+mssql
    SVN合并分支,从主干合并到分支
    AIR HTML相关资料[js部分]
    USACO 1.1friday
    H.High String
    POJ 3670 Eating Together
    HDU 1203:I NEED A OFFER!
    CodeForces #1 B. Spreadsheets
  • 原文地址:https://www.cnblogs.com/ruojisun/p/7582391.html
Copyright © 2011-2022 走看看