挑战程序竞赛系列(88):3.6平面扫描(2)

挑战程序竞赛系列(88):3.6平面扫描(2)

传送门:POJ 3168: Barn Expansion

题意:

求不重叠的矩形个数。

当然,题意是说求能够被扩大的矩形个数,很坑爹,一开始以为扩大的单位是1格,其实题目中明确指出: at least some amount without bumping into another barn,英文是硬伤,所以只要不重叠即可。

思路参考hankcs,把矩形抽象成一个个区间,对应于水平区间和垂直区间,定义每个区间的起点和终点。设置一个计数器,遇到起点计数+1,遇到终点计数-1。

那么矩形不重叠等价于对应于的起点计数不超过2。

接着分别沿x轴扫描,和y轴扫描即可。注意一个细节,排序时,横纵坐标相同时,起点优先,因为corner相碰算重叠。

代码如下:

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

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

    static final int MAX_N = 25000 + 16;

    class Point implements Comparable<Point>{
        int ord;         // 当前轴坐标
        int pos;         // 另一轴上的坐标
        int id;          // 对应的矩阵id
        int type;        // 起点: 0 终点: 1

        Point(int ord, int pos, int id, int type){
            this.ord  = ord;
            this.pos  = pos;
            this.id   = id;
            this.type = type;
        }

        @Override
        public int compareTo(Point that) {
            if (this.ord != that.ord) {
                return this.ord - that.ord;
            }
            if (this.pos != that.pos) {
                return this.pos - that.pos;
            }
            return this.type - that.type;
        }
    }

    int N;
    Point[] toX = new Point[4 * MAX_N];     // 以x轴方向扫描
    Point[] toY = new Point[4 * MAX_N];     // 以y轴方向扫描
    boolean[] ok = new boolean[MAX_N];

    void scan(Point[] ps) {

        for (int i = 0; i < 4 * N; ++i) {

            int j = i;
            while (j < 4 * N && ps[j].ord == ps[i].ord) j++;

            boolean invalid = false;
            int share = 0;

            for (int k = i; k < j; ++k) {
                Point p = ps[k];
                if (invalid) {
                    ok[p.id] = false;
                }
                if (p.type == 0) {
                    share ++;
                    if (share >= 2) {
                        invalid = true;
                    }
                }
                else {
                    share --;
                    if (share == 0) {
                        invalid = false;
                    }
                }
            }
            i = j - 1;
        }
    }

    void solve() {

        Arrays.sort(toX, 0, 4 * N);
        Arrays.sort(toY, 0, 4 * N);

        Arrays.fill(ok, true);

        scan(toX);
        scan(toY);

        int ans = 0;
        for (int i = 0; i < N; ++i) {
            if (ok[i]) ans ++;
        }

        out.println(ans);
    }

    void read() {
        N = ni();
        for (int i = 0; i < N; ++i) {
            int A = ni(), B = ni(), C = ni(), D = ni();
            toX[4 * i]     = new Point(A, B, i, 0);
            toX[4 * i + 1] = new Point(A, D, i, 1);
            toX[4 * i + 2] = new Point(C, B, i, 0);
            toX[4 * i + 3] = new Point(C, D, i, 1);

            toY[4 * i]     = new Point(B, A, i, 0);
            toY[4 * i + 1] = new Point(B, C, i, 1);
            toY[4 * i + 2] = new Point(D, A, i, 0);
            toY[4 * i + 3] = new Point(D, C, i, 1);
        }

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

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

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏闻道于事

Java面向对象之抽象类,接口

抽象类: 含有抽象方法的类被声明为抽象类 抽象方法由子类去实现 含有抽象方法的类必须被声明为抽象类 抽象类被子类继承,子类(如果不是抽象类)必须重写抽象类中...

30850
来自专栏彭湖湾的编程世界

【算法】归并排序算法的编码和优化

参考资料 《算法(第4版)》          — — Robert Sedgewick, Kevin Wayne 归并排序的概念 归并排序的实现我是这样来描述...

28180
来自专栏java学习

面试题53(考察求职者对String声明变量在jvm中的存储方法)

(单选题) 1、有如下一段代码,请选择其运行结果() public class StringDemo{ private static final Stri...

30630
来自专栏小樱的经验随笔

pandas用法小结

14020
来自专栏三木的博客

Javascript中的数组

数组的定义: var colors = new Array(20); var colors = new Array('red');  // ['red'...

242100
来自专栏向治洪

归并排序算法的编码和优化

在大型公司的面试过程中,排序是必问的知识。本篇内容来自《算法(第4版)》 — — Robert Sedgewick, Kevin Wayne 概念 归并排序的实...

28560
来自专栏机器学习入门

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

挑战程序竞赛系列(90):3.6凸包(1) 传送门:POJ 2187: Beauty Contest 题意: 平面上有N个牧场。i号牧场的位置在格点(xi,y...

23170
来自专栏机器学习入门

LWC 54:696. Count Binary Substrings

LWC 54:696. Count Binary Substrings 传送门:696. Count Binary Substrings Problem: G...

18770
来自专栏JMCui

Java泛型学习

1、泛型的概念     泛型即“参数化类型”,就比如我们定义方法的时候,定义一个变量,称为形参,变量值根据传进去的实参的值不同而改变。而泛型的出现,就是为了解决...

33540
来自专栏机器学习入门

挑战程序竞赛系列(73):4.7高度数组(3)

挑战程序竞赛系列(73):4.7高度数组(3) 传送门:POJ 3729: Facer’s String 题意: 公共子串: 给出两个字符串A,B,求...

22080

扫码关注云+社区

领取腾讯云代金券