Codeforces Round #365 (Div. 2) C. Chris and Road

题目链接:点击打开链接

题目描述:给出一个凸多边形,多边形以速度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;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值