列表

详情


NC233137. Chris and Road

描述

And while Mishka is enjoying her trip...

Chris is a little brown bear. No one knows, where and when he met Mishka, but for a long time they are together (excluding her current trip). However, best friends are important too. John is Chris' best friend.

Once walking with his friend, John gave Chris the following problem:

At the infinite horizontal road of width w, bounded by lines and , there is a bus moving, presented as a convex polygon of n vertices. The bus moves continuously with a constant speed of v in a straight Ox line in direction of decreasing x coordinates, thus in time of its points are changing. Formally, after time t each of x coordinates of its points will be decreased by vt.

There is a pedestrian in the point (0, 0), who can move only by a vertical pedestrian crossing, presented as a segment connecting points (0, 0) and (0, w) with any speed not exceeding u. Thus the pedestrian can move only in a straight line Oy in any direction with any speed not exceeding u and not leaving the road borders. The pedestrian can instantly change his speed, thus, for example, he can stop instantly.

Please look at the sample note picture for better understanding.

We consider the pedestrian is hit by the bus, if at any moment the point he is located in lies the bus polygon (this means that if the point lies on the polygon vertex or on its edge, the pedestrian is not hit by the bus).

You are given the bus position at the moment 0. Please help Chris determine minimum amount of time the pedestrian needs to cross the road and reach the point (0, w) and not to be hit by the bus.

输入描述

The first line of the input contains four integers  — the number of the bus polygon vertices, road width, bus speed and pedestrian speed respectively.

The next n lines describes polygon vertices in counter-clockwise order. i-th of them contains pair of integers x_i and — coordinates of i-th polygon point. It is guaranteed that the polygon is non-degenerate.

输出描述

Print the single real t — the time the pedestrian needs to croos the road and not to be hit by the bus. The answer is considered correct if its relative or absolute error doesn't exceed .

示例1

输入:

5 5 1 2
1 2
3 1
4 3
3 4
1 4

输出:

5.0000000000

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(g++ 7.5.0) 解法, 执行用时: 6ms, 内存消耗: 584K, 提交时间: 2022-11-15 16:31:03

#include<bits/stdc++.h>
using namespace std;

using db = long double;
using ll = long long;

constexpr db eps = 1e-12;

struct point{
	ll x, y;
	bool operator < (const point &a) const {
		return x==a.x ? y < a.y-eps : x < a.x - eps;
	}
	point operator + (const point &a) const{
		return {x+a.x, y+a.y};
	}
	point operator - (const point &a) const{
		return {x-a.x, y-a.y};
	}
	ll operator *(const point &a) const{
		return x*a.x + y*a.y;
	}
	ll operator ^(const point &a) const{
		return x*a.y - y*a.x;
	}
	int to_left(const point &a) const{
		const auto t = (*this)^a;
		return (t>eps) - (t<-eps);
	}
};

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

    int n,w,v,u; cin >> n >> w >> v >> u;
    vector<point> vec(n);
    for(auto &[x,y]: vec) cin >> x >> y;

    point uv = {v, u};

    vector<int> all(n, 0);
    for(size_t i = 0 ; i < n ; i ++){
    	all[i] = uv.to_left(vec[i]);
    }

    if(count(all.begin(), all.end(), 1)==0 or count(all.begin(), all.end(), -1)==0){
    	return cout << fixed << setprecision(10) << 1.0l * w / u, 0;
    }

    db ans = 0;
    for(auto &[x,y]: vec){
    	ans = max(ans, (db)x/v + (db)(w-y)/u);
    }
    cout << fixed << ' ' << setprecision(10) << ans;

	return 0;
}

C++ 解法, 执行用时: 7ms, 内存消耗: 556K, 提交时间: 2022-02-12 01:40:03

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    double x, y, l = 999999999.0, r = 0, w, u, v;
    scanf("%d%lf%lf%lf", &n, &w, &v, &u);
    for(int i = 1; i <= n; i++)
    {
        scanf("%lf%lf", &x, &y);
        l = min(l, x - y / u * v);
        r = max(r, x - y / u * v);
    }
    double ans = w / u;
    if(l < 0)
        ans += r / v;
    printf("%.10lf", ans);
    return 0;
}

上一题