BZOJ3190 赛车

BZOJ3190 赛车

题目传送门

题解

这题有点像1007的那道题,我们可以把赛车的速度转化成斜率,把赛车的初始位置转化成与\(y\)轴的交点,然后就和1007差不多了。不过要注意的是计算两个直线交点的时候,如果交点在\(x\)轴的负半轴,是不能直接拿来比较的,因为规定了时间为正。

code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool Finish_read;
template<class T>inline void read(T &x){Finish_read=0;x=0;int f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}while(isdigit(ch))x=x*10+ch-'0',ch=getchar();x*=f;Finish_read=1;}
template<class T>inline void print(T x){if(x/10!=0)print(x/10);putchar(x%10+'0');}
template<class T>inline void writeln(T x){if(x<0)putchar('-');x=abs(x);print(x);putchar('\n');}
template<class T>inline void write(T x){if(x<0)putchar('-');x=abs(x);print(x);}
/*================Header Template==============*/
#define PAUSE printf("Press Enter key to continue..."); fgetc(stdin);
int n;
struct car {
    int v,x,idx;
    double cro;
    bool operator < (const car &rhs) const {
    	return v==rhs.v?x<rhs.x:v<rhs.v;
    }
}p[10010];
int st[10010],top,vis[10010];
/*==================Define Area================*/
int main() {
    scanf("%d",&n);
    for(int i=1;i<=n;i++) read(p[i].x);
    for(int i=1;i<=n;i++) read(p[i].v),p[i].idx=i;
    sort(p+1,p+n+1);
    top=0;
    for(int i=1;i<=n;i++) {
        while(top) {
            if(p[i].x>p[st[top]].x) top--;
            else if(1.0*p[st[top]].x-p[i].x<p[st[top]].cro*(p[i].v-p[st[top]].v)) top--;
            else break;
        }
        if(top&&p[i].v>p[st[top]].v) p[i].cro=(double)(p[st[top]].x-p[i].x)/(p[i].v-p[st[top]].v);
        st[++top]=i;
    }
    printf("%d\n",top);
    for(int i=1;i<=top;i++) vis[p[st[i]].idx]=1;
    int tot=0;
    for(int i=1;i<=n;i++) {
        if(vis[i]) {
            printf("%d",i);
            tot++;
            if(tot!=top) printf(" ");
        }
    }
    return 0;
}
posted @ 2018-08-07 08:19  Apocrypha  阅读(147)  评论(0编辑  收藏  举报