QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#121854#6517. Computational GeometrySorting#WA 5ms3532kbC++202.9kb2023-07-08 22:20:562023-07-08 22:21:00

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-08 22:21:00]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:3532kb
  • [2023-07-08 22:20:56]
  • 提交

answer

#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <numeric>
#include <utility>
#include <array>
#include <climits>

using namespace std;

typedef long long ll;

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);}
    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);}
    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;}
};

template<class P> bool onSegment(P s, P e, P p){
    return p.cross(s, e) == 0 && (s - p).dot(e - p) <= 0;
}

typedef Point<ll> P;
array<P, 2> hullDiameter(vector<P> S){
    int n = S.size(), j = n < 2 ? 0 : 1;
    pair<ll, array<P, 2>> res({0, {S[0], S[0]}});
    for(int i = 0; i < j; ++i){
        for(;;j = (j + 1) % n){
            res = max(res, {(S[i] - S[j]).dist2(), {S[i], S[j]}});
            if((S[(j + 1) % n] - S[j]).cross(S[i + 1] - S[i]) >=  0)
                break;
        }   
    }
    return res.second;
}

const int N = 5e3 + 3;
int n;
P p[N];

int nxt(int x){
    return (x + 1) % n;
}
int prv(int x){
    return (x + n - 1) % n;
}

ll diam(int l, int r){
    vector<P> v;
    for(int x = l; x != r; x = nxt(x)){
        while(v.size() >= 2 && onSegment(p[x], v.back(), v[(int)v.size() - 2]))
            v.pop_back();
        v.push_back(p[x]);
    }
    for(int x: vector<int>{r, l}){
        while(v.size() >= 2 && onSegment(p[x], v.back(), v[(int)v.size() - 2]))
            v.pop_back();
        v.push_back(p[x]);
    }
    v.pop_back();
    auto [p1, p2] = hullDiameter(v);
    return (p1 - p2).dist2();
}

void solve(){
    cin >> n;
    for(int i = 0; i < n; ++i){
        cin >> p[i].x >> p[i].y;
    }

    int j = 0;
    ll ans = LLONG_MAX;
    for(int i = 0; i < n; ++i){
        // cout << i << " " << j << " i j" << endl;
        while(i == j || nxt(i) == j)
            j = nxt(j);

        while(onSegment(p[i], p[nxt(i)], p[j]))
            j = nxt(j);

        if(nxt(j) == i)
            continue;

        if(onSegment(p[j], p[nxt(j)], p[i]))
            continue;

        ll d1 = diam(i, j);
        ll d2 = diam(j, i);
        ans = min(ans, d1 + d2);
        // cout << i << " " << j << " - " << d1 << " " << d2 << "\n";

        if(d1 < d2){
            j = nxt(j);
            i = i - 1;
            continue;
        }
        else{
            continue;
        }
    }

    cout << ans << "\n";
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int t;
    cin >> t;
    while(t--)
        solve();
}

詳細信息

Test #1:

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

input:

2
4
1 0
2 0
1 1
0 0
6
10 4
9 7
5 7
4 5
6 4
9 3

output:

4
44

result:

ok 2 number(s): "4 44"

Test #2:

score: -100
Wrong Answer
time: 5ms
memory: 3532kb

input:

713
8
8 25
3 15
0 5
10 0
19 2
24 6
23 15
15 34
8
25 16
18 25
10 32
1 23
0 14
21 0
27 2
32 6
7
16 15
8 20
1 16
0 12
16 0
21 1
24 5
7
15 1
18 0
24 8
27 15
4 19
0 17
7 8
4
10 20
0 30
15 0
14 10
6
15 0
24 10
21 14
12 14
7 11
0 3
7
18 7
16 9
12 10
6 9
0 4
5 0
15 1
9
0 23
8 13
14 6
24 0
34 1
41 11
37 20
1...

output:

1075
1417
706
687
1550
497
324
1668
494
162
519
190
786
983
367
1154
580
524
509
275
651
298
146
1618
494
965
705
1450
274
322
69
442
616
457
1078
175
431
555
371
1222
1222
1994
712
586
858
624
885
575
1351
27
222
407
703
670
1003
1455
579
3389
939
334
1794
834
938
602
203
198
1666
617
769
326
2553
...

result:

wrong answer 2nd numbers differ - expected: '1389', found: '1417'