QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#173726 | #5433. Absolute Difference | aesthetic | WA | 1ms | 4056kb | C++20 | 2.5kb | 2023-09-10 01:28:35 | 2023-09-10 01:28:35 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> split(const vector<pair<int, int>> &v, const vector<int> &pos)
{
vector<pair<int, int>> ret;
for(auto[l, r] : v)
{
if(l == r)
{
ret.emplace_back(l, r);
continue;
}
int last = l;
auto it = upper_bound(pos.begin(), pos.end(), l);
while(it != pos.end() && (*it) <= r)
{
ret.emplace_back(last, *it);
last = *it;
it++;
}
}
return ret;
}
long double f(int l1, int r1, int l2, int r2)
{
if(l1 == l2 && r1 == r2)
{
return (r1 - l1) / 3.0L;
}
if(r1 <= l2 || r2 <= l1)
{
long double E1 = (l1 + r1) / 2.0L;
long double E2 = (l2 + r2) / 2.0L;
return abs(E1 - E2);
}
return 0;
}
int main()
{
ios_base::sync_with_stdio(false), cin.tie(NULL);
int n, m;
cin >> n >> m;
vector<pair<int, int>> a(n), b(m);
int tota = 0, totb = 0;
for(int i = 0; i < n; ++i)
{
cin >> a[i].first >> a[i].second;
tota += (a[i].second - a[i].first);
}
for(int i = 0; i < m; ++i)
{
cin >> b[i].first >> b[i].second;
totb += (b[i].second - b[i].first);
}
vector<int> pos;
for(auto[l, r] : a)
pos.push_back(l), pos.push_back(r);
for(auto[l, r] : b)
pos.push_back(l), pos.push_back(r);
sort(pos.begin(), pos.end());
pos.erase(unique(pos.begin(), pos.end()), pos.end());
a = split(a, pos);
b = split(b, pos);
n = a.size();
m = b.size();
sort(a.begin(), a.end());
sort(b.begin(), b.end());
vector<long long> pref_prob(m), pref_exp(m);
for(int i = 0; i < m; ++i)
{
auto [l2, r2] = b[i];
long long E2 = (l2 + r2);
long long prob2 = 1;
if(totb)
prob2 = (r2 - l2);
if(i == 0)
{
pref_prob[i] = prob2;
pref_exp[i] = E2 * prob2;
}
else
{
pref_prob[i] = pref_prob[i - 1] + prob2;
pref_exp[i] = pref_exp[i - 1] + E2 * prob2;
}
}
long double ans = 0;
for(int i = 0; i < n; ++i)
{
auto [l1, r1] = a[i];
long double prob1 = 1.0L / n;
long long E1 = (l1 + r1);
if(tota)
prob1 = (r1 - l1) * 1.0L / tota;
int jj = lower_bound(b.begin(), b.end(), make_pair(l1, -1)) - b.begin();
if(jj > 0)
ans += prob1 * (pref_prob[jj - 1] * E1 - pref_exp[jj - 1]) / 2.0L;
for(int j = jj; j < m; ++j)
{
auto [l2, r2] = b[j];
long double prob2 = 1.0L / m;
if(totb)
prob2 = (r2 - l2) * 1.0L / totb;
ans += prob1 * prob2 * f(l1, r1, l2, r2);
}
}
if(totb)
ans /= totb;
else
ans /= m;
cout << setprecision(20) << fixed << ans << endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3772kb
input:
1 1 0 1 0 1
output:
0.33333333333333333334
result:
ok found '0.333333333', expected '0.333333333', error '0.000000000'
Test #2:
score: 0
Accepted
time: 1ms
memory: 4056kb
input:
1 1 0 1 1 1
output:
0.50000000000000000000
result:
ok found '0.500000000', expected '0.500000000', error '0.000000000'
Test #3:
score: -100
Wrong Answer
time: 1ms
memory: 3964kb
input:
1 1 -1000000000 1000000000 -1000000000 1000000000
output:
0.33333333333333333334
result:
wrong answer 1st numbers differ - expected: '666666666.6666666', found: '0.3333333', error = '1.0000000'