QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#102698 | #5433. Absolute Difference | retcarizy# | WA | 2ms | 5840kb | C++14 | 3.9kb | 2023-05-03 16:14:27 | 2023-05-03 16:14:28 |
Judging History
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 + m);
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;
}
for (int j=k;B[j].first<=A[i].second&&j<=M;j++)
if (lena>eps&&lenb>eps){
int l1=max(A[i].first,B[j].first);
int r1=min(A[i].second,B[j].second);
if (B[j].first<=A[i].first) ans+=(0.5*(A[i].first+A[i].second)-0.5*(B[j].first+l1))*(A[i].second-A[i].first)/lena*(l1-B[j].first)/lenb;
if (B[j].second>=A[i].second) ans+=(0.5*(r1+B[j].second)-0.5*(A[i].first+A[i].second))*(A[i].second-A[i].first)/lena*(B[j].second-r1)/lenb;
if (B[j].first>A[i].first) ans+=(0.5*(r1+A[i].second)-0.5*(l1+r1))*(A[i].second-r1)/lena*(r1-l1)/lenb;
if (B[j].second<A[i].second) ans+=(0.5*(l1+r1)-0.5*(A[i].first+l1))*(l1-A[i].first)/lena*(r1-l1)/lenb;
ans+=(1.0*(p3(r1)-p3(l1))/3-1.0*l1*(p2(r1)-p2(l1))-1.0*l1*l1*(r1-l1))/lena/lenb;
}else if (lena>eps){
ans+=(1.0*B[j].first-0.5*(B[j].first+A[i].first))*(B[j].first-A[i].first)/lena*1.0/numb;
ans+=(0.5*(B[j].first+A[i].second)-1.0*B[j].first)*(A[i].second-B[j].first)/lena*1.0/numb;
}else if (lenb>eps){
ans+=(1.0*A[i].first-0.5*(B[j].first+A[i].first))*(A[i].first-B[j].first)/numa/lenb;
ans+=(0.5*(B[j].second+A[i].second)-1.0*A[i].first)*(B[j].second-A[i].first)/numa/lenb;
}
}
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;
}
printf("%.12lf\n", ans);
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 5804kb
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: 2ms
memory: 5840kb
input:
1 1 0 1 1 1
output:
0.500000000000
result:
ok found '0.500000000', expected '0.500000000', error '0.000000000'
Test #3:
score: -100
Wrong Answer
time: 1ms
memory: 3764kb
input:
1 1 -1000000000 1000000000 -1000000000 1000000000
output:
-333333333.333333432674
result:
wrong answer 1st numbers differ - expected: '666666666.6666666', found: '-333333333.3333334', error = '1.5000000'