# 挑战程序竞赛系列（95）：3.6数值积分（1）

## 挑战程序竞赛系列（95）：3.6数值积分（1）

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

0 条评论

## 相关文章

### 聊聊HystrixThreadPool

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

921

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

892

### 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

### Oracle sqlldr 如何导入一个日期列

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

1866

### 高通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

### Save Camera Document

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

2898