挑战程序竞赛系列(95):3.6数值积分(1)

挑战程序竞赛系列(95):3.6数值积分(1)

传送门:AOJ 1313: Intersection of Two Prisms

题意:

有一个侧棱与Z轴平行的棱柱P1和一个侧棱与y轴平行的棱柱P2。它们都向两端无限延伸,底面分别是包含M个顶点和N个顶点的凸多边形,其中第i个顶点的坐标分别是(X1, Y1)和(X2, Y2)。请计算这两个棱柱公共部分的体积。

按照x轴进行切片,求出每一个瞬间的面积(积分),所以只需要知道给定x,求出f(x)即可。书中给出了积分的近似公式:

代码如下:

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

public class Main{

    String INPUT = "./data/judge/201710/A1313.txt";

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

    static final int INF = 0x3f3f3f3f;
    int N, M;

    double width(int[] X, int[] Y, int n, double x) {
        double lb = INF, ub = -INF;
        for (int i = 0; i < n; ++i) {
            double x1 = X[i], y1 = Y[i], x2 = X[(i + 1) % n], y2 = Y[(i + 1) % n];
            if ((x1 - x) * (x2 - x) <= 0 && x1 != x2) {
                double y = y1 + (y2 - y1) * (x - x1) / (x2 - x1);
                lb = Math.min(lb, y);
                ub = Math.max(ub, y);
            }
        }
        return Math.max(0, ub - lb);
    }

    int min(int[] nums, int start, int end) {
        int min = INF;
        for (int i = start; i < end; ++i) {
            min = Math.min(min, nums[i]);
        }
        return min;
    }

    int max(int[] nums, int start, int end) {
        int max = -INF;
        for (int i = start; i < end; ++i) {
            max = Math.max(max, nums[i]);
        }
        return max;
    }

    void solve(int[] X1, int[] Y1, int[] X2, int[] Y2) {
        int min1 = min(X1, 0, N), max1 = max(X1, 0, N);
        int min2 = min(X2, 0, M), max2 = max(X2, 0, M);

        List<Integer> xs = new ArrayList<>();
        for (int i = 0; i < N; ++i) xs.add(X1[i]);
        for (int i = 0; i < M; ++i) xs.add(X2[i]);

        Collections.sort(xs);

        double res = 0.0;
        for (int i = 0; i + 1 < xs.size(); ++i) {
            double a = xs.get(i), b = xs.get(i + 1), c = (a + b) / 2;
            if (min1 <= c && c <= max1 && min2 <= c && c <= max2) {
                double fa = width(X1, Y1, N, a) * width(X2, Y2, M, a);
                double fb = width(X1, Y1, N, b) * width(X2, Y2, M, b);
                double fc = width(X1, Y1, N, c) * width(X2, Y2, M, c);
                res += (b - a) / 6 * (fa + 4 * fc + fb);
            }
        }

        out.printf("%.10f\n", res);
    }

    void read() {
        while (true) {
            N = ni();
            M = ni();

            if (N + M == 0) break;

            int[] X1 = new int[N];
            int[] Y1 = new int[N];

            for (int i = 0; i < N; ++i) {
                X1[i] = ni();
                Y1[i] = ni();
            }

            int[] X2 = new int[M];
            int[] Y2 = new int[M];
            for (int i = 0; i < M; ++i) {
                X2[i] = ni();
                Y2[i] = ni();
            }

            solve(X1, Y1, X2, Y2);
        }
    }

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

注意width中for的循环,因为给定的坐标点已经是凸包形式,所以这种O(n)的更新方案才正确。

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

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏码匠的流水账

聊聊HystrixThreadPool

hystrix-core-1.5.12-sources.jar!/com/netflix/hystrix/HystrixThreadPool.java

921
来自专栏c#开发者

在DataGrid中选择,确认,删除多行复选框列表

在DataGrid中选择,确认,删除多行复选框列表 Selecting, Confirming & Deleting Mul...

3637
来自专栏搞前端的李蚊子

Html5模拟通讯录人员排序(sen.js)

// JavaScript Document  var PY_Json_Str = ""; var PY_Str_1 = ""; var PY_Str_...

6326
来自专栏我和未来有约会

简练的视图模型 ViewModel

patterns & practices Developer Center 发布了 Unity Application Block 1.2 for Silver...

2319
来自专栏marsggbo

Udacity并行计算课程 CS344 编程作业答案

892
来自专栏c#开发者

Processing Binary Documents Through BizTalk Via Web Services[转]

Published 21 July 06 08:24 AM | rseroter Just finishing up a two-week BizTalk P...

3414
来自专栏Hadoop数据仓库

Oracle sqlldr 如何导入一个日期列

1. LOAD DATA INFILE * INTO TABLE test FIELDS TERMINATED BY X'9' TRAILING NULLCO...

1866
来自专栏linux驱动个人学习

高通Audio中ASOC的machine驱动

ASoC被分为Machine、Platform和Codec三大部分,其中的Machine驱动负责Platform和Codec之间的耦合以及部分和设备或板子特定的...

1K4
来自专栏专知

2018年SCI期刊最新影响因子排行,最高244,人工智能TPAMI9.455

2018年6月26日,最新的SCI影响因子正式发布,涵盖1万2千篇期刊。CA-Cancer J Clin 依然拔得头筹,其影响因子今年再创新高,达244.585...

1462
来自专栏Pulsar-V

Save Camera Document

#pragma once #include "HCCamera.h" #include <time.h> #include <cstdio> #incl...

2898

扫码关注云+社区