URAL 1990 Podracing(尺取法)

题目链接:
URAL 1990 Podracing
题意:
左边有一条折线,右边有一条折线,两条折线的起点和终点的纵坐标相同,保证两条折线不相交,还有一些摄像头,一条线段平行x轴的线段从起点到终点,必须得在两条折线中间,并且不能碰到摄像头,问线段最长的长度
分析:
总体思路同一水平线上折线间线段取最大,所有水平线上取最小。
先计算从左折线所有顶点到右折线的最短距离,
再计算从右折线所有顶点到左折线的最短距离,
然后计算所有在两条折线之间的点同一水平线上的最短线段,需要注意折线间同一水平线有多个点的情况。

用g++交居然TLE,然后改用c++交就A了,以后再也不轻易用cin,cou了!

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iomanip>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
typedef long long ll;
const int MAX_N = 100010;
const double eps = 1e-6;

int n, m, q, total;

struct Point{
    double x, y;

    Point() {}
    Point(double _x, double _y) :x(_x), y(_y) {}
    bool operator < (const Point& rhs) const {
        if(y != rhs.y) return y < rhs.y;
        else return x < rhs.x;
    }
}lp[MAX_N], rp[MAX_N], point[MAX_N * 3];

inline double GetX(Point a, Point b, Point c)
{
    double res = (c.y - a.y) * (b.x - a.x) / (b.y - a.y)  + a.x;
    return res;
}

int main()
{
    IOS;
    while(cin >> n){
        int total = 0;
        for(int i = 0; i < n; i++){
            cin >> lp[i].x >> lp[i].y;
        }
        cin >> m;
        for(int i = 0; i < m; i++){
            cin >> rp[i].x >> rp[i].y;
        }
        int lid = 0, rid = 0;
        double ans = rp[0].x - lp[0].x, x1, x2;
        for(int i = 1; i < n; i++){
            while(rp[rid].y < lp[i].y) rid++;
            if(rp[rid].y == lp[i].y) x2 = rp[rid].x;
            else x2 = GetX(rp[rid - 1], rp[rid], lp[i]);
            ans = min(ans, x2 - lp[i].x);
        }
        for(int i = 1; i < m; i++){
            while(lp[lid].y < rp[i].y) lid++;
            if(lp[lid].y == rp[i].y) x1 = lp[lid].x;
            else x1 = GetX(lp[lid - 1], lp[lid], rp[i]);
            ans = min(ans, rp[i].x - x1);
        }
        lid = rid = 0;
        cin >> q;
        for(int i = 0; i < q; i++) {
            double tmpx, tmpy;
            cin >> tmpx >> tmpy;
            if(tmpy > lp[n - 1].y || tmpy < lp[0].y ) continue;
            while(lp[lid].y < tmpy) lid++;
            while(rp[rid].y < tmpy) rid++;
            if(lp[lid].y == tmpy) x1 = lp[lid].x;
            else x1 = GetX(lp[lid - 1], lp[lid], Point(tmpx, tmpy));
            if(rp[rid].y == tmpy) x2 = rp[rid].x;
            else x2 = GetX(rp[rid - 1], rp[rid], Point(tmpx, tmpy));
            if(tmpx < x1 || tmpx > x2 ) continue;
            point[total++] = Point(x1, tmpy);
            point[total++] = Point(tmpx, tmpy);
            point[total++] = Point(x2, tmpy);
        }
        sort(point, point + total);
        for(int i = 0; i < total; i++){
            double tmp = -1;
            while(i != total - 1 && point[i].y == point[i + 1].y){ 
                tmp = max (tmp, point[i + 1].x - point[i].x);
                i++;
            }
            if(tmp != -1) ans = min(ans, tmp);
        }
        cout << fixed << setprecision(10) << ans << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值