多校第一场——1009 Leading Robots

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个机器人的初始位置p和加速度a,在无限长的跑道中,有多少机器人能成为领头羊。
题解:先将机器人按照a从小到大排序,a相同p从小到大排序。这样,后面的机器人一定会超过前面的机器人或者和前一个机器人一样(这种情况不能算领头羊)。如果后面的机器人p比前面的大,那么一定会比前面的先到第一,前面的机器人不能成为领头羊,就出栈。如果栈内有两个机器人a、b,要加入一个机器人c, b追上a的时间是t1,c追上b的时间是t2,那么t2一定严格大于t1,否则的话b就需要出栈。然后再判断a前面的机器人和a和c能否同时存在。栈维护完后,如果有两个机器人的p和a一样,那么他们一定不能到达第一。

代码:

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

#define a first
#define p second

const int N = 50010;
struct robot
{
    double a, p;
} R[N];

bool panduan(robot a, robot b, robot c)
{
    return (b.p - c.p)*(b.a - a.a) - (a.p - b.p)*(c.a - b.a) <= 0;
}
bool cmp(robot a, robot b)
{
    return a.a == b.a ? a.p < b.p : a.a < b.a;
}
int main()
{

    int T;
    cin >> T;
    while (T--)
    {
        int n;
        scanf("%d",&n);
        map<pii, int>mp;
        for (int i = 1; i <= n; ++i)
        {
            scanf("%lf%lf",&R[i].p,&R[i].a);
            pii p;
            p.p = R[i].p;
            p.a = R[i].a;
            mp[p]++;
        }
        sort(R + 1, R + n + 1, cmp);
        int top = 0;
        int temp[N];
        memset(temp, 0, sizeof(temp));
        for (int i = 1; i <= n; ++i)
        {
            while ((top > 0 && R[temp[top]].p <= R[i].p) || (top > 1 && panduan(R[temp[top - 1]], R[temp[top]], R[i])))
            {
                --top;
            }
            temp[++top] = i;
        }
        int ans = top;
        for (int i = 1; i <= top; i++)
        {
            pii p;
            p.a = R[temp[i]].a;
            p.p = R[temp[i]].p;
            if (mp[p] > 1)
            {
                ans--;
            }
        }
        printf("%d\n",ans);
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43330910/article/details/107578295