51nod

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1278

因为圆心都在x轴上,把每个圆转化成线段后,按线段的起点排序,那么对于每个圆都要从后面找出起点大于当前圆转化成的线段终点的一个点,这个点之后的圆都会与当前圆相离.

因为按起点排序所以可以二分求解.

发现自己二分写的乱七八糟的.

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <vector>
 5 #include <cstring>
 6 #include <string>
 7 #include <algorithm>
 8 #include <string>
 9 #include <set>
10 #include <functional>
11 #include <numeric>
12 #include <sstream>
13 #include <stack>
14 #include <map>
15 #include <queue>
16 #pragma comment(linker, "/STACK:102400000,102400000")
17 #define CL(arr, val)    memset(arr, val, sizeof(arr))
18 
19 #define ll long long
20 #define inf 0x7f7f7f7f
21 #define lc l,m,rt<<1
22 #define rc m + 1,r,rt<<1|1
23 #define pi acos(-1.0)
24 
25 #define L(x)    (x) << 1
26 #define R(x)    (x) << 1 | 1
27 #define MID(l, r)   (l + r) >> 1
28 #define Min(x, y)   (x) < (y) ? (x) : (y)
29 #define Max(x, y)   (x) < (y) ? (y) : (x)
30 #define E(x)        (1 << (x))
31 #define iabs(x)     (x) < 0 ? -(x) : (x)
32 #define OUT(x)  printf("%I64d
", x)
33 #define lowbit(x)   (x)&(-x)
34 #define Read()  freopen("a.txt", "r", stdin)
35 #define Write() freopen("b.txt", "w", stdout);
36 #define maxn 1000000000
37 #define N 2510
38 #define mod 1000000000
39 using namespace std;
40 
41 struct node
42 {
43     int x,y;
44     bool operator < (const node a) const
45     {
46         if(x==a.x) return y<a.y;
47         return x<a.x;
48     }
49 }p[50001];
50 int n;
51 int erfen(int a,int b)
52 {
53     int l=a,r=n-1,mid=0;
54     while(l<=r)
55     {
56         mid=(l+r)/2;
57         if(p[mid].x>b) r=mid-1;
58         else l=mid+1;
59     }
60     if(p[mid].x<=b) mid++;
61     if(mid>=n) return n;
62     else return mid;
63 }
64 
65 int main()
66 {
67     //freopen("a.txt","r",stdin);
68     int a,b;
69     scanf("%d",&n);
70     for(int i=0;i<n;i++)
71     {
72         scanf("%d%d",&a,&b);
73         p[i].x=a-b;
74         p[i].y=a+b;
75     }
76     sort(p,p+n);
77     //for(int i=0;i<n;i++)
78     //{
79       //  printf("%d %d
",p[i].x,p[i].y);
80     //}
81     int num=0;
82     for(int i=0;i<n-1;i++)
83     {
84         int x=erfen(i+1,p[i].y);
85         num+=n-x;
86        // printf("%d %d
",num,x);
87 
88     }
89     printf("%d
",num);
90     return 0;
91 }

当然也可以直接把p[i].x赋值给另外一个一维数组,然后就可以用upper_bound实现二分查找了.

原文地址:https://www.cnblogs.com/nowandforever/p/4598464.html