QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#383128 | #7734. Intersection over Union | mshcherba | AC ✓ | 1228ms | 3992kb | C++20 | 3.7kb | 2024-04-08 23:20:46 | 2024-04-08 23:20:46 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef long double db;
void solve()
{
vector<PII> v(4);
for (auto& [x, y] : v)
cin >> x >> y;
if (v[0].F == v[1].F || v[0].S == v[1].S)
{
cout << "1\n";
return;
}
db a, b, alpha, tg, ctg, si, co;
bool ok = false;
FOR(i, 0, 4)
{
auto [x1, y1] = v[i];
auto [x2, y2] = v[(i + 1) % 4];
if (x1 < x2 && y1 < y2)
{
auto [x3, y3] = v[(i + 2) % 4];
a = hypot(x3 - x2, y3 - y2);
b = hypot(x2 - x1, y2 - y1);
tg = (db)(y2 - y1) / (x2 - x1);
ctg = 1 / tg;
co = 1 / sqrt(tg * tg + 1);
si = 1 / sqrt(ctg * ctg + 1);
ok = true;
}
}
assert(ok);
if (a * co > b * si)
{
swap(a, b);
}
// outer
auto f1 = [&](db w, db h)
{
db tr = (tg + ctg) * (w * w + h * h);
db inte = a * b - tr;
db uni = (a * si + b * co - 2 * w) * (a * co + b * si - 2 * h) + tr;
return inte / uni;
};
// inner
auto f2 = [&](db w, db h)
{
db tr = (b * co - h * ctg - w) * (b * si - w * tg - h);
db inte = (a * si + b * co - 2 * w) * (a * co + b * si - 2 * h) - tr;
db uni = a * b + tr;
return inte / uni;
};
auto f = [&](db w, db h)
{
if (w < a * si)
{
return (a * si - w) * ctg > h ? f1(w, h) : f2(w, h);
}
else
{
return w * tg - a * si * si / co > h ? f1(w, h) : f2(w, h);
}
};
const db c = (-1 + sqrt(5)) / 2;
auto g = [&](db w)
{
db l = max((db)0, w * tg - a * si * si / co), r = min((a * co + b * si) / 2, w * tg + a * co);
db m1 = r - c * (r - l), fm1 = f(w, m1),
m2 = l + c * (r - l), fm2 = f(w, m2);
FOR(it, 0, 60)
{
if (fm1 > fm2)
{
r = m2;
m2 = m1;
fm2 = fm1;
m1 = r - c * (r - l);
fm1 = f(w, m1);
}
else
{
l = m1;
m1 = m2;
fm1 = fm2;
m2 = l + c * (r - l);
fm2 = f(w, m2);
}
}
return f(w, (l + r) / 2);
};
db l = 0, r = (a * si + b * co) / 2;
db m1 = r - c * (r - l), gm1 = g(m1),
m2 = l + c * (r - l), gm2 = g(m2);
FOR(it, 0, 60)
{
if (gm1 > gm2)
{
r = m2;
m2 = m1;
gm2 = gm1;
m1 = r - c * (r - l);
gm1 = g(m1);
}
else
{
l = m1;
m1 = m2;
gm1 = gm2;
m2 = l + c * (r - l);
gm2 = g(m2);
}
}
cout << g((l + r) / 2) << "\n";
/*db ans = g((l + r) / 2), br = 0;
const int C = 1000;
FOR(i, 0, C)
{
db w = (a * si + b * co) / 2 * i / C;
FOR(j, 0, C)
{
db h = (a * co + b * si) / 2 * j / C;
br = max(br, f(w, h));
}
}
cout << ans << " " << br << endl;
assert(abs(br - ans) < 1e-3);*/
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(17);
int t;
cin >> t;
while (t--)
solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3900kb
input:
3 0 2 2 0 0 -2 -2 0 7 -2 9 -2 9 2 7 2 7 13 11 10 5 2 1 5
output:
0.70710678118654752 1 0.62384322483109367
result:
ok 3 numbers
Test #2:
score: 0
Accepted
time: 1187ms
memory: 3892kb
input:
10000 -568767734 379152673 -565681345 -54946093 -131582579 -51859704 -134668968 382239062 -194884120 -559906233 -194884142 -158042604 -998611400 -158042648 -998611378 -559906277 459335966 -945199065 478030260 -934243779 450535683 -887326546 431841389 -898281832 -483567491 491964356 -523827401 408140...
output:
0.99296541252647005 0.99999993156883114 0.59086981567862606 0.62177048491080392 0.57883238413512738 1 0.49999999737417562 0.68523458057386941 0.45596171976721496 0.50028739390208503 0.98029611059210510 0.49204357179154080 0.54403738870671092 0.38048010710523954 0.92311833176433285 0.8925587008596665...
result:
ok 10000 numbers
Test #3:
score: 0
Accepted
time: 2ms
memory: 3908kb
input:
11 -999999999 238255972 -999999998 238255969 1000000000 904922635 999999999 904922638 678595699 247253438 -168427985 235963055 -168327599 228431927 678696085 239722310 -192017652 -554548342 868151340 -554547158 868151544 -737211410 -192017448 -737212594 -999841167 -113051212 -112620642 -999956242 99...
output:
0.00003535596408262 0.50022088590642686 0.99999666287698491 0.69630788885775817 0.00003162327663726 0.28251770996713315 0.99122103253953508 0.16452921001040721 0.05818740240908305 0.13825759387592938 0.04744473217643556
result:
ok 11 numbers
Test #4:
score: 0
Accepted
time: 1137ms
memory: 3964kb
input:
10000 -948990367 928227410 -873396995 996000778 851933079 -928405843 776339707 -996179211 -109521044 -738814438 897130737 -601388427 761521924 391952296 -245129857 254526285 534464734 -606222047 863618110 -14372639 -542024234 767366629 -871177610 175517221 636708790 -567653754 157968528 -562763908 1...
output:
0.15078853544727064 0.88803120075968691 0.61262235796682012 0.98242536604426466 0.66847652347018971 0.99997801536379838 0.46532406104744507 1 0.95911969795504607 0.75733536202649659 0.69724693218880543 0.29259150548288865 0.03846304652408571 0.91821270985748746 0.93358475601810211 0.7057847626209763...
result:
ok 10000 numbers
Test #5:
score: 0
Accepted
time: 1228ms
memory: 3972kb
input:
10000 0 1 1 0 4 3 3 4 0 1 3 0 8 15 5 16 0 5 25 0 64 195 39 200 0 7 49 0 124 525 75 532 0 8 4 0 34 15 30 23 0 8 12 0 38 39 26 47 0 40 100 0 274 435 174 475 0 56 196 0 514 1113 318 1169 0 32 8 0 212 51 204 83 0 32 24 0 124 75 100 107 0 160 200 0 692 615 492 775 0 224 392 0 1172 1365 780 1589 0 1081897...
output:
0.50000000000000005 0.50000000000000003 0.50000000000000001 0.50000000000000003 0.50000000000000003 0.49999999999999997 0.49999999999999999 0.49999999999999999 0.50000000000000001 0.50000000000000000 0.50000000000000000 0.50000000000000003 0.49998061213560060 0.50897597947675029 0.50248280959887504 ...
result:
ok 10000 numbers
Test #6:
score: 0
Accepted
time: 1199ms
memory: 3992kb
input:
10000 0 1 1 0 2 1 1 2 -1000000000 -999999999 -999999999 -1000000000 1000000000 999999999 999999999 1000000000 -1000000000 -999999999 999999999 -1000000000 1000000000 999999999 -999999999 1000000000 -1000000000 0 0 -1000000000 1000000000 0 0 1000000000 0 1 1 0 1000000000 999999999 999999999 100000000...
output:
0.70710678118654752 0.00001581151330529 0.99999999950000000 0.70710678118654752 0.00002236092978758 0.99999999900000000 0.99912269159020068 0.09850178135278598 0.08441949174412754 0.99217257629802580 0.71050532478837426 0.99999997554761989 0.00077722908368440 0.00852625685312162 0.13347659945090750 ...
result:
ok 10000 numbers
Test #7:
score: 0
Accepted
time: 1191ms
memory: 3932kb
input:
10000 0 508076 762114 0 762124 15 10 508091 -998442427 -981568404 -498442427 -981568405 -498442425 18431595 -998442425 18431596 0 1 250000000 0 250000001 250000000 1 250000001 -999999999 -27628659 -999999998 -27628661 1000000000 972371338 999999999 972371340 -999999999 238255972 -999999998 238255969...
output:
0.00327047422434107 0.99999999750000000 0.99999999600000002 0.00002500031251445 0.00003535596408262 0.00004609878484481 0.00006800966511705 0.00010124740883690 0.00014578442315927 0.00021274310096927 0.00031329816943577 0.00046981768420957 0.00070469831716131 0.00105156389366913 0.00157771069405258 ...
result:
ok 10000 numbers
Extra Test:
score: 0
Extra Test Passed