HDU Leading Robots (2020杭电多校)

题目链接:杭电多校2020
题目描述
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:
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.
输入
The input consists of several test cases. The first line of input consists of an integer T(1≤ T≤50), indicating the number of test cases. The first line of each test case consists of an integer N(0 < N≤ 50000), indicating the number of robots. Each of the following N lines consist of two integers: p,a (0 < p,a < 2^31) indicating a robot’s position and its acceleration speed.
输出
For each test case, output the number of possible leading robots on a separate line.
Sample Input
1
3
1 1
2 3
3 2
Sample Output
2
题目的大致意思就是机器人在不同的位置开始起跑,每个机器人的加速度不同,我们需要找出能够跑在最右边的机器人的个数。这题利用模拟就能够做出。
下面就是AC代码:

#include 
#define rep(i,a,b) for(int i=a;i
#define T int t ;cin >> t;while(t--)
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
inline ll gcd(ll a,ll b){return b == 0? a:gcd(b, a % b);}
const int maxn = 2e5 + 10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-11;
const ll mod = 1e9 + 7;
struct node{
	ll a,p ;
	bool operator < (const node &b) const{
		return a<b.a ||(a==b.a&&p<b.p ) ;
	}
}robot[maxn];
inline bool check(node x,node y,node z){
	return (x.p - y.p )*(y.a - z.a) >=(y.p - z.p )*(x.a - y.a ) ;
}
int s[maxn] ;
map<node,int> mp ;
int main(){
	T{
		mp.clear() ;
		int n ;scanf("%d",&n);
		for(int i = 0 ; i < n ; i++){
			scanf("%lld%lld",&robot[i].p,&robot[i].a);  //p代表机器人的位置a代表机器人的加速度 
			mp[robot[i]]++ ;
		}
		sort(robot,robot+n) ;  //对机器人按照加速度排序 
		int top = 0 ;
		for(int i = 0 ; i <n; i++){
			while(top > 0 && robot[s[top]].p <= robot[i].p ||top > 1&&check(robot[i],robot[s[top]],robot[s[top-1]]))
				top-- ;  //寻找第一个追上第一个的机器人 
				top++ ;
			s[top] = i ;
		}
		int ans = top ;
		for(int i = 1 ; i <= top ; i++)
			if(mp[robot[s[i]]] > 1) 
				ans-- ;
		cout << ans << endl ;
	} 
	return 0 ;
} 

你可能感兴趣的:(杭电多校,模拟)