[JLOI2013] racing monotony stack

Description
There are a total of N cars on the field, which are called g1 and g2 respectively gn. The track is an infinite straight line. Initially, gi is at the position of ki ahead of the starting line. After the start of the race, the vehicle gi will travel at a constant speed in vi units per second. Your task is to figure out which cars have been in the lead (i.e. no other car has been in front of him).

Sample Input
4
1 1 0 0
15 16 10 20

Sample Output
3
1 2 4

First of all, we can rule out some of the most useless states. If the starting point and speed of one car are lower than that of another car, we can rule out them.
Then you want to convert it into a straight line, and then you want to maintain a straight line that is not covered...

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
int _max(int x, int y) {return x > y ? x : y;}

struct node {
    int x, y, p;
} a[11000], gg[11000], sta[11000];
int len, tp, yy[11000];
int hh;

bool cmp(node a, node b) {
    if(a.x == b.x) return a.y > b.y;
    return a.x > b.x;
}

bool pd(node x, node y, node hh) {
    double cc = (double)(y.x - x.x) / (x.y - y.y);
    double x1 = cc * (double)x.y + (double)x.x;
    double x2 = cc * (double)hh.y + (double)hh.x;
    if(x1 < x2) return 1;
    else return 0;
}

int main() {
    int n; scanf("%d", &n);
    for(int i = 1; i <= n; i++) scanf("%d", &a[i].x);
    for(int i = 1; i <= n; i++) scanf("%d", &a[i].y);
    for(int i = 1; i <= n; i++) a[i].p = i;
    sort(a + 1, a + n + 1, cmp);
    int u = 1, maxx = a[1].y;
    gg[++len] = a[u];
    while(a[u + 1].x == a[1].x) yy[++hh] = a[++u].p, maxx = _max(maxx, a[u].y);
    for(; u <= n;) {
        if(a[u + 1].y <= maxx && (a[u + 1].y != gg[len].y || a[u + 1].x != gg[len].x)) u++;
        else gg[++len] = a[++u], maxx = _max(maxx, a[u].y);
    }
    sta[++tp] = gg[1];
    for(int i = 2; i <= len; i++) {
        while(tp > 1 && pd(sta[tp], gg[i], sta[tp - 1])) tp--;
        sta[++tp] = gg[i];
    }
    for(int i = 1; i <= tp; i++) yy[++hh] = sta[i].p;
    sort(yy + 1, yy + hh + 1);
    printf("%d\n", hh);
    for(int i = 1; i <= hh; i++) printf("%d ", yy[i]);
    printf("\n");
    return 0;
}

Posted by MichaelHe on Tue, 31 Mar 2020 06:05:42 -0700