QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#102608#5433. Absolute Differenceretcarizy#WA 2ms5656kbC++145.0kb2023-05-03 15:04:302023-05-03 15:04:34

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-03 15:04:34]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:5656kb
  • [2023-05-03 15:04:30]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
int n, m, N, M, numa, numb;
pair<int, int> a[100005], b[100005], A[100005], B[100005];
double ans;
int lena, lenb;
double p3(double x){
	return x * x * x;
}
double p2(double x){
	return x * x;
}
int main(){
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++)
		scanf("%d%d", &a[i].first, &a[i].second);
	for (int i = 1; i <= m; i++)
		scanf("%d%d", &b[i].first, &b[i].second);
	sort(a + 1, a + 1 + n);
	sort(b + 1, b + 1 + n);
	A[N = 1] = a[1];
	for (int i = 2; i <= n; i++)
		if (a[i].first <= A[N].second)
			A[N].second = max(A[N].second, a[i].second);
		else
			A[++N] = a[i];
	B[M = 1] = b[1];
	for (int i = 2; i <= m; i++)
		if (b[i].first <= B[M].second)
			B[M].second = max(B[M].second, b[i].second);
		else
			B[++M] = b[i];
	for (int i = 1; i <= N; i++){
		lena += A[i].second - A[i].first;
		numa++;
	}
	for (int i = 1; i <= M; i++){
		lenb += B[i].second - B[i].first;
		numb++;
	}
	int k;
	double mid, len;
	len = 0;
	k = 1;
	for (int i = 1; i <= N; i++){
		while (k <= M && B[k].second < A[i].first){
			if (lenb > eps){
				mid = (mid * len + 0.5 * (B[k].second + B[k].first) * (B[k].second - B[k].first)) / (len + B[k].second - B[k].first);
				len += B[k].second - B[k].first;
			}
			else{
				mid = (mid * len + 0.5 * (B[k].second + B[k].first) * 1) / (len + 1);
				len += 1;				
			}
			k++;
		}
		if (lena > eps && lenb > eps)
			ans += (0.5 * (A[i].first + A[i].second) - mid) * (A[i].second - A[i].first) / lena * len / lenb;
		else{
			if (lena > eps)
				ans += (0.5 * (A[i].first + A[i].second) - mid) * (A[i].second - A[i].first) / lena * len / numb;
			else
			if (lenb > eps)
				ans += (0.5 * (A[i].first + A[i].second) - mid) * 1 / numa * len / lenb;
			else
				ans += (0.5 * (A[i].first + A[i].second) - mid) * 1 / numa * len / numb;
		}
		if (k <= M && B[k].first < A[i].first && B[k].second >= A[i].first && B[k].second < A[i].second){
			double a = A[i].first, b = A[i].second, c = B[k].first, d = B[k].second;
			ans += ((p3(d) - p3(a)) / 3 - (c + d) * (p2(d) - p2(a)) / 2 + (d - a) * (p2(c) + p2(d)) / 2 + 0.5 * (b - d) * (d - c) * (d + c)) / lena / lenb;
		}
		if (k <= M && B[k].first <= A[i].first && B[k].second >= A[i].second){
			double a = A[i].first, b = A[i].second, c = B[k].first, d = A[i].second;
			if (lena > eps){
				ans += ((p3(d) - p3(a)) / 3 - (c + d) * (p2(d) - p2(a)) / 2 + (d - a) * (p2(c) + p2(d)) / 2 + 0.5 * (b - d) * (d - c) * (d + c)) / lena / lenb;			
				c = A[i].first, d = A[i].second, a = A[i].second, b = B[k].second;
				ans += ((p3(d) - p3(a)) / 3 - (c + d) * (p2(d) - p2(a)) / 2 + (d - a) * (p2(c) + p2(d)) / 2 + 0.5 * (b - d) * (d - c) * (d + c)) / lena / lenb;			
			}
			else
			if (lenb < eps){
				
			}
			else{
				a = A[i].first, b = A[i].second, c = B[k].first, d = B[k].second;
				ans += (a - 0.5 * (c + a)) * (a - c) / lenb / numa + (0.5 * (a + d) - a) * (d - a) / lenb / numa;
			}
		}
	}
	len = 0;
	k = M;
	for (int i = N; i >= 1; i--){
		while (k >= 1 && B[k].first > A[i].second){
			if (lenb > eps){
				mid = (mid * len + 0.5 * (B[k].second + B[k].first) * (B[k].second - B[k].first)) / (len + B[k].second - B[k].first);
				len += B[k].second - B[k].first;
			}
			else{
				mid = (mid * len + 0.5 * (B[k].second + B[k].first) * 1) / (len + 1);
				len += 1;				
			}
			k++;
		}
		if (lena > eps && lenb > eps)
			ans += (mid - 0.5 * (A[i].first + A[i].second)) * (A[i].second - A[i].first) / lena * len / lenb;
		else
		if (lena > eps)
			ans += (mid - 0.5 * (A[i].first + A[i].second)) * (A[i].second - A[i].first) / lena * len / numb;
		else
		if (lenb > eps)
			ans += (mid - 0.5 * (A[i].first + A[i].second)) * 1 / numa * len / lenb;
		else
			ans += (mid - 0.5 * (A[i].first + A[i].second)) * 1 / numa * len / numb;
		if (k >= 1 && B[k].second > A[i].second && B[k].first <= A[i].second && B[k].first > A[i].first){
			double c = A[i].first, d = A[i].second, a = B[k].first, b = B[k].second;
			ans += ((p3(d) - p3(a)) / 3 - (c + d) * (p2(d) - p2(a)) / 2 + (d - a) * (p2(c) + p2(d)) / 2 + 0.5 * (b - d) * (d - c) * (d + c)) / lena / lenb;
		}
	}
	k = 1;
	for (int i = 1; i <= M; i++){
		while (k <= N && A[k].second < B[i].first)
			k++;
		if (k <= N && B[i].first >= A[k].first && B[i].second <= A[k].second && A[k] != B[i]){
			double a = B[i].first, b = B[i].second, c = A[k].first, d = B[i].second;
			if (lenb > eps){
				ans += ((p3(d) - p3(a)) / 3 - (c + d) * (p2(d) - p2(a)) / 2 + (d - a) * (p2(c) + p2(d)) / 2 + 0.5 * (b - d) * (d - c) * (d + c)) / lena / lenb;			
				c = B[i].first, d = B[i].second, a = B[i].second, b = A[k].second;
				ans += ((p3(d) - p3(a)) / 3 - (c + d) * (p2(d) - p2(a)) / 2 + (d - a) * (p2(c) + p2(d)) / 2 + 0.5 * (b - d) * (d - c) * (d + c)) / lena / lenb;						
			}
			else{
				a = B[i].first, b = B[i].second, c = A[k].first, d = A[k].second;
				ans += (a - 0.5 * (c + a)) * (a - c) / lena / numb + (0.5 * (d + a) - a) * (d - a) / lena / numb;
			}
		}
	}
	printf("%.12lf\n", ans);
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 5656kb

input:

1 1
0 1
0 1

output:

0.333333333333

result:

ok found '0.333333333', expected '0.333333333', error '0.000000000'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3744kb

input:

1 1
0 1
1 1

output:

0.500000000000

result:

ok found '0.500000000', expected '0.500000000', error '0.000000000'

Test #3:

score: 0
Accepted
time: 2ms
memory: 3552kb

input:

1 1
-1000000000 1000000000
-1000000000 1000000000

output:

666666666.666666626930

result:

ok found '666666666.666666627', expected '666666666.666666627', error '0.000000000'

Test #4:

score: -100
Wrong Answer
time: 2ms
memory: 3540kb

input:

1 1
-1000000000 0
0 1000000000

output:

-500000000.000000059605

result:

wrong answer 1st numbers differ - expected: '1000000000.0000000', found: '-500000000.0000001', error = '1.5000000'