欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-07-15 16:19:58
...

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:
HDU6759 Leading Robots(2020杭电多校训练第一场)
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。
HDU6759 Leading Robots(2020杭电多校训练第一场)

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;
}

相关标签: 2020杭电多校训练