半平面求交 - 洛谷 - P3256 [JLOI2013] 赛车

欢迎关注更多精彩
关注我,学习常用算法与数据结构,一题多解,降维打击。

往期相关背景点击前往

题目大意

题目链接
https://www.luogu.com.cn/problem/P3194

有一场赛车比赛,每辆车有自己的起位置和速度,比赛时间无限长。

问题是求有哪些车辆跑在最前面。

解析

在这里插入图片描述
在直角坐标系中画出直线代表每辆车的状态。

每个时刻,处在最高处的车辆就是最前在的车辆。

所有时刻组成的图形是一个半平在交,所有半平面交的边就是领先过的车辆。

由于比赛时刻是从0开始,需要加一条负Y轴来限制。

注意:有些车辆的起位置和速度是相同的,需要去重。

代码


#include<stdio.h>
#include<cmath>
#include <algorithm>
#include <vector>
#include <list>
#include <cstring>
#include <set>
#include <map>


using namespace std;
const double EPS = 1e-6;
const double EPS2 = 0;

const int N = 1e6 + 10;
const int M = 1e6 + 10;

int cmp(double d) {
	if (abs(d) < EPS)return 0;
	if (d > 0)return 1;
	return -1;
}

class Point {
public:
	double x, y;
	int id;

	Point() {}
	Point(double a, double b) :x(a), y(b) {}
	Point(const Point& p) :x(p.x), y(p.y), id(p.id) {}

	void in() {
		scanf("%lf %lf", &x, &y);
	}
	void out() {
		printf("%f %f\n", x, y);
	}

	double dis() {
		return sqrt(x * x + y * y);
	}

	double dis2() {
		return x * x + y * y;
	}

	Point operator -() const {
		return Point(-x, -y);
	}

	Point operator -(const Point& p) const {
		return Point(x - p.x, y - p.y);
	}

	Point operator +(const Point& p) const {
		return Point(x + p.x, y + p.y);
	}
	Point operator *(double d)const {
		return Point(x * d, y * d);
	}

	Point operator /(double d)const {
		return Point(x / d, y / d);
	}


	void operator -=(Point& p) {
		x -= p.x;
		y -= p.y;
	}

	void operator +=(Point& p) {
		x += p.x;
		y += p.y;
	}
	void operator *=(double d) {
		x *= d;
		y *= d;
	}

	void operator /=(double d) {
		this ->operator*= (1 / d);
	}

	bool operator<(const Point& a) const {
		return x < a.x || (abs(x - a.x) < EPS && y < a.y);
	}

	bool operator==(const Point& a) const {
		return abs(x - a.x) < EPS && abs(y - a.y) < EPS;
	}
};

// 向量操作

double cross(const Point& a, const Point& b) {
	return a.x * b.y - a.y * b.x;
}

double dot(const Point& a, const Point& b) {
	return a.x * b.x + a.y * b.y;
}


class Line {
public:
	Point front, tail;
	int ind;
	Line() {}
	Line(const Point &a, const Point &b) :front(a), tail(b) {}
	Line(const Point &a, const Point &b, int i) :front(a), tail(b), ind(i) {}
};

int cmp(const Line& a, const Line& b) {
	//auto ta = atan2(a.front.y - a.tail.y, a.front.x - a.tail.x);
	//auto tb = atan2(b.front.y - b.tail.y, b.front.x - b.tail.x);

	// return cmp(ta - tb);

	return cmp(a.front.y - a.tail.y - (b.front.y - b.tail.y));
}


// 点在直线哪一边>0 左边,<0边
int SideJudge(const Line& a, const Point& b) {
	if (cross(a.front - a.tail, b - a.tail) < 0)return -1;
	return 1;
}


int LineSort(const Line& a, const Line& b) {
	int c = cmp(a, b);
	if (c)return c < 0;
	return a.tail.y > b.tail.y;
}

/*
点p 到 p+r 表示线段1
点q 到 q+s 表示线段2
线段1 上1点用 p' = p+t*r (0<=t<=1)
线段2 上1点用 q' = q+u*s (0<=u<=1)
让两式相等求交点 p+t*r = q+u*s
两边都叉乘s
(p+t*r)Xs = (q+u*s)Xs
pXs + t*rXs = qXs
t = (q-p)Xs/(rXs)
同理,
u = (p-q)Xr/(sXr) -> u = (q-p)Xr/(rXs)

以下分4种情况:
1. 共线,sXr==0 && (q-p)Xr==0, 计算 (q-p)在r上的投影在r长度上的占比t0,
计算(q+s-p)在r上的投影在r长度上的占比t1,查看[t0, t1]是否与范围[0,1]有交集。
如果t0>t1, 则比较[t1, t0]是否与范围[0,1]有交集。
t0 = (q-p)*r/(r*r)
t1 = (q+s-p)*r/(r*r) = t0 + s · r / (r · r)
2. 平行sXr==0 && (q-p)Xr!=0
3. 0<=u<=1 && 0<=t<=1 有交点
4. 其他u, t不在0到范围内,没有交点。
*/
pair<double, double> intersection(const Point& q, const Point& s, const Point& p, const Point& r) {
	// 计算 (q-p)Xr
	auto qpr = cross(q - p, r);
	auto qps = cross(q - p, s);

	auto rXs = cross(r, s);
	if (cmp(rXs) == 0)return { -1, -1 }; // 平行或共线
	// 求解t, u
	// t = (q-p)Xs/(rXs)
	auto t = qps / rXs;

	// u = (q-p)Xr/(rXs)
	auto u = qpr / rXs;

	return { u, t };
}

Point LineCross(const Line &a, const Line &b) {
	Point dira = a.front - a.tail;
	Point dirb = b.front - b.tail;

	auto p = intersection(a.tail, dira, b.tail, dirb);
	return a.tail + dira*p.first;
}

class HalfPlane {
public:
	vector<Line> lines;

	void addLine(const Line &a) {
		lines.push_back(a);
	}

	set<int> run() {
		sort(lines.begin(), lines.end(), LineSort);
		vector<int> q(lines.size()+10);
		vector<Point> t(lines.size()+10);

		int l = -1, r = 0;
		q[0] = 0;
		for (int i = 1; i < lines.size(); ++i) {
			if (cmp(lines[i], lines[i - 1]) == 0)continue;
			while (r - l > 1 && SideJudge(lines[i], t[r]) < 0)r--;
			while (r - l > 1 && SideJudge(lines[i], t[l+2]) < 0)l++;
			q[++r] = i;
			t[r] = LineCross(lines[q[r]], lines[q[r - 1]]);
		}
		/*while (r - l > 1 && SideJudge(lines[q[l + 1]], t[r]) < 0)r--;
		t[r+1] = LineCross(lines[q[l+1]], lines[q[r]]);
		r++;*/
		// 统计交点
		/*
		l++;
		vector<Point> ans(r-l);
		for (int i = 0; i < ans.size(); ++i) {
			ans[i] = t[i + l + 1];
		}*/

		set<int> ans;
		for (int i = l + 1; i <= r; ++i) ans.insert(lines[q[i]].ind);
		return ans;
	}
};

Point oiPs[N];
int position[N], volocity[N];
int carBelong[N];

void  solve() {
	int n;
	scanf("%d", &n);
	HalfPlane hp;
	
	for (int i = 0; i < n; ++i) scanf("%d", position+i);
	for (int i = 0; i < n; ++i) scanf("%d", volocity+i);
	hp.addLine({ Point(0, -1), Point(0, 0), 0});
	map<pair<int, int>, int> numInd;

	for (int i = 0; i < n; ++i) {
		pair<int, int> ele(position[i], volocity[i]);
		if (numInd.count(ele) > 0) {
			carBelong[i] = numInd[ele];
			continue;
		}

		carBelong[i] = numInd.size() + 1;
		numInd[ele] = numInd.size() + 1;

		Line l(Point( 1, position[i]+volocity[i]), Point(0, position[i]), carBelong[i]);
		hp.addLine(l);
	}

	auto seeLine = hp.run();
	vector<int> ans;
	for (int i = 0; i < n; ++i) {
		if (seeLine.count(carBelong[i]))ans.push_back(i + 1);
	}

	printf("%d\n", ans.size());
	for (int i = 0; i < ans.size(); ++i) {
		if (i)putchar(' ');
		printf("%d", ans[i]);
	}
	puts("");
}


int main() {
	solve();
	return 0;

}

/*

*/


本人码农,希望通过自己的分享,让大家更容易学懂计算机知识。创作不易,帮忙点击公众号的链接。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值