【Codeforces Round 365 (Div 2)C】【贪心 极限阻碍思想】 Chris and Road 过马路不被凸包车碰撞的最小时间
2016-08-06 09:09
736 查看
C. Chris and Road
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 y = 0 and y = w,
there is a bus moving, presented as a convex polygon ofn 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 only x coordinates of
its points are changing. Formally, after time t each of x coordinates
of its points will be decreased byvt.
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 strictly
inside 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.
Input
The first line of the input contains four integers n, w, v, u (3 ≤ n ≤ 10 000, 1 ≤ w ≤ 109, 1 ≤ v, u ≤ 1000) —
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 xi and yi( - 109 ≤ xi ≤ 109, 0 ≤ yi ≤ w) —
coordinates of i-th polygon point. It is guaranteed that the polygon is non-degenerate.
Output
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 10 - 6.
Example
input
output
Note
Following image describes initial position in the first sample case:
![](http://codeforces.com/predownloaded/63/6b/636b3a29deae4428cbf36211370e1e33bfcaeeff.png)
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 0, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int n, w;
double u, v;
double x, y;
int main()
{
while (~scanf("%d%d%lf%lf", &n, &w, &v, &u))
{
bool early = 1;
bool late = 1;
double ans = 0;
for (int i = 1; i <= n; ++i)
{
scanf("%lf%lf", &x, &y);
if (x / v > y / u)early = 0;
if (x / v < y / u)late = 0;
gmax(ans, x / v + (w - y) / u);
}
if (early || late)ans = w / u;
printf("%.10f\n", ans);
}
return 0;
}
/*
【trick&&吐槽】
这道题看似是计算几何,实际其实是一个贪心问题。
【题意】
给你一个无线长的路,路的横坐标范围为[-∞,∞]
路的纵坐标范围为[0,w]
车速为v(稳定),人速为u(上限)
告诉你车子凸包每个点的坐标,
车从右往左开,人想从[0,0]到达[0,w],只能向上走。
问你人过马路不被车撞的最小时间。
【类型】
贪心
【分析】
这道题看似是计算几何,实际其实是一个贪心问题。
首先,如果没有该车的话,这个人达到(0,w)的最小时间显然是w/u.
然后,车的每个点到达OY轴都有一个时间x/v,同时到达一个相应位置y
如果这个人到达任意一个y的时间y/u比车都要早的话,那直接走过了
如果这个人到达任意一个y的时间y/u比车都要晚的话,那直接走过了
如果不是以上两种情况,说明这个人会被车阻碍。
那显然,max((w-y)/v + x/v)就是答案
【时间复杂度&&优化】
O(n)
*/
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 y = 0 and y = w,
there is a bus moving, presented as a convex polygon ofn 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 only x coordinates of
its points are changing. Formally, after time t each of x coordinates
of its points will be decreased byvt.
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 strictly
inside 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.
Input
The first line of the input contains four integers n, w, v, u (3 ≤ n ≤ 10 000, 1 ≤ w ≤ 109, 1 ≤ v, u ≤ 1000) —
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 xi and yi( - 109 ≤ xi ≤ 109, 0 ≤ yi ≤ w) —
coordinates of i-th polygon point. It is guaranteed that the polygon is non-degenerate.
Output
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 10 - 6.
Example
input
5 5 1 2 1 2 3 1 4 3 3 4 1 4
output
5.0000000000
Note
Following image describes initial position in the first sample case:
![](http://codeforces.com/predownloaded/63/6b/636b3a29deae4428cbf36211370e1e33bfcaeeff.png)
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 0, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int n, w;
double u, v;
double x, y;
int main()
{
while (~scanf("%d%d%lf%lf", &n, &w, &v, &u))
{
bool early = 1;
bool late = 1;
double ans = 0;
for (int i = 1; i <= n; ++i)
{
scanf("%lf%lf", &x, &y);
if (x / v > y / u)early = 0;
if (x / v < y / u)late = 0;
gmax(ans, x / v + (w - y) / u);
}
if (early || late)ans = w / u;
printf("%.10f\n", ans);
}
return 0;
}
/*
【trick&&吐槽】
这道题看似是计算几何,实际其实是一个贪心问题。
【题意】
给你一个无线长的路,路的横坐标范围为[-∞,∞]
路的纵坐标范围为[0,w]
车速为v(稳定),人速为u(上限)
告诉你车子凸包每个点的坐标,
车从右往左开,人想从[0,0]到达[0,w],只能向上走。
问你人过马路不被车撞的最小时间。
【类型】
贪心
【分析】
这道题看似是计算几何,实际其实是一个贪心问题。
首先,如果没有该车的话,这个人达到(0,w)的最小时间显然是w/u.
然后,车的每个点到达OY轴都有一个时间x/v,同时到达一个相应位置y
如果这个人到达任意一个y的时间y/u比车都要早的话,那直接走过了
如果这个人到达任意一个y的时间y/u比车都要晚的话,那直接走过了
如果不是以上两种情况,说明这个人会被车阻碍。
那显然,max((w-y)/v + x/v)就是答案
【时间复杂度&&优化】
O(n)
*/
相关文章推荐
- Codeforces Round #365 (Div. 2) C. Chris and Road
- 【Codeforces Round 326 (Div 2)C】【贪心】Duff and Weight Lifting 每次取数二的幂数最小取数次数
- 【Codeforces Round 365 (Div 2)E】【乘除法DP map映射 约数分解】Mishka and Divisors n个数中选最小数量使得乘积为K的倍数
- 【Codeforces Round 330 (Div 2)E】【贪心 暴力】Edo and Magnets 给定矩形最多去除m个,最小面积矩形使得覆盖所有小矩形重心
- 【Codeforces Round 324 (Div 2)E】【贪心 构造】Anton and Ira 全排列交换 最小距离成本
- 【Codeforces Round 365 (Div 2)D】【离线询问 树状数组 前驱思想】Mishka and Interesting sum 区间内出现次数偶数的数的异或和
- 【Codeforces Round 345 (Div 2) A】【贪心 水题】Joysticks 2个操纵杆一个充电器 最长蓄电时间
- Codeforces Round #324 (Div. 2) E. Anton and Ira(贪心)
- Codeforces Round #324 (Div. 2) C. Marina and Vasya(贪心)
- 【Codeforces Round 330 (Div 2)D】【计算几何 二分答案】Max and Bike 最小骑车距离使得圆上传感器很坐标位移为dis
- Codeforces Round #288 (Div. 2)C. Anya and Ghosts(模拟+贪心)
- 【Codeforces Round 274 (Div 2)C】【贪心】Exams a[i]位置写a[i]或b[i] 所有位置的数保证不下降的最早结束时间
- 【Codeforces Round 263 (Div 2)C】【贪心 哈弗曼思维】Appleman and Toastman 每个非1size子树延展为2子树的最大权
- 【Codeforces Round 273 (Div 2)B】【贪心】 Random Teams n人分m组,可形成的最小朋友数和最大朋友数
- Codeforces Round #228 (Div. 1) A. Fox and Box Accumulation 贪心
- Codeforces Round #331 (Div. 2)-Wilbur and Array(贪心模拟)
- Codeforces Round #292 (Div. 2) C. Drazil and Factorial(贪心YY)
- 【Codeforces Round 332 (Div 2)A】【水题】A. Patrick and Shopping 遍历三元环的最小成本
- Codeforces Round #253 Div2 D.Andrey and Problem 概率+贪心
- Codeforces Round #169 (Div. 2) D. Little Girl and Maximum XOR(贪心,中等)