当前位置: 代码迷 >> 综合 >> URAL 1990 Podracing(尺取法)
  详细解决方案

URAL 1990 Podracing(尺取法)

热度:73   发布时间:2023-12-08 10:49:14.0

题目链接:
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;
}