2020 Multi-University Training Contest 1 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: 



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 < 231231) 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

Sponsor

 

补:

场上只想到o2的做法

单调栈认识的还不够

#include
using namespace std;
#define ll long long
const int maxn = 5e4 + 5;
using namespace std;
struct node{
    int p;
    int a;
} C[maxn];
int vis[maxn];
bool cmp(node a, node b){
    if(a.p == b.p){
        return a.a > b.a;
    }
    return a.p > b.p;
}

void solve(){
    memset(vis, 0, sizeof(vis));
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++)
        cin >> C[i].p >> C[i].a;
    sort(C + 1, C + n + 1, cmp);
    stack ss;
    for(int i = 2; i <= n; i++){
        if(C[i].p == C[i - 1].p && C[i].a == C[i - 1].a){
            vis[i] = vis[i - 1] = 1;
        }
    }
    for(int i = 1; i <= n; i++){
        if(ss.empty()){
            ss.push(i);
        }
        else{
            int pre = ss.top();
            if(C[i].p == C[pre].p || C[i].a <= C[pre].a)
                    continue;
            if(ss.size() == 1){
                ss.push(i);
            }
            else{
                while(ss.size() > 1){
                    ss.pop();
                    int pr = ss.top();
                    ss.push(pre);
                    double t1 = 2.0 * (C[pr].p - C[pre].p) / (C[pre].a - C[pr].a);
                    double t2 = 2.0 * (C[pre].p - C[i].p) / (C[i].a - C[pre].a);
                    if(t2 > t1){
                        break;
                    }
                    else{
                        ss.pop();
                    }
                    pre = ss.top();
                }
                ss.push(i);
            }
        }

    }
    int ans = 0;
    while(!ss.empty()){
        int x = ss.top();
        ss.pop();
        if(!vis[x])
            ans++;
    }
    cout << ans << endl;
}

int main(){
    ios::sync_with_stdio(0);
    int t;
    cin >> t;
    while(t--){
        solve();
    }
}

 

你可能感兴趣的:(单调栈)