QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#367542#7846. Glacier TravelsquishybananaAC ✓16ms4008kbC++233.9kb2024-03-26 05:52:592024-03-26 05:53:00

Judging History

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

  • [2024-03-26 05:53:00]
  • 评测
  • 测评结果:AC
  • 用时:16ms
  • 内存:4008kb
  • [2024-03-26 05:52:59]
  • 提交

answer

#include <iostream>
#include <vector>
#include <cmath>
#include <functional>
#include <iomanip>
using namespace std;
using ld = long double;
using pld = pair<ld,ld>;
using pii = pair<int, int>;
const ld 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; }
	ld dist() const { return sqrtl((ld)dist2()); }
	// angle to x-axis in interval [-pi, pi]
	ld 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(ld 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 << ")"; }
};
template<class P>
vector<P> circleLine(P c, ld r, P a, P b) {
	P ab = b - a, p = a + ab * (c-a).dot(ab) / ab.dist2();
	ld s = a.cross(b, c), h2 = r*r - s*s / ab.dist2();
	if (h2 < 0) return {};
	if (h2 == 0) return {p};
	P h = ab.unit() * sqrtl(h2);
	return {p - h, p + h};
}

ld gss(ld a, ld b, function<ld(ld)> f) {
	ld r = (sqrtl(5)-1)/2, eps = 1e-8;
	ld x1 = b - r*(b-a), x2 = a + r*(b-a);
	ld f1 = f(x1), f2 = f(x2);
	while (b-a > eps) {
		if (f1 < f2) { //change to > to find maximum
			b = x2; x2 = x1; f2 = f1;
			x1 = b - r*(b-a); f1 = f(x1);
		} else {
			a = x1; x1 = x2; f1 = f2;
			x2 = a + r*(b-a); f2 = f(x2);
		}
	}
	return a;
}

ld s;

int n;
vector<Point<ld>> p;
int main() {
	cin >> s;
	s -= eps;
	cin >> n;
	p.resize(n);
	for (auto &[i, j] : p) {
		int x, y ; cin >> x >> y;
		i = x;j = y;
	}
	for (int i = n-1; i >= 0; i--) {
		p[i] = p[i] - p[0];
	}


	Point<ld> me = p[0], partner;
	bool started = false;
	int i = 0, j = -1;
	const ld inf = 1e18;
	ld ans = inf;
	bool bum = 0;
	ld trav = 0;
	while (i < n-1) {
		if (started) {
			ans = min((me-partner).dist(), ans);
		}
		int stepType = -1;

		ld nextT = inf;
		if (i < n-1) {
			ld o = (me-p[i+1]).dist2();
			if (o < nextT) {
				stepType = 0;
				nextT = o;
			}
		}

		if (started) {
			ld o = (partner - p[j+1]).dist2();
			if (o < nextT) {
				nextT = o;
				stepType = 1;
			}
			auto partnerDir = (p[j+1]-p[j]).unit();
			auto dir = (p[i+1]-p[i]).unit();

			if (!bum) {
				function<ld(ld)> dis = [&](ld t) ->ld {
					return (me + dir*t - partner - partnerDir*t).dist2();
				};
				// weird shit
				o = gss(-1, min((p[j+1]-p[j]).dist(), (p[i+1]-p[i]).dist()), dis);
				if (o*o < nextT && o > 0) {
					nextT = o*o;
					stepType = 3;
				}
			}
		}
		else {
			ld dis = s-trav;
			ld o = (p[i+1]-p[i]).dist();
			if (dis < o && dis > 0) {
				o = dis*dis;
				if (o < nextT) {
					nextT = o;
					stepType = 2;
				}
			}
		}
		nextT = sqrtl(nextT);
		if (started) {
			auto partnerDir = (p[j+1]-p[j]).unit();
			partner = partner+partnerDir*nextT;
		}
		auto dir = (p[i+1]-p[i]).unit();
		trav += nextT;
		me = me+dir*nextT;
		if (stepType == 0) {
			i++;
			me = p[i];
		}
		else if (stepType == 1) {
			j++;
			partner = p[j];
		}
		else if (stepType == 2) {
			j = 0;
			started = true;
			partner = p[0];
		}
		if (stepType == 3) {
			bum = true;
		}
		else {
			bum = false;
		}
	}

	cout << setprecision(20) << fixed << ans << endl;

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3940kb

input:

5
4
20 0
10 0
10 10
0 10

output:

3.53553390522563084578

result:

ok found '3.53553', expected '3.53553', error '0.00000'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3884kb

input:

3.16227766
9
-2 4
2 4
3 1
4 4
5 1
6 4
10 2
6 1
7 4

output:

0.99999999963052603518

result:

ok found '1.00000', expected '1.00000', error '0.00000'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3972kb

input:

20
5
9 38
36 16
-5 36
-24 15
30 37

output:

2.29359576024161754965

result:

ok found '2.29360', expected '2.29360', error '0.00000'

Test #4:

score: 0
Accepted
time: 0ms
memory: 3944kb

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

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

result:

ok found '0.34421', expected '0.34421', error '0.00000'

Test #6:

score: 0
Accepted
time: 16ms
memory: 4000kb

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

result:

ok found '0.04450', expected '0.04450', error '0.00000'

Test #7:

score: 0
Accepted
time: 10ms
memory: 4008kb

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

result:

ok found '0.03819', expected '0.03819', error '0.00000'

Test #8:

score: 0
Accepted
time: 0ms
memory: 3748kb

input:

40
3
100 0
0 100
0 0

output:

15.30733729422090743690

result:

ok found '15.30734', expected '15.30734', error '0.00000'

Test #9:

score: 0
Accepted
time: 0ms
memory: 3924kb

input:

48.2842712475
4
-10 -10
10 10
-10 10
10 -10

output:

0.00000000416219304864

result:

ok found '0.00000', expected '0.00000', error '0.00000'