QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#121857 | #6517. Computational Geometry | Sorting# | WA | 5ms | 3528kb | C++20 | 2.9kb | 2023-07-08 22:25:43 | 2023-07-08 22:25:48 |
Judging History
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[(int)v.size() - 2], v.back()))
v.pop_back();
v.push_back(p[x]);
}
for(int x: vector<int>{r, l}){
while(v.size() >= 2 && onSegment(p[x], v[(int)v.size() - 2], v.back()))
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[j], p[nxt(i)]))
j = nxt(j);
if(nxt(j) == i)
continue;
if(onSegment(p[j], p[i], p[nxt(j)]))
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: 3504kb
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: 3528kb
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 954 1302 242 442 616 1548 1078 938 431 555 371 1222 1222 1994 712 586 858 624 885 575 1351 884 1315 407 938 670 1003 1455 579 3389 939 2735 1794 834 938 602 203 198 1666 617 1166 32...
result:
wrong answer 2nd numbers differ - expected: '1389', found: '1417'