2020杭电多校第一场Leading Robots

第九题:Leading Robots

题目:

Sandy likes to play with robots. He is going to organize a running competition between his robots. And he is going to give some presents to the winners. Robots are arranged in a line. They have their initial position (distance from the start line) and acceleration speed. These values might be different. When the competition starts, all robots move to the right with speed:
v(t)=a*t

Here a is acceleration speed and t is time from starting moment.

Now, the problem is that, how many robots can be the leader from the starting moment?

Here leader means the unique rightmost robot from the start line at some moment. That is, at some specific time, if a robot is rightmost and unique then it is the leading robot at that time. There can be robots with same initial position and same acceleration speed.

The runway is so long that you can assume there’s no finish line.

思路

先将机器人按照加速度从小到大排序,速度相同按照位置从小到大排序,那么我们就可知后面的机器人一定会超过前面的机器人或者一直在同一个位置,如果后面的机器人初始位置比前面的大,那么后面的机器人一定会比前面的先到第一,那么就把前面的机器人出栈,如果栈内有两个机器人a,b要加入一个机器人c, b追上a的时间是t1,c追上b的时间是t2,那么t2一定严格大于t1,否则的话b就需要出栈。然后再判断a前面的机器人和a和c能否同时存在。然后再判重就可以了,如果有两个机器人的初始位置和加速度一样,那么他们一定不能到达第一。

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;

#define x first
#define y second

const int N=50010;
struct node{
	double x,y;
}p1[N];

map<pii,int>mp;

int n,top,sta[N];

bool f1(node a,node b,node c){
	return (b.y-c.y)*(b.x-a.x)-(a.y-b.y)*(c.x-b.x)<=0;
}
bool cmp(node a,node b){
	if(a.x!=b.x) return a.x<b.x;
	return a.y<b.y;
}
int main(){
	
	int T;
	cin>>T;
	while(T--)
	{
		scanf("%d",&n);
		mp.clear();
		for(int i=1;i<=n;++i)
		{
			scanf("%lf%lf",&p1[i].y,&p1[i].x);
			pii p;
			p.y=p1[i].y,p.x=p1[i].x;
			mp[p]++;
		}
		sort(p1+1,p1+n+1,cmp);
		top=0;
		for(int i=1;i<=n;++i)
		{
			while((top>0&&p1[sta[top]].y<=p1[i].y)||(top>1&&f1(p1[sta[top-1]],p1[sta[top]],p1[i]))) --top;
			sta[++top]=i;
		}
		int ans=top;
		for(int i=1;i<=top;i++)
		{
			pii p;
			p.x=p1[sta[i]].x;
			p.y=p1[sta[i]].y;
			if(mp[p]>1) ans--;
		}
		printf("%d\n",ans);
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_44828887/article/details/107499606