zoukankan      html  css  js  c++  java
  • HDU 4643 GSM (2013多校5 1001题 计算几何)

    GSM

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
    Total Submission(s): 622    Accepted Submission(s): 206


    Problem Description
    Xiao Ming is traveling around several cities by train. And the time on the train is very boring, so Xiao Ming will use the mobile Internet. We all know that mobile phone receives the signal from base station and it will change the base station when moving on the train. Xiao Ming would like to know how many times the base station will change from city A to city B.
    Now, the problem is simplified. We assume the route of train is straight, and the mobile phone will receive the signal from the nearest base station. 
     
    Input
    Multiple cases. For each case, The first line: N(3<=N<=50) - the number of cities, M(2<=M<=50) - the number of base stations. Then there are N cities with coordinates of (x, y) and M base stations with coordinates of (x, y) - (0<=x<=1000, 0<=y<=1000, both x and y is integer).Then there is a number : K, the next, there are K queries, for each query, each line, there are two numbers: a, b.
     
    Output
    For each query, tell Xiao Ming how many times the base station will change from city a to city b.
     
    Sample Input
    4 4 0 2 1 3 1 0 2 0 1 2 1 1 2 2 2 1 4 1 2 1 3 1 4 3 4
     
    Sample Output
    0 1 2 1
    Hint
    The train way from a to b will not cross the point with the same distance from more than 2 base stations. (For the distance d1 and d2, if fabs(d1-d2)<1e-7, we think d1 == d2). And every city exactly receive signal from just one base station.
     
    Source
     
    Recommend
    zhuyuanchen520
     

    在从u->v的路径上,不断分成两段去做。

    很简单

    #include <stdio.h>
    #include <algorithm>
    #include <iostream>
    #include <string.h>
    #include <queue>
    #include <map>
    #include <set>
    #include <vector>
    #include <string>
    #include <math.h>
    #include <time.h>
    using namespace std;
    
    const double eps = 1e-8;
    struct Point
    {
        double x,y;
        Point(){}
        Point(double _x,double _y)
        {
            x = _x;y = _y;
        }
        void input()
        {
            scanf("%lf%lf",&x,&y);
        }
    };
    //*两点间距离
    inline double dis(Point a,Point b)
    {
        return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
    }
    Point p1[55],p2[55];
    int n,m;
    inline int Belong(Point p)
    {
        int k = 0;
        double d = dis(p,p2[0]);
        for(int i = 1;i < m;i++)
        {
            double d2 = dis(p,p2[i]);
            if(d2 < d)
            {
                d = d2;
                k = i;
            }
        }
        return k;
    }
    int solve(Point a,Point b)
    {
        int k1 = Belong(a);
        int k2 = Belong(b);
        if(k1 == k2)return 0;
        if(dis(a,b)<eps)return 1;
        Point t = Point((a.x+b.x)/2,(a.y+b.y)/2);
        return solve(a,t)+solve(t,b);
    }
    
    int main()
    {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        while(scanf("%d%d",&n,&m) == 2)
        {
            for(int i = 0;i < n;i++)
                p1[i].input();
            for(int i = 0;i < m;i++)
                p2[i].input();
            int K;
            int u,v;
            scanf("%d",&K);
            while(K--)
            {
                scanf("%d%d",&u,&v);
                u--;v--;
                printf("%d
    ",solve(p1[u],p1[v]));
            }
        }
        return 0;
    }
  • 相关阅读:
    【竞赛笔记】飞思卡尔智能车竞赛
    【实习笔记】智能广场健身设备总结
    RabbitMQ之安装
    数据结构与算法之队列
    joda-time时间操作组件
    JavaScript中的跨域问题
    数据结构与算法之链表
    Jedis集成到项目中
    ICMP协议和ping命令
    jedis的使用
  • 原文地址:https://www.cnblogs.com/kuangbin/p/3244132.html
Copyright © 2011-2022 走看看