HDU6759 Leading Robots(2020杭电多校训练第一场)

Leading Robots
HDU6759 Leading Robots
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1811 Accepted Submission(s): 504

Problem Description
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.

Input
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 < 231) indicating a robot’s position and its acceleration speed.

Output
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

题意:给出n个人的初始位置和加速度,运动方向都是向右,求有多少人可以作为leader。当某时刻一个人是唯一一个最领先的人,那么在那个独属的moment, 它就是leader。
在这里插入图片描述

Code:

#include<cmath>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<set>
#include<vector>
#include<string>
#include<map>
typedef long long ll;
using namespace std;
const double pi=4*atan(1.0);
const ll mod=1e9+7;
const int inf=0x3f3f3f3f;
using namespace std;
struct node
{
    ll p;
    ll a;
}x[100005];
bool cmp(node a,node b)
{
    if(a.a!=b.a)
        return a.a<b.a;
    else
        return a.p<b.p;
}
int same[100005];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            scanf("%lld %lld",&x[i].p,&x[i].a);
            x[i].p*=-2;
            same[i]=0;
        }
        sort(x,x+n,cmp);
        for(int i=1; i<n; i++)
        {
            if(x[i].p==x[i-1].p&&x[i].a==x[i-1].a)
                same[i]=same[i-1]=1;
        }
        int cnt=0;
        int q[100005];
        q[cnt++]=0;
        memset(q,0,sizeof(q));
        for(int i=1; i<n; i++)
        {
            int pre=q[cnt-1];
            if(x[i].a==x[pre].a)continue;//节约时间
            while(cnt>0&&x[i].p<=x[q[cnt-1]].p)cnt--;//排除斜率为负数的情况
            while(cnt>1)
            {
                int j=q[cnt-1],k=q[cnt-2];
                if((x[i].p-x[j].p)*(x[j].a-x[k].a)<=(x[j].p-x[k].p)*(x[i].a-x[j].a))
                    cnt--;
                else break;
            }
            q[cnt++]=i;
        }
        int ans=0;
        for(int i=0;i<cnt;i++)
        {
            if(!same[q[i]])ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值