QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#485737 | #8082. Minimum Euclidean Distance | zttttt | WA | 0ms | 3796kb | C++23 | 4.7kb | 2024-07-21 03:35:05 | 2024-07-21 03:35:05 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=5010;
int aa[N],bb[N],cc[N];
pair<int,int>p[N];
//struct S}{
// int x1,y1,x2,y1;
//}s[N];
int xx[N],yy[N];
signed main(){
int n,q;
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>p[i].first>>p[i].second;
}
for(int i=2;i<=n;i++){
xx[i]=p[i].first-p[i-1].first;
}
xx[1]=p[1].first-p[n].first;
//for(int i=1;i<=n;i++)cout<<i<<" "<<xx[i]<<endl;
for(int i=2;i<=n;i++){
yy[i]=p[i].second-p[i-1].second;
}
yy[1]=p[1].second-p[n].second;
//for(int i=1;i<=n;i++)cout<<i<<" "<<yy[i]<<endl;
for(int i=2;i<=n;i++){
aa[i]=p[i].second-p[i-1].second;
bb[i]=p[i-1].first-p[i].first;
cc[i]=p[i].first*p[i-1].second-p[i-1].first*p[i].second;
cc[i]*=-1;
}
aa[1]=p[1].second-p[n].second;
bb[1]=p[n].first-p[1].first;
cc[1]=p[1].first*p[n].second-p[n].first*p[1].second;
cc[1]*=-1;
// for(int i=2;i<=n;i++){
// bb[i]=p[i-1].second-xl[i]*p[i-1].first;
// }
// bb[1]=p[n].second-xl[1]*p[n].first;
for(int i=1;i<=q;i++){
int x1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;
double r=0.5*sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
//cout<<r<<endl;
double yxx=(x1+x2)*0.5;
//cout<<yxx<<endl;
double yxy=(y1+y2)*0.5;
//cout<<yxy<<endl;
int cntl=0,cntz=0,cntf=0;
for(int j=1;j<n;j++){
double xlx=yxx-p[j].first;
// cout<<j<<" "<<p[j].first<<endl;
// cout<<j<<" "<<yxx<<endl;
// cout<<j<<" "<<xlx<<endl;
double xly=yxy-p[j].second;
double cj=xx[j+1]*xly-yy[j+1]*xlx;
// cout<<j<<" "<<cj<<endl;
if(cj<0){
cntf++;
}else if(cj>0){
cntz++;
}else{
cntl++;
}
}
//cout<<cntf<<" "<<cntl<<" "<<cntz<<endl;
double xlx=yxx-p[n].first;
double xly=yxy-p[n].second;
double cj=xx[1]*xly-yy[1]*xlx;
if(cj<0){
cntf++;
}else if(cj>0){
cntz++;
}else{
cntl++;
}
if(cntl>0){
cout<<fixed<<setprecision(9);
cout<<0.5*r*r<<endl;
}else if(cntz==n){
cout<<fixed<<setprecision(9);
cout<<0.5*r*r<<endl;
}else{
double jl=100000000;
// for(int j=1;j<=n;j++){
// jl=min(jl,sqrt((yxx-p[j].first)*(yxx-p[j].first)+(yxy-p[j].second)*(yxy-p[j].second)));
// }
// for(int j=1;j<=n;j++){
// jl=min(jl,)
// }
int flag=0;
for(int j=2;j<=n;j++){
int aaa=bb[j],bbb=-aa[j];
double ccc=aa[j]*yxy-bb[j]*yxx;
double jdx=1.0*(-cc[j]*bbb+bb[j]*ccc)/(aa[j]*bbb-bb[j]*aaa);
double jdy=1.0*(aa[j]*(-ccc)+cc[j]*aaa)/(aa[j]*bbb-bb[j]*aaa);
//cout<<j<<" "<<jdx<<endl;
// if(fabs(sqrt((yxx-jdx)*(yxx-jdx)+(yxy-jdy)*(yxy-jdy))-1946708139892914)<1e-6){
// cout<<yxx<<" "<<yxy<<" "<<p[j-1].first<<" "<<p[j-1].second<<" "<<p[j].first<<" "<<p[j].second<<" "<<jdx<<" "<<jdy<<endl;
// flag=1;
// }
if((jdx>min(p[j-1].first,p[j].first)||fabs(jdx-min(p[j-1].first,p[j].first))<1e-6)&&(jdx<max(p[j-1].first,p[j].first)||fabs(jdx-max(p[j-1].first,p[j].first))<1e-6)&&(jdy>min(p[j-1].second,p[j].second)||fabs(jdy-min(p[j-1].second,p[j].second))<1e-6)&&(jdy<max(p[j-1].second,p[j].second)||fabs(jdy-max(p[j-1].second,p[j].second))<1e-6)){
jl=min(jl,sqrt((yxx-jdx)*(yxx-jdx)+(yxy-jdy)*(yxy-jdy)));
// if(fabs(jl-1946966666535562.75)<1e-6){
// cout<<yxx<<" "<<yxy<<" "<<p[j-1].first<<" "<<p[j-1].second<<" "<<p[j].first<<" "<<p[j].second<<" "<<jdx<<" "<<jdy<<endl;
// flag=1;
// break;
// }
}
}
if(flag)continue;
int aaa=bb[1],bbb=-aa[1];
double ccc=aa[1]*yxy-bb[1]*yxx;
double jdx=1.0*(-cc[1]*bbb+bb[1]*ccc)/(aa[1]*bbb-bb[1]*aaa);
double jdy=1.0*(aa[1]*(-ccc)+cc[1]*aaa)/(aa[1]*bbb-bb[1]*aaa);
// if(fabs(sqrt((yxx-jdx)*(yxx-jdx)+(yxy-jdy)*(yxy-jdy))-1946708139892914)<1e-6){
// cout<<0<<endl;
// flag=1;
// }
if((jdx>min(p[1].first,p[n].first)||fabs(jdx-min(p[1].first,p[n].first))<1e-6)&&(jdx<max(p[1].first,p[n].first)||fabs(jdx-max(p[1].first,p[n].first))<1e-6)&&(jdy>min(p[1].second,p[n].second)||fabs(jdy-min(p[1].second,p[n].second))<1e-6)&&(jdy<max(p[1].second,p[n].second)||fabs(jdy-max(p[1].second,p[n].second))<1e-6)){
jl=min(jl,sqrt((yxx-jdx)*(yxx-jdx)+(yxy-jdy)*(yxy-jdy)));
// if(fabs(jl-1946966666535562.75)<1e-6){
// cout<<0<<endl;
// flag=1;
// break;
// }
}
if(flag)continue;
//cout<<jl<<endl;
for(int i=1;i<=n;i++){
jl=min(jl,sqrt((yxx-p[i].first)*(yxx-p[i].first)+(yxy-p[i].second)*(yxy-p[i].second)));
}
// if(fabs(0.5*r*r+jl*jl-1946966666535993)<1e-6){
// cout<<0.5*r*r<<endl;
// continue;
// }
cout<<0.5*r*r+jl*jl<<endl;
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3796kb
input:
4 3 0 0 1 0 1 1 0 1 0 0 1 1 1 1 2 2 1 1 2 3
output:
0.250000000 0.750000000 1.875000000
result:
ok Your answer is acceptable!^ ^
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 3780kb
input:
48 10 -30 0 -29 -4 -28 -7 -27 -9 -25 -12 -22 -16 -21 -17 -17 -20 -14 -22 -12 -23 -9 -24 -5 -25 -4 -25 0 -24 3 -23 5 -22 8 -20 12 -17 13 -16 16 -12 18 -9 19 -7 20 -4 21 0 21 1 20 5 19 8 18 10 16 13 13 17 12 18 8 21 5 23 3 24 0 25 -4 26 -5 26 -9 25 -12 24 -14 23 -17 21 -21 18 -22 17 -25 13 -27 10 -28 ...
output:
589.5 52 1051.250000000 66.625000000 174.375000000 562.875000000 272.875000000 287.875000000 689.625000000 436.250000000
result:
wrong answer Except 51.470588235300, but found 52.000000000000!QAQ