QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#764197 | #5433. Absolute Difference | fosov | WA | 1ms | 4064kb | C++14 | 2.8kb | 2024-11-20 06:12:13 | 2024-11-20 06:12:14 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lll __int128
#define INF 0x3f3f3f3f
#define LNF 0x3f3f3f3f3f3f3f3fll
#define MOD 998244353
#define pii pair<int, int>
#define ld long double
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define N 20
int main() {
#ifdef TEST
freopen("zz.in", "r+", stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << setprecision(18);
int n, m; cin >> n >> m;
vector<pii> a(n), b(m);
ll ca = 0, cb = 0;
for (int i = 0; i < n; ++ i) cin >> a[i].fi >> a[i].se, ca += a[i].se - a[i].fi;
for (int i = 0; i < m; ++ i) cin >> b[i].fi >> b[i].se, cb += b[i].se - b[i].fi;
sort(all(a)), sort(all(b));
auto pa = [&](pii x) {
return ca ? (ld) (x.se - x.fi) / ca : (ld) 1 / n;
};
auto pb = [&](pii x) {
return cb ? (ld) (x.se - x.fi) / cb : (ld) 1 / m;
};
auto w = [&](pii x) {
return x.fi + (ld) (x.se - x.fi) / 2;
};
vector<ld> sp(m+1, 0), swp(m+1, 0);
for (int i = 0; i < m; ++ i) {
sp[i+1] = sp[i] + pb(b[i]);
swp[i+1] = swp[i] + w(b[i]) * pb(b[i]);
}
ld res = 0;
for (int i = 0, l = 0, r = 0; i < n; ++ i) {
while (l < n && b[l].se < a[i].fi) ++ l;
while (r < n && b[r].fi <= a[i].se) ++ r;
res += (w(a[i]) * sp[l] - swp[l]) * pa(a[i]);
res += ((swp[m] - swp[r]) - w(a[i]) * (sp[m] - sp[r])) * pa(a[i]);
queue<pair<pii, pii>> cur;
for (int j = l; j < r; ++ j) cur.emplace(a[i], b[j]);
while (!cur.empty()) {
auto [u, v] = cur.front(); cur.pop();
if (u.fi == v.fi && u.se == v.se) {
ld d = ca ? u.se - u.fi : 1;
res += d * pa(u) * pb(v) / 3;
continue;
}
if (u.fi < v.fi && v.se < u.se) {
cur.emplace(pii(u.fi, v.fi), v);
cur.emplace(pii(v.se, u.se), v);
cur.emplace(v, v);
continue;
}
if (v.fi < u.fi && v.se > u.fi) {
cur.emplace(u, pii(v.fi, u.fi));
cur.emplace(u, pii(u.fi, v.se));
continue;
}
if (v.se > u.se && v.fi < u.se) {
cur.emplace(u, pii(u.se, v.se));
cur.emplace(u, pii(v.fi, u.se));
continue;
}
res += abs(w(u) - w(v)) * pa(u) * pb(v);
}
}
cout << res << '\n';
}
// = (p0w0/q0) * p1/q1 - (p1w1/q1) * (p0/q0)
// = (w0 - w1) * p1 * p0
// m = (r^2-l^2)/(2(r-l)), r-l=s
// (s1*(r0^2-l0^2)/2-s0*(r1^2-l1^2)) * s0 * s1 / q0 / q1
// y*1/m - x(r)/s
// = (sy-xrm)/sm
// a/s0 b/s1
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3820kb
input:
1 1 0 1 0 1
output:
0.333333333333333333
result:
ok found '0.333333333', expected '0.333333333', error '0.000000000'
Test #2:
score: 0
Accepted
time: 0ms
memory: 4064kb
input:
1 1 0 1 1 1
output:
0.5
result:
ok found '0.500000000', expected '0.500000000', error '0.000000000'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3828kb
input:
1 1 -1000000000 1000000000 -1000000000 1000000000
output:
666666666.666666667
result:
ok found '666666666.666666627', expected '666666666.666666627', error '0.000000000'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3832kb
input:
1 1 -1000000000 0 0 1000000000
output:
1000000000
result:
ok found '1000000000.000000000', expected '1000000000.000000000', error '0.000000000'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3832kb
input:
1 1 -1000000000 -1000000000 -1000000000 1000000000
output:
1000000000
result:
ok found '1000000000.000000000', expected '1000000000.000000000', error '0.000000000'
Test #6:
score: 0
Accepted
time: 0ms
memory: 3988kb
input:
1 1 -999999999 1000000000 -1000000000 -1000000000
output:
1000000000.5
result:
ok found '1000000000.500000000', expected '1000000000.500000000', error '0.000000000'
Test #7:
score: 0
Accepted
time: 0ms
memory: 3992kb
input:
1 1 -1000000000 1000000000 -999999999 -999999999
output:
999999999
result:
ok found '999999999.000000000', expected '999999999.000000000', error '0.000000000'
Test #8:
score: 0
Accepted
time: 0ms
memory: 3828kb
input:
1 1 1000000000 1000000000 -1000000000 -1000000000
output:
2000000000
result:
ok found '2000000000.000000000', expected '2000000000.000000000', error '0.000000000'
Test #9:
score: -100
Wrong Answer
time: 1ms
memory: 3888kb
input:
1000 1000 -2175 -2174 -1068 -1065 -1721 -1718 777 834 1162 1169 -3529 -3524 3966 3993 1934 1952 -234 -223 -4967 -4947 8500 8510 5272 5276 -6048 -6033 -34 -22 700 705 -7890 -7886 5538 5543 4114 4126 -9201 -9162 -1521 -1519 -5103 -5100 439 441 993 997 -1684 -1680 -8413 -8404 6724 6728 -3242 -3239 2616...
output:
6717.11605374111006
result:
wrong answer 1st numbers differ - expected: '6717.1171457', found: '6717.1160537', error = '0.0000002'