QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#366591 | #7846. Glacier Travel | kevinyang# | WA | 3ms | 4080kb | C++17 | 7.3kb | 2024-03-25 05:49:23 | 2024-03-25 05:49:24 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
#define double long double
double eps = 1e-9;
template <class T> int sgn(T x) { return (x > 0) - (x < 0); }
template<class T>
struct Point {
typedef Point P;
T x, y;
explicit Point(T x=0, T y=0) : x(x), y(y) {}
bool operator<(P p) const { return tie(x,y) < tie(p.x,p.y); }
bool operator==(P p) const { return tie(x,y)==tie(p.x,p.y); }
P operator+(P p) const { return P(x+p.x, y+p.y); }
P operator-(P p) const { return P(x-p.x, y-p.y); }
P operator*(T d) const { return P(x*d, y*d); }
P operator/(T d) const { return P(x/d, y/d); }
T dot(P p) const { return x*p.x + y*p.y; }
T cross(P p) const { return x*p.y - y*p.x; }
T cross(P a, P b) const { return (a-*this).cross(b-*this); }
T dist2() const { return x*x + y*y; }
double dist() const { return sqrt((double)dist2()); }
// angle to x-axis in interval [-pi, pi]
double angle() const { return atan2(y, x); }
P unit() const { return *this/dist(); } // makes dist()=1
P perp() const { return P(-y, x); } // rotates +90 degrees
P normal() const { return perp().unit(); }
// returns point rotated 'a' radians ccw around the origin
P rotate(double a) const {
return P(x*cos(a)-y*sin(a),x*sin(a)+y*cos(a)); }
friend ostream& operator<<(ostream& os, P p) {
return os << "(" << p.x << "," << p.y << ")"; }
};
typedef Point<double> P;
template<class P> bool onSegment(P s, P e, P p) {
return p.cross(s, e) == 0 && (s - p).dot(e - p) <= 0;
}
double segDist(P& s, P& e, P& p) {
if (s==e) return (p-s).dist();
auto d = (e-s).dist2(), t = min(d,max((double)0.0,(p-s).dot(e-s)));
return ((p-s)*d-(e-s)*t).dist()/d;
}
template<class P>
double lineDist(const P& a, const P& b, const P& p) {
return (double)(b-a).cross(p-a)/(b-a).dist();
}
template<class P>
pair<int, P> lineInter(P s1, P e1, P s2, P e2) {
auto d = (e1 - s1).cross(e2 - s2);
if (d == 0) // if parallel
return {-(s1.cross(e1, s2) == 0), P(0, 0)};
auto p = s2.cross(e1, e2), q = s2.cross(e2, s1);
return {1, (s1 * p + e1 * q) / d};
}
P match(P a, P b, P c, P d){
double dist = (d - c).dist();
return (b-a).unit() * dist + a;
}
pair<P, P> isoceles(P a, P b, P c, P d){
//cout << a << ' ' << b << ' ' << c << ' ' << d << '\n';
auto m1 = (a + d)/2.0;
auto m2 = (c + b)/2.0;
auto m = lineInter(m1,m2,a,c).second;
auto mperpvec = (m1-m2).perp();
auto mperp = m+mperpvec;
if((b-c).dist() < eps){
return make_pair((a+b)/2.0,(c+d)/2.0);
}
auto p1 = lineInter(m,mperp,a,b).second;
auto p2 = lineInter(m,mperp,c,d).second;
// cout << m1 << ' ' << m2 << '\n';
// cout << mperp << '\n';
//cout << p1 << ' ' << p2 << '\n';
// cout << (p1-p2).dist() << '\n';
return {p1,p2};
}
double solve(vector<P>a, vector<Point<int>>b, int n, double k){
vector<double>psa(n+1);
for(int i = 2; i<=n; i++){
psa[i] = psa[i-1] + (a[i]-a[i-1]).dist();
}
double ans = 1e18;
int r = 1;
for(int i = 1; i<=n; i++){
while(r <= n && psa[r] - psa[i] < k){
r++;
}
if(r==n+1){
break;
}
double rq = k - (psa[r-1] - psa[i]);
P np = (a[r] - a[r-1]).unit()*rq + a[r-1];
ans = min(ans,(np-a[i]).dist());
if(i + 1 < r){
auto res = lineInter(a[i],a[i+1],a[r-1],a[r]);
if(res.first == 0 || res.first == -1){
auto vec1 = a[i+1]-a[i];
auto vec2 = a[r]-a[r-1];
if(sgn(vec1.x) == sgn(vec2.x) && sgn(vec1.y) == sgn(vec2.y)){ // same direction
continue;
}
else{
auto p = isoceles(a[i],match(a[i],a[i+1],np,a[r]),np,a[r]);
if(segDist(a[i],a[i+1],p.first) < eps && segDist(np,a[r],p.second) < eps){
ans = min(ans,(p.first-p.second).dist());
}
}
}
else{
P mid = res.second;
double dist1 = (a[i]-mid).dist() - (a[i+1]-mid).dist();
double dist2 = (a[r-1]-mid).dist() - (a[r] - mid).dist();
if(sgn(dist1) != sgn(dist2)){
auto p = isoceles(a[i],match(a[i],a[i+1],np,a[r]),np,a[r]);
if(segDist(a[i],a[i+1],p.first) < eps && segDist(np,a[r],p.second) < eps){
ans = min(ans,(p.first-p.second).dist());
}
}
}
}
//cout << i << ' ' << r << '\n';
//cout << np << '\n';
}
return ans;
}
double solve2(vector<P>a, vector<Point<int>>b, int n, double k){
vector<double>psa(n+1);
for(int i = 2; i<=n; i++){
psa[i] = psa[i-1] + (a[i]-a[i-1]).dist();
}
double ans = 1e18;
int l = 1;
for(int i = 1; i<n; i++){
while(l < i && psa[i]-psa[l] > k){
l++;
}
if(l == 1)continue;
double rq = k - (psa[i] - psa[l]);
P np = a[l] + (a[l-1]-a[l]).unit()*rq;
ans = min(ans,(np-a[i]).dist());
//cout << i << " : " << np << '\n';
auto res = lineInter(a[l-1],a[l],a[i],a[i+1]);
if(res.first == 0 || res.first == -1){
auto vec1 = a[l]-a[l-1];
auto vec2 = a[i+1]-a[i];
if(sgn(vec1.x) == sgn(vec2.x) && sgn(vec1.y) == sgn(vec2.y)){ // same direction
continue;
}
else{
auto p = isoceles(np,a[l],a[i],match(a[i],a[i+1],np,a[l]));
if(segDist(a[i],a[i+1],p.second) < eps && segDist(np,a[l],p.first) < eps){
ans = min(ans,(p.first-p.second).dist());
}
}
}
else{
P mid = res.second;
double dist1 = (a[l-1]-mid).dist() - (a[l]-mid).dist();
double dist2 = (a[i]-mid).dist() - (a[i+1] - mid).dist();
if(sgn(dist1) != sgn(dist2)){
auto p = isoceles(np,a[l],a[i],match(a[i],a[i+1],np,a[l]));
if(segDist(a[i],a[i+1],p.second) < eps && segDist(np,a[l],p.first) < eps){
ans = min(ans,(p.first-p.second).dist());
}
}
}
}
return ans;
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
double s;
cin >> s;
int n;
cin >> n;
vector<P>a(n+1);
vector<Point<int>>b(n+1);
for(int i = 1; i<=n; i++){
int x,y;
double dx,dy;
cin >> x >> y;
dx = x;
dy = y;
b[i] = Point<int>{x,y};
a[i] = P{dx,dy};
}
double ans1 = solve(a,b,n,s);
//cout << ans1 << '\n' << '\n';
double ans2 = solve2(a,b,n,s);
cout << fixed << setprecision(10);
//cout << ans2 << '\n';
reverse(a.begin()+1,a.end());
reverse(b.begin()+1,b.end());
ans1 = min(ans1, solve(a,b,n,s));
ans2 = min(ans2, solve2(a,b,n,s));
//cout << "gay\n";
/*
line segment i is notated by a[i-1] to a[i]
*/
cout << min(ans1, ans2) << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3824kb
input:
5 4 20 0 10 0 10 10 0 10
output:
3.5355339059
result:
ok found '3.53553', expected '3.53553', error '0.00000'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3828kb
input:
3.16227766 9 -2 4 2 4 3 1 4 4 5 1 6 4 10 2 6 1 7 4
output:
0.9999999999
result:
ok found '1.00000', expected '1.00000', error '0.00000'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3824kb
input:
20 5 9 38 36 16 -5 36 -24 15 30 37
output:
2.2935957604
result:
ok found '2.29360', expected '2.29360', error '0.00000'
Test #4:
score: 0
Accepted
time: 0ms
memory: 4080kb
input:
10 40 17 18 12 -5 12 -16 -10 16 7 -15 18 -18 19 15 -19 1 -18 11 -8 -12 -17 -12 5 -12 -15 -8 -10 -10 -4 4 -2 -3 15 17 -2 -9 -13 7 -12 17 15 -3 -19 -14 6 6 14 -5 -10 -15 17 -16 -11 15 9 -6 10 8 19 -1 12 -6 -18 2 14 17 9 -7 -8 -3 7 11 -12 -14 -19 4 -1 15 -17 16
output:
0.0000000000
result:
ok found '0.00000', expected '0.00000', error '-0.00000'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3944kb
input:
10 40 10 11 11 15 6 -16 -18 4 10 10 -10 16 -17 11 2 6 -6 -9 17 -7 -7 -5 10 -18 -9 9 -14 10 19 -3 -14 -3 15 -5 -3 16 -10 14 -9 -12 10 -18 10 -4 -9 -11 11 -2 9 2 12 15 2 -17 -8 -16 19 7 -19 -2 -17 7 16 -9 6 -6 8 -18 15 9 17 2 -19 12 -15 -9 1 -15 19 -12
output:
0.3442144373
result:
ok found '0.34421', expected '0.34421', error '0.00000'
Test #6:
score: 0
Accepted
time: 3ms
memory: 4072kb
input:
1000 4000 -720538 -681604 667325 347504 -911397 -962007 -264075 -858319 49605 149209 964851 361021 -397274 28661 -460607 -273701 104862 -136807 -803899 -693703 -974 -337735 323678 -209811 -745617 -358684 -984333 603546 722843 -444579 701511 -255784 -676477 -836480 998942 -227888 -502059 -438394 9641...
output:
0.0445047436
result:
ok found '0.04450', expected '0.04450', error '0.00000'
Test #7:
score: 0
Accepted
time: 3ms
memory: 3904kb
input:
1000 4000 703 909 500 496 -214 628 914 -44 -330 -238 -1424 -1426 347 1420 332 -1417 877 1678 -1390 819 -921 525 121 -923 -851 -859 512 -151 765 -109 416 -1529 -269 667 475 -234 -708 300 1917 -1935 -909 1342 1141 -696 -272 1295 1908 -1124 -898 -1225 1608 630 -1937 1246 -255 1021 -1005 -1650 -1595 -39...
output:
0.0381857100
result:
ok found '0.03819', expected '0.03819', error '0.00000'
Test #8:
score: 0
Accepted
time: 0ms
memory: 3784kb
input:
40 3 100 0 0 100 0 0
output:
15.3073372946
result:
ok found '15.30734', expected '15.30734', error '0.00000'
Test #9:
score: -100
Wrong Answer
time: 0ms
memory: 3824kb
input:
48.2842712475 4 -10 -10 10 10 -10 10 10 -10
output:
20.0000000000
result:
wrong answer 1st numbers differ - expected: '0.00000', found: '20.00000', error = '20.00000'