题目链接:点击打开链接
题目描述:给出一个凸多边形,多边形以速度v向x轴负半轴移动,一个点以速度u从(0,0)移动到(0,w)。点可以在任意时刻停下。求点不与多边形相撞(不在多边形内)到达目的地的最短时间。
解题思路:
对于每个出现的y值,用一个map记录“(0,y)在多边形内的最后时间”。然后定义2个变量last、ans,分别代表点所在的纵坐标,以及到达这个纵坐标的最长时间(ans可能出现小于map[y]的情况,含义就是“多边形离开(0,y)前,点到达(0,y)”这种情况是不符合题意的,点进入了多边形内)。last初始为0,遍历map,每次更新last和ans,最后ans+(w-last)/u就是符合题意的答案了。
#include <set>
#include <map>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef __int64 LL;
typedef pair<int,int> PII;
#define FIN freopen("in.txt", "r", stdin);
#define FOUT freopen("out.txt", "w", stdout);
#define lson l, mid, cur << 1
#define rson mid + 1, r, cur << 1 | 1
//#pragma comment(linker, "/STACK:1024000000,1024000000")
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const double EXP = 1e-8;
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 50;
const int MAXM = 2e4 + 50;
int n, w, u, v;
struct point
{
double x, y;
}p[MAXN];
map<int, double> mp;
bool check()
{
for (int i = 0; i < n; i++)
if (p[i].y / u > p[i].x / v)
return false;
return true;
}
int main()
{
#ifdef LOCAL_NORTH
FIN;
#endif // LOCAL_NORTH
while (~scanf("%d%d%d%d", &n, &w, &v, &u))
{
mp.clear();
for (int i = 0; i < n; i++)
{
scanf("%lf%lf", &p[i].x, &p[i].y);
mp[p[i].y] = max(mp[p[i].y], p[i].x / v);
}
if (check())
{
printf("%.10lf\n", (double)w / u);
continue;
}
int last = 0;
double ans = 0;
for (map<int, double>::iterator it = mp.begin(); it != mp.end(); it++)
{
ans += (double)(it->first - last) / u;
ans = max(ans, it->second);
last = it->first;
}
ans += (double)(w - last) / u;
printf("%.10lf\n", ans);
}
return 0;
}
简化写法:
#include <set>
#include <map>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef __int64 LL;
typedef pair<int,int> PII;
#define FIN freopen("in.txt", "r", stdin);
#define FOUT freopen("out.txt", "w", stdout);
#define lson l, mid, cur << 1
#define rson mid + 1, r, cur << 1 | 1
//#pragma comment(linker, "/STACK:1024000000,1024000000")
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const double EXP = 1e-8;
const int MAXN = 1e5 + 50;
const int MAXM = 2e4 + 50;
const int MOD = 1e9 + 7;
int n, w, u, v;
struct point
{
double x, y;
}p[MAXN];
int main()
{
#ifndef ONLINE_JUDGE
FIN;
#endif // ONLINE_JUDGE
while (~scanf("%d%d%d%d", &n, &w, &v, &u))
{
bool ok = true;
double ans = 0;
for (int i = 0; i < n; i++)
{
scanf("%lf%lf", &p[i].x, &p[i].y);
if (p[i].y / u > p[i].x / v)
ok = false;
double tmp = max(p[i].y / u, p[i].x / v);
tmp += (w - p[i].y) / u;
ans = max(ans, tmp);
}
printf("%.10lf\n", ok ? (double)w / u : ans);
}
return 0;
}