Death Podracing HDU

来源:互联网 发布:pascal语言编程难吗 编辑:程序博客网 时间:2024/05/18 13:04

参考http://www.cnblogs.com/forever97/p/hdu6136.html
题目大意】

  一堆人在操场上跑步,他们都有一定的速度和初始位置,
  当两个人相遇的时候编号较小的就会出局,当场上剩下最后一个人的时候游戏结束,
  问时长为多少

代码没有敲对,留个坑

#include<bits/stdc++.h>using namespace std;#define sf scanf#define pf printf#define mem(a,b) memset(a,b,sizeof(a));#define rep(i,a,b) for(int i=(a);i<=(b);++i)#define MP make_pair#define ULL unsigned long long#define LL   long long#define inf 0x3f3f3f3f#define md ((ll+rr)>>1)#define ls (i<<1)#define rs (ls|1)#define eps 1e-5#define N 100010#define ree freopen("in.txt","r",stdin);#define bug pf("----------------");//2017年09月01日20:18:58struct People{int pw,v,d;}p[N];bool cmp(People a,People b){ return a.d<b.d; }struct Frac{    int num,den;    Frac(){}    Frac(int _num,int _den){ den=_den;num=_num; }    bool friend operator <(Frac a,Frac b){ return 1LL*a.num*b.den<a.den*b.num; }};struct Node{    int a,b; Frac t;    Node(){}    Node(int _a,int _b,Frac _t){        a=_a;b=_b;t=_t;    }    bool friend  operator<(Node a,Node b){ return a.t<b.t; }};int n,L;int nxt[N],pre[N],del[N];priority_queue<Node>q;Frac Cal(People a,People b){    int v=b.v-a.v,d=a.d-b.d;    if(d<0)d+=L;if(v<0)v=-v,d=L-d;    if(v==0)return Frac(inf,1);    int GCD=__gcd(v,d);    return Frac(d/GCD,v/GCD);}void out(int id){    del[id]=1;    nxt[pre[id]]=nxt[id];    pre[nxt[id]]=pre[id];    q.push(Node(pre[id],nxt[id],Cal(p[pre[id]],p[nxt[id]])));}int main(){    ree    int T;    sf("%d",&T);    while(T--){        sf("%d%d",&n,&L);        mem(del,0);        for(int i=0;i<n;++i)sf("%d",&p[i].d),p[i].pw=i;        for(int i=0;i<n;++i)sf("%d",&p[i].v);        sort(p,p+n,cmp);while(!q.empty())q.pop();        for(int i=0;i<n;++i){            nxt[i]=(i+1)%n;pre[i]=(i-1+n)%n;            q.push(Node(i,nxt[i],Cal(p[i],p[nxt[i]])));        }        int cnt=n;Frac ans;        while(!q.empty()){            Node u=q.top();q.pop();            if(del[u.a]||del[u.b])continue;            if(--cnt==1){ans=u.t;break;}            if(p[u.a].pw>p[u.b].pw)out(u.b);            else out(u.a);        }        pf("%d/%d\n",ans.num,ans.den);    }}
原创粉丝点击