QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#366586#7846. Glacier Travelkevinyang#WA 1ms3960kbC++177.3kb2024-03-25 05:48:302024-03-25 05:48:30

Judging History

你现在查看的是最新测评结果

  • [2024-03-25 05:48:30]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3960kb
  • [2024-03-25 05:48:30]
  • 提交

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';
    cout << min(ans1,ans2) << '\n';
    reverse(a.begin()+1,a.end());
    reverse(b.begin()+1,b.end());
    //cout << "gay\n";
    /*

    line segment i is notated by a[i-1] to a[i]
    */
    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3808kb

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: 1ms
memory: 3960kb

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: 3924kb

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: -100
Wrong Answer
time: 0ms
memory: 3800kb

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.1320876332

result:

wrong answer 1st numbers differ - expected: '0.00000', found: '0.13209', error = '0.13209'