hdu multi-school (1) 1009 (stack simulation, mathematics)

1009 Leading Robots

Leading Robots
question meaning: known robot v (t) = a ∗ tv(t)=a*tv(t)=at , existingnnn robots, given the current position of the robot to aposition positionp o s i t i o n , accelerationaaa . Q After an infinite period of time, how many robots have been the first?
Idea: There is no idea. In the end, the robot with the largest a must be ranked first. It may be clearer to draw them as lines on two-dimensional coordinates. I felt like I wanted to do sort, but I didn't make it in the end^^.

The main idea of ​​the topic: Give you the initial position and acceleration of each robot, and ask how many robots you have in total that are likely to become a leader robot (at the top). We first sort the position of each robot, according to the highest position, the highest acceleration, and then we use the stack to save the position of each robot that may become the leader, acceleration and its time to become the leader, then it is obvious that in the back If the acceleration is less than or equal to the current robot, then it is impossible for them to become leaders, so we can continue directly when we cycle. If the acceleration is large, then it must be able to catch up with the robot in front.

So next we have to calculate the time it takes for it to catch up with the previous robot. I hope everyone has learned physics well. There is a formula s = v 0 t + 1 2 at 2 s=v_0t+\frac{1}{2}at^2s=v0t+21at2 . Then it catches up with the world of the robot in front of it when their distance is the same, that is

p o s t a i l − p o s i + 0 t + 1 2 a t a i l t 2 = 0 t + 1 2 a i t 2 pos_{tail}-pos_i+0t+\frac{1}{2}a_{tail}t^2=0t+\frac{1}{2}a_it^2 postailposi+0 t+21atailt2=0 t+21ait2

So in order to make our results accurate, we change the formula to 1 2 t 2 = postail − posiai − atail \frac{1}{2}t^2=\frac{pos_{tail}-pos_i}{a_i -a_{tail}}21t2=aiatailpostailposi, Then we don’t need to change ttt is calculated, just use it to compare the size, so1 2 t 2 \frac{1}{2}t^221t2 can replace it completely, and then we will use the score to save each time.

Then if the time for the current robot to catch up with the top robot is less than or equal to the time for the top robot to become the leader, then we will perform a stack operation, which means that the robot on the top of the stack has been surpassed by someone before surpassing others.

Finally, we need to judge whether there are elements with the same position and the same acceleration in the stack. We can make a map to count this.

Code:

struct fenshu
{
    
    
	int fz, fm;
	bool operator <(const fenshu &a)const {
    
    
		return (LL)fz*a.fm < (LL)fm*a.fz;
	}
	bool operator <=(const fenshu &a)const {
    
    
		return (LL)fz*a.fm <= (LL)fm*a.fz;
	}
	bool operator ==(const fenshu &a)const {
    
    
		return (LL)fz*a.fm == (LL)fm*a.fz;
	}
}times[maxn];
map<pii, int > mp;
struct node {
    
    
	int p, a;
}cun[maxn];
bool cmp(node a,node b){
    
    
	if (a.p == b.p)return a.a > b.a;
	return a.p > b.p;
}
stack<int> houxuan;
stack<fenshu>q;
int main()
{
    
    
	int t, n, nw, ans, leader;
	int st, temp;
	pii p;
	sci(t);
	while (t--)
	{
    
    
		
		while (!q.empty())q.pop();
		while (!houxuan.empty())houxuan.pop();
		mp.clear();
		ans = 0;
		
		sci(n);
		for (int i = 1; i <= n; i++) {
    
    
			sci(cun[i].p);
			sci(cun[i].a);
			p.first = cun[i].p;
			p.second = cun[i].a;
			mp[p]++;
		}
		sort(cun + 1, cun + 1 + n, cmp);
		leader = 1;
		

		//int st;
		houxuan.push(1);
		q.push(fenshu{
    
     0,1 });
		for (int i = 2; i <= n; i++) {
    
    
			if (cun[i].a <= cun[houxuan.top()].a)continue;
			while ((!houxuan.empty()) && (fenshu {
    
     cun[houxuan.top()].p - cun[i].p, cun[i].a - cun[houxuan.top()].a } <= q.top())) {
    
    
				houxuan.pop();
				q.pop();
			}
			q.push(fenshu{
    
     cun[houxuan.top()].p - cun[i].p, cun[i].a - cun[houxuan.top()].a });
			houxuan.push(i);
		}
		while (!houxuan.empty())
		{
    
    
			p.first = cun[houxuan.top()].p;
			p.second = cun[houxuan.top()].a;
			if (mp[p] == 1)ans++;
			houxuan.pop();
		}
		printf("%d\n", ans);
	}
	return 0;
}

It seems that there is also a two-dimensional convex hull approach, not for the time being ^^
To be added

Guess you like

Origin blog.csdn.net/weixin_44986601/article/details/108690843