挑战程序竞赛系列(90):3.6凸包(1)

挑战程序竞赛系列(90):3.6凸包(1)

传送门:POJ 2187: Beauty Contest

题意:

平面上有N个牧场。i号牧场的位置在格点(xi,yi)(x_i, y_i),所有牧场的位置互不相同。请计算距离最远的两个牧场的距离,输出最远距离的平方。

假设有四个点,其中一个点在三个点的内部,可以知道该点是冗余的,所以我们只需要维护所有点的凸包,求出凸包上点集中的两两最大即可。

凸包的详细介绍可以参考博文: http://blog.csdn.net/u014688145/article/details/72200018#t3

暴力枚举点对

代码如下:

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.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;

public class Main{

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

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

    static final int MAX_N = 50000;

    class Point implements Comparable<Point>{
        int x;
        int y;

        Point(int x, int y){
            this.x = x;
            this.y = y;
        }

        Point sub(Point a) {
            return new Point(x - a.x, y - a.y);
        }

        int det(Point a) {
            return x * a.y - y * a.x;
        }

        @Override
        public int compareTo(Point o) {
            return this.x != o.x ? this.x - o.x : this.y - o.y;
        }
    }

    int N;
    Point[] ps;

    List<Point> convexHull(){
        Arrays.sort(ps);

        List<Point> ans = new ArrayList<Point>();

        int[] stack = new int[2 * N];
        int tot = 0;
        for (int i = 0; i < N; ++i) {
            while (tot > 1 && ps[stack[tot - 1]].sub(ps[stack[tot - 2]]).det(ps[i].sub(ps[stack[tot - 1]])) <= 0) tot--;
            stack[tot++] = i;
        }

        for (int i = N - 2, t = tot; i >= 0; --i) {
            while (tot > t && ps[stack[tot - 1]].sub(ps[stack[tot - 2]]).det(ps[i].sub(ps[stack[tot - 1]])) <= 0) tot--;
            stack[tot++] = i;
        }
        tot --;

        for (int i = 0; i < tot; ++i) {
            ans.add(ps[stack[i]]);
        }

        return ans;
    }

    int dist(Point a, Point b) {
        int dx = a.x - b.x;
        int dy = a.y - b.y;
        return dx * dx + dy * dy;
    }

    void solve() {

        List<Point> qs = convexHull();
        int max = 0;
        for (int i = 0; i < qs.size(); ++i) {
            for (int j = i + 1; j < qs.size(); ++j) {
                max = Math.max(max, dist(qs.get(i), qs.get(j)));
            }
        }
        out.println(max);
    }

    void read() {
         N = ni();
         ps = new Point[N];
         for (int i = 0; i < N; ++i) {
             ps[i] = new Point(ni(), ni());
         }
         solve();
    }

    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);
        }
    }
}

旋转卡壳法

参考博文: http://www.cnblogs.com/DreamUp/archive/2010/09/16/1828131.html

参考《挑战》P263,借用书上的解释,和上篇博文的一张图:

实际上,最远点对一定在所有对踵点对上,所以只需要枚举出凸包上的对踵点对即能高效求解最远点对。

那么假设我们知道了对踵点对,qa和qb,如何求下一个对踵点对呢?考虑qa和qa的下一个顶点,qb和qb的下一顶点,可以画出两条向量,并且以qa为原点,做qb和qb的下一顶点的平行线,如下:

于是根据叉积可以得到两向量的相对位置关系(谁在最外侧),如果平移后的线在内侧,则说明对踵点对为qb和qa的next,反之为qb的next和qa。

有了上述关系,就可以直接根据对踵点对更新最远点对了,而我们知道在对踵点对内部的点是不可能为最远点对的,这是复杂度降低的真正原因。

代码如下:

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/201710/P2187.txt";

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

    static final int MAX_N = 50000 + 16;
    int N;

    class P implements Comparable<P>{
        int x;
        int y;

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

        P sub(P a) {
            return new P(x - a.x, y - a.y);
        }

        int det(P a) {
            return x * a.y - y * a.x;
        }

        @Override
        public int compareTo(P o) {
            return x != o.x ? x - o.x : y - o.y;
        }
    }

    P[] ps;

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

    P[] convexHull() {
        P[] qs = new P[2 * N];
        Arrays.sort(ps);
        int k = 0;
        for (int i = 0; i < N; ++i) {
            while (k > 1 && qs[k - 1].sub(qs[k - 2]).det(ps[i].sub(qs[k - 1])) <= 0) k--;
            qs[k++] = ps[i];
        }

        for (int i = N - 2, t = k; i >= 0; --i) {
            while (k > t && qs[k - 1].sub(qs[k - 2]).det(ps[i].sub(qs[k - 1])) <= 0) k--;
            qs[k++] = ps[i];
        }

        k --;
        P[] res = new P[k];
        System.arraycopy(qs, 0, res, 0, k);
        return res;
    }

    void solve() {
        P[] qs = convexHull();
        int n = qs.length;
        if (n == 2) {
            out.println(dist(qs[0], qs[1]));
            return;
        }

        int i = 0;
        int j = 0;
        for (int k = 1; k < n; ++k) {
            if (qs[k].x < qs[i].x) i = k;
            if (qs[k].x > qs[j].x) j = k;
        }

        int si = i;
        int sj = j;

        int max = 0;
        while (i != sj || j != si) {
            max = Math.max(max, dist(qs[i], qs[j]));
            if (qs[(i + 1) % n].sub(qs[i]).det(qs[(j + 1) % n].sub(qs[j])) < 0) {
                i = (i + 1) % n;
            }
            else {
                j = (j + 1) % n;
            }
        }
        out.println(max);
    }   

    void read() {
        N = ni();
        ps = new P[N];
        for (int i = 0; i < N; ++i) {
            ps[i] = new P(ni(), ni());
        }
        solve();
    }

    FastScanner in;
    PrintWriter out;

    void run() throws IOException {
        boolean oj;
        try {
            oj = ! System.getProperty("user.dir").equals("F:\\oxygen_workspace\\Algorithm");
        } 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);
        }
    }
} 

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏小小挖掘机

来学习几个简单的Hive函数啦

咳咳,今天来介绍一下几个Hive函数吧,先放一张我登哥划水的照片,希望大家也做一只自由的鱼儿,在知识的海洋里游呀游,嘻嘻! ? 今天我们来介绍几个Hive常用的...

3414
来自专栏个人随笔

Java 关于集合框架那点事儿

 1.引入集合框架   采用数组存在的一些缺陷:    1.数组长度固定不变,不能很好地适应元素数量动态变化的情况。    2.可通过数组名.length获取数...

26910
来自专栏老马说编程

(31) 剖析Arrays / 计算机程序的思维逻辑

数组是存储多个同类型元素的基本数据结构,数组中的元素在内存连续存放,可以通过数组下标直接定位任意元素,相比我们在后续章节介绍的其他容器,效率非常高。 数组操作是...

1818
来自专栏前端儿

理解运用JS的闭包、高阶函数、柯里化

JS的闭包,是一个谈论得比较多的话题了,不过细细想来,有些人还是理不清闭包的概念定义以及相关的特性。

712
来自专栏阿炬.NET

Reflector、reflexil、De4Dot、IL指令速查表

3185
来自专栏杨龙飞前端

编写一个javscript函数 fn,该函数有一个参数 n(数字类型),其返回值是一个数组,该数组内是 n 个随机且不重复的整数,且整数取值范围是 [2, 32]。

1855
来自专栏Jackson0714

IL指令速查

3247
来自专栏博客园

IL指令详细表

512
来自专栏较真的前端

关于数字的前端面试题

2176
来自专栏Golang语言社区

Go语言的fmt包中文教程

Fmt包 import "fmt" 简介 ▾ Package fmt包含有格式化I/O函数,类似于C语言的printf和scanf。格式字符串的规则来源于C但更...

2346

扫码关注云+社区