Submission #38433391
Source Code Expand
#include <bits/stdc++.h>
template<class T>
using Point = std::pair<T, T>;
template<class T>
class ConvexHull{
public:
std::vector<Point<T>> lower;
std::vector<Point<T>> upper;
Point<T> sub(Point<T> a,Point<T> b){ // return a-b
return {a.first - b.first,a.second-b.second};
}
T cross(Point<T> a, Point<T> b, Point<T> c){ // (a-b) x (c-b)
auto [x0,y0] = sub(a,b);
auto [x1,y1] = sub(c,b);
return x1*y0 -x0*y1;
}
ConvexHull(): lower{}, upper{}{}
ConvexHull(Point<T> p): lower{p}, upper{p}{}
ConvexHull(std::vector<Point<T>>&p) {
sort(p.begin(),p.end());
{
std::vector<Point<T>> v=p; // 逆时针弧
for(Point<T>p : v){
while(lower.size() >= 2 && cross(lower.rbegin()[1], lower.back(), p) <= 0) lower.pop_back();
lower.push_back(p);
}
}
{
std::vector<Point<T>> v=p; // 顺时针弧
for(Point<T>p : v){
while(upper.size() >= 2 && cross(upper.rbegin()[1], upper.back(), p) >= 0) upper.pop_back();
upper.push_back(p);
}
}
}
ConvexHull(const ConvexHull<T>& a, const ConvexHull<T>& b){ // 两个凸包合并
{
std::vector<Point<T>> v; // 逆时针弧
std::merge(a.lower.begin(), a.lower.end(), b.lower.begin(), b.lower.end(), back_inserter(v)); // 本身a,b下标有序, merge 会按pair的比较的排序
for(Point<T> p : v){
while(lower.size() >= 2 && cross(lower.rbegin()[1], lower.back(), p) <= 0) lower.pop_back();
lower.push_back(p);
}
}
{
std::vector<Point<T>> v; // 顺时针弧
std::merge(a.upper.begin(), a.upper.end(), b.upper.begin(), b.upper.end(), back_inserter(v)); // 本身a,b下标有序, merge 会按pair的比较的排序
for(Point<T> p : v){
while(upper.size() >= 2 && cross(upper.rbegin()[1], upper.back(), p) >= 0) upper.pop_back();
upper.push_back(p);
}
}
}
};
// -- template --
using namespace std;
typedef long long ll;
#define rep(i,a,n) for (ll i=a;i<(ll)n;i++)
#define per(i,a,n) for (ll i=n;i-->(ll)a;)
ll read(){ll r;scanf("%lld",&r);return r;}
pair<ll,ll> xy[100010];
double dis(const pair<ll,ll> &p0,const pair<ll,ll>&p1){
ll dx=p0.first-p1.first;
ll dy=p0.second-p1.second;
return sqrt(dx*dx+dy*dy);
}
int main(){
int n=read();
rep(i,0,n) {
xy[i]={read(),read()};
}
xy[n]={read(),read()}; // S
xy[n+1]={read(),read()}; // T
vector<ConvexHull<ll>> S;
rep(i,0,n+2) S.push_back(xy[i]);
while(S.size() > 1){
rep(i,0,S.size()/2) S[i] = ConvexHull<ll>(S[i*2],S[i*2+1]);
if(S.size() & 1) S[S.size()/2] = S.back();
S.resize((S.size()+1)/2);
}
assert(S[0].lower[0] == S[0].upper[0]);
assert(S[0].lower.back() == S[0].upper.back());
vector<pair<ll,ll> > convex;
rep(i,0,S[0].lower.size()) convex.push_back(S[0].lower[i]);
per(i,1,S[0].upper.size()-1) convex.push_back(S[0].upper[i]);
auto inconvex = [&](const pair<ll,ll> p) -> int{
rep(i,0,size(convex)) if(convex[i]==p) return i;
return -1;
};
int s_i = inconvex(xy[n]);
int t_i = inconvex(xy[n+1]);
int sz=size(convex);
if(s_i != -1 and t_i != -1){
double ans0 = 0;
rep(i,1,size(convex)){
int i0=(s_i+i-1)%size(convex);
int i1=(s_i+i)%size(convex);
ans0 += dis(convex[i0],convex[i1]);
if(i1==t_i) break;
}
double ans1 = 0;
rep(i,1,size(convex)){
int i0=(sz+s_i-i+1)%sz;
int i1=(sz+s_i-i)%sz;
ans1 += dis(convex[i0],convex[i1]);
if(i1==t_i) break;
}
printf("%.15lf\n",min(ans0,ans1));
}else{
printf("%.15lf\n", dis(xy[n],xy[n+1]));
}
return 0;
}
Submission Info
Submission Time |
|
Task |
Ex - Don't Swim |
User |
cromarmot |
Language |
C++ (GCC 9.2.1) |
Score |
600 |
Code Size |
3600 Byte |
Status |
AC |
Exec Time |
87 ms |
Memory |
20420 KiB |
Compile Error
./Main.cpp: In function ‘ll read()’:
./Main.cpp:63:21: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
63 | ll read(){ll r;scanf("%lld",&r);return r;}
| ~~~~~^~~~~~~~~~~
Judge Result
Set Name |
Sample |
All |
after_contest |
Score / Max Score |
0 / 0 |
600 / 600 |
0 / 0 |
Status |
|
|
|
Set Name |
Test Cases |
Sample |
00_sample_01.txt, 00_sample_02.txt |
All |
00_sample_01.txt, 00_sample_02.txt, 01_test_01.txt, 01_test_02.txt, 01_test_03.txt, 01_test_04.txt, 01_test_05.txt, 01_test_06.txt, 01_test_07.txt, 01_test_08.txt, 01_test_09.txt, 01_test_10.txt, 01_test_11.txt, 01_test_12.txt, 01_test_13.txt, 01_test_14.txt, 01_test_15.txt, 01_test_16.txt, 01_test_17.txt, 01_test_18.txt, 01_test_19.txt, 01_test_20.txt, 01_test_21.txt, 01_test_22.txt, 01_test_23.txt, 01_test_24.txt, 01_test_25.txt, 01_test_26.txt, 01_test_27.txt, 01_test_28.txt, 01_test_29.txt, 01_test_30.txt, 01_test_31.txt, 01_test_32.txt, 01_test_33.txt, 01_test_34.txt, 01_test_35.txt, 01_test_36.txt, 01_test_37.txt, 01_test_38.txt, 01_test_39.txt, 01_test_40.txt, 01_test_41.txt, 01_test_42.txt, 01_test_43.txt, 01_test_44.txt, 01_test_45.txt, 01_test_46.txt, 01_test_47.txt, 01_test_48.txt, 01_test_49.txt, 01_test_50.txt, 01_test_51.txt, 01_test_52.txt, 01_test_53.txt, 01_test_54.txt, 01_test_55.txt, 01_test_56.txt, 01_test_57.txt, 01_test_58.txt, 01_test_59.txt, 01_test_60.txt, 01_test_61.txt, 01_test_62.txt, 01_test_63.txt, 01_test_64.txt, 01_test_65.txt, 01_test_66.txt, 01_test_67.txt, 01_test_68.txt, 01_test_69.txt, 01_test_70.txt, 01_test_71.txt, 01_test_72.txt, 01_test_73.txt, 01_test_74.txt, 01_test_75.txt |
after_contest |
02_after_contest_00.txt |
Case Name |
Status |
Exec Time |
Memory |
00_sample_01.txt |
AC |
7 ms |
3556 KiB |
00_sample_02.txt |
AC |
2 ms |
3548 KiB |
01_test_01.txt |
AC |
52 ms |
11316 KiB |
01_test_02.txt |
AC |
62 ms |
15664 KiB |
01_test_03.txt |
AC |
47 ms |
10708 KiB |
01_test_04.txt |
AC |
43 ms |
10956 KiB |
01_test_05.txt |
AC |
3 ms |
3716 KiB |
01_test_06.txt |
AC |
45 ms |
9516 KiB |
01_test_07.txt |
AC |
65 ms |
14616 KiB |
01_test_08.txt |
AC |
65 ms |
15336 KiB |
01_test_09.txt |
AC |
80 ms |
17128 KiB |
01_test_10.txt |
AC |
79 ms |
18156 KiB |
01_test_11.txt |
AC |
84 ms |
16624 KiB |
01_test_12.txt |
AC |
79 ms |
17384 KiB |
01_test_13.txt |
AC |
78 ms |
16268 KiB |
01_test_14.txt |
AC |
75 ms |
16484 KiB |
01_test_15.txt |
AC |
79 ms |
17920 KiB |
01_test_16.txt |
AC |
75 ms |
15928 KiB |
01_test_17.txt |
AC |
78 ms |
16268 KiB |
01_test_18.txt |
AC |
74 ms |
16880 KiB |
01_test_19.txt |
AC |
75 ms |
16992 KiB |
01_test_20.txt |
AC |
79 ms |
17236 KiB |
01_test_21.txt |
AC |
43 ms |
9292 KiB |
01_test_22.txt |
AC |
43 ms |
9336 KiB |
01_test_23.txt |
AC |
69 ms |
15144 KiB |
01_test_24.txt |
AC |
65 ms |
14984 KiB |
01_test_25.txt |
AC |
83 ms |
17680 KiB |
01_test_26.txt |
AC |
73 ms |
16480 KiB |
01_test_27.txt |
AC |
79 ms |
16756 KiB |
01_test_28.txt |
AC |
63 ms |
14536 KiB |
01_test_29.txt |
AC |
31 ms |
7580 KiB |
01_test_30.txt |
AC |
13 ms |
4552 KiB |
01_test_31.txt |
AC |
87 ms |
17332 KiB |
01_test_32.txt |
AC |
83 ms |
17172 KiB |
01_test_33.txt |
AC |
82 ms |
17324 KiB |
01_test_34.txt |
AC |
83 ms |
17364 KiB |
01_test_35.txt |
AC |
83 ms |
17864 KiB |
01_test_36.txt |
AC |
82 ms |
17112 KiB |
01_test_37.txt |
AC |
82 ms |
17892 KiB |
01_test_38.txt |
AC |
84 ms |
17120 KiB |
01_test_39.txt |
AC |
82 ms |
17228 KiB |
01_test_40.txt |
AC |
83 ms |
17284 KiB |
01_test_41.txt |
AC |
23 ms |
6220 KiB |
01_test_42.txt |
AC |
65 ms |
16212 KiB |
01_test_43.txt |
AC |
16 ms |
5112 KiB |
01_test_44.txt |
AC |
84 ms |
16808 KiB |
01_test_45.txt |
AC |
81 ms |
18564 KiB |
01_test_46.txt |
AC |
20 ms |
6428 KiB |
01_test_47.txt |
AC |
34 ms |
7640 KiB |
01_test_48.txt |
AC |
81 ms |
16836 KiB |
01_test_49.txt |
AC |
82 ms |
17860 KiB |
01_test_50.txt |
AC |
74 ms |
15388 KiB |
01_test_51.txt |
AC |
86 ms |
20392 KiB |
01_test_52.txt |
AC |
85 ms |
20344 KiB |
01_test_53.txt |
AC |
83 ms |
17228 KiB |
01_test_54.txt |
AC |
84 ms |
17256 KiB |
01_test_55.txt |
AC |
85 ms |
20420 KiB |
01_test_56.txt |
AC |
87 ms |
18440 KiB |
01_test_57.txt |
AC |
82 ms |
17360 KiB |
01_test_58.txt |
AC |
85 ms |
18708 KiB |
01_test_59.txt |
AC |
83 ms |
17160 KiB |
01_test_60.txt |
AC |
83 ms |
17116 KiB |
01_test_61.txt |
AC |
2 ms |
3528 KiB |
01_test_62.txt |
AC |
2 ms |
3680 KiB |
01_test_63.txt |
AC |
2 ms |
3672 KiB |
01_test_64.txt |
AC |
2 ms |
3528 KiB |
01_test_65.txt |
AC |
2 ms |
3616 KiB |
01_test_66.txt |
AC |
2 ms |
3680 KiB |
01_test_67.txt |
AC |
2 ms |
3676 KiB |
01_test_68.txt |
AC |
2 ms |
3596 KiB |
01_test_69.txt |
AC |
2 ms |
3676 KiB |
01_test_70.txt |
AC |
2 ms |
3532 KiB |
01_test_71.txt |
AC |
3 ms |
3684 KiB |
01_test_72.txt |
AC |
2 ms |
3500 KiB |
01_test_73.txt |
AC |
3 ms |
3528 KiB |
01_test_74.txt |
AC |
2 ms |
3572 KiB |
01_test_75.txt |
AC |
2 ms |
3532 KiB |
02_after_contest_00.txt |
AC |
2 ms |
3672 KiB |