前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >挑战程序竞赛系列(84):3.6极限情况(1)

挑战程序竞赛系列(84):3.6极限情况(1)

作者头像
用户1147447
发布2018-01-02 10:36:38
4970
发布2018-01-02 10:36:38
举报
文章被收录于专栏:机器学习入门机器学习入门

挑战程序竞赛系列(84):3.6极限情况(1)

传送门:POJ 1981: Circle and Points

题意:

平面上有N个点,给定半径为1的圆,最多能在圆内点的个数。

这种题目很大特色在于,如果枚举圆的圆心,那么在偌大的空间中,有无数个圆,显然是不现实的。所以得考虑极限情况,也就是找出一种特殊的状态,更新它们的集合,能够获得答案。

此题的极限情况为:当两个点在一个圆上时,最大值一定在于这种情况。因此,我们可以枚举两个点构成的圆,并且计算该圆内有多少个点,更新最大值即可。

证明: 假设存在一个圆,圆内包含了最大个数的点,稍微移动圆的圆心不会发生任何变化,直到移动至出现两个点在圆上时,最大值依旧没有发生变化,但一旦再移动时,最大个数将减小。所以最大值一定存在于某两个点i和j构成的圆,且i和j在圆上or圆内。

参考至: http://www.hankcs.com/program/algorithm/poj-1981-circle-and-points.html

alt text
alt text

所以我们只需要计算出两个点构成的极角,当然都得以i为坐标原点。

  • atan2的角度为j和i的纵坐标与横坐标之比,取反tan。
  • acos的角度为d/2,图中很清楚了。
  • 所以两个极限角即为atan2 - acos和atan2 + acos。

最后,对极角排个序,遇到起始点加1,遇到终点减1,不断更新重叠区域个数的最大值即可。

代码如下:

代码语言:javascript
复制
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main{

    String INPUT = "./data/judge/201709/P1981.txt";

    public static void main(String[] args) throws IOException {
        new Main().run();
    }

    static final int MAX_N = 302;

    class P {
        double x;
        double y;
        P(double x, double y){
            this.x = x;
            this.y = y;
        }
    }

    class PolarAngle implements Comparable<PolarAngle>{

        double  angle;
        boolean flag;
        PolarAngle(double angle, boolean flag){
            this.angle = angle;
            this.flag  = flag;
        }
        @Override
        public int compareTo(PolarAngle that) {
            return Double.compare(this.angle, that.angle);
        }
    }

    P[] points = new P[MAX_N];
    PolarAngle[] pa = new PolarAngle[2 * MAX_N];

    int N;
    int ans;

    double dist(P a, P b) {
        double dx = a.x - b.x;
        double dy = a.y - b.y;
        return Math.sqrt(dx * dx + dy * dy);
    }

    void solve() {
        ans = 1;
        for (int i = 0; i < N; ++i) {
            int m = 0;
            for (int j = 0; j < N; ++j) {
                double d = 0;
                if (i != j && (d = dist(points[i], points[j])) <= 2.0) {
                    double theta = Math.acos(d / 2);
                    double phi   = Math.atan2(points[j].y - points[i].y, points[j].x - points[i].x);
                    pa[m++] = new PolarAngle(phi - theta, true);  // start
                    pa[m++] = new PolarAngle(phi + theta, false); // end;
                }
            }

            Arrays.sort(pa, 0, m);

            int sum = 1;
            for (int j = 0; j < m; ++j) {
                if (pa[j].flag) {
                    sum ++;
                }
                else {
                    sum --;
                }
                ans = Math.max(ans, sum);
            }
        }
    }

    void read() {
        while (true) {
            N = ni();
            if (N == 0) break;
            for (int i = 0; i < N; ++i) {
                points[i] = new P(nd(), nd());
            }
            ans = 0;
            solve();
            out.println(ans);
        }
    }

    FastScanner in;
    PrintWriter out;

    void run() throws IOException {
        boolean oj;
        try {
            oj = ! System.getProperty("user.dir").equals("F:\\java_workspace\\leetcode");
        } catch (Exception e) {
            oj = System.getProperty("ONLINE_JUDGE") != null;
        }

        InputStream is = oj ? System.in : new FileInputStream(new File(INPUT));
        in = new FastScanner(is);
        out = new PrintWriter(System.out);
        long s = System.currentTimeMillis();
        read();
        out.flush();
        if (!oj){
            System.out.println("[" + (System.currentTimeMillis() - s) + "ms]");
        }
    }

    public boolean more(){
        return in.hasNext();
    }

    public int ni(){
        return in.nextInt();
    }

    public long nl(){
        return in.nextLong();
    }

    public double nd(){
        return in.nextDouble();
    }

    public String ns(){
        return in.nextString();
    }

    public char nc(){
        return in.nextChar();
    }

    class FastScanner {
        BufferedReader br;
        StringTokenizer st;
        boolean hasNext;

        public FastScanner(InputStream is) throws IOException {
            br = new BufferedReader(new InputStreamReader(is));
            hasNext = true;
        }

        public String nextToken() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    hasNext = false;
                    return "##";
                }
            }
            return st.nextToken();
        }

        String next = null;
        public boolean hasNext(){
            next = nextToken();
            return hasNext;
        }

        public int nextInt() {
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return Integer.parseInt(more);
        }

        public long nextLong() {
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return Long.parseLong(more);
        }

        public double nextDouble() {
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return Double.parseDouble(more);
        }

        public String nextString(){
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return more;
        }

        public char nextChar(){
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return more.charAt(0);
        }
    }
}
alt text
alt text
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-09-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 挑战程序竞赛系列(84):3.6极限情况(1)
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档