2017多校联合第八场/hdu 6136Death Podracing(优先队列+循环链表)

Death Podracing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 448    Accepted Submission(s): 178


Problem Description
> During the Trade Federation invasion of Naboo, Anakin Skywalker won the Boonta Eve Classic on Tatooine, securing his freedom from a life of slavery. Betting on the races was a popular pastime with many of the watchers on Tatooine and it was through Qui-Gon Jinn's bet with Watto that Skywalker would win the race that Skywalker was freed.
>
> — Wookieepedia

Here comes the most awesome racing in the universe! Little Anakin sits in his own podracer, breathing deeply. He must win.

The real podracing is deadly. In this circular track (again  L  in length), participants start in distinct locations and race with distinct speeds (the speed can be positive and minus, which means running clockwise and counterclockwise). Every podracer is equipped with a powerful weapon system respectively -- all for triumphing over enemies!!! yes! enemies!

If two competitors meet, which means that they reach an identical location simultaneously, the one with less powerful weapon system will be destroyed and be knocked out of the racing -- dead or alive. The power of the  i -th participant is exactly  i , and because each person runs with a distinct speed, so there must be a winner. 

The racing stops when there is only one person still running in the track -- our winner! The others are crushed into pieces.

Little Anakin wants to know, when the racing will stop.
 

Input
The first line contains an integer  T  ( T1000 ), denoting the number of test cases. 

For each test case, the first line contains two integers  n  ( 2n105 ) and  L  ( 1L109 ).

The following line contains  n  distinct integers  d1,d2,...,dn  ( 0di<L ), representing the starting points of the participants. 

The next line contains  n  distinct integers  v1,v2,...,vn  ( 0|vi|109 ), representing the velocity of the participants.

The sum of all  n  in all test cases doesn't exceed  2×106 .
 

Output
One line for each test case. You should output the answer that is reduced to lowest terms.
 

Sample Input
          
          
2 2 4 0 2 3 2 10 100 56 89 62 71 7 24 83 1 47 52 9 -16 34 -38 47 49 -32 17 39 -9
 

Sample Output
          
          
2/1 37/7
 

Source
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   6181  6180  6179  6178  6177 
 

Statistic |  Submit |  Discuss |  Note


【题目大意】

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

 

【题解】

  我们发现每次发生碰撞的一定是相邻的两个人,
  所以我们找出相邻关系中最先发生碰撞的,将碰撞失败的那个人删除,
  之后继续这个过程,
  按照上述的做法我们建立一个循环链表,把所有人串起来,
  当发生淘汰的时候把那个人从循环链表中删去即可,
  用优先队列维护相邻关系,每次出队最小相遇时间即可。


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <string.h>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <list>
#include <bitset>
#include <stack>
#include <stdlib.h>
#define lowbit(x) (x&-x)
#define e exp(1.0)
#define eps 1e-8
//ios::sync_with_stdio(false);
//    auto start = clock();
//    cout << (clock() - start) / (double)CLOCKS_PER_SEC<<endl;
typedef long long ll;
typedef long long LL;
using namespace std;
typedef unsigned long long ull;
const int maxn=1e5+10;
const int inf=0x3f3f3f3f;
struct data
{
    int d,v,pw;
}p[maxn];
int cmp(data &a,data &b)
{
    return a.d<b.d;
}
int del[maxn],n,l,nxt[maxn],pre[maxn];
struct node
{
    int den,num;
    node(){}
    node(int num,int den)
    {
        this->num=num;
        this->den=den;
    }
    bool operator<(const node &a)const
    {
        return (ll)num*a.den<(ll)a.num*den;
    }
};
node cal(data a,data b)
{
    int V=b.v-a.v;
    int D=a.d-b.d;
    if(D<0) D+=l;
    if(V<0) V=-V,D=l-D;
    if(V==0) return node(inf,1);
    int GCD=__gcd(V,D);
    return node(D/GCD,V/GCD);
}
struct relt
{
    int a,b;
    node t;
    relt(){}
    relt(int a,int b,node t)
    {
        this->a=a;
        this->b=b;
        this->t=t;
    }
    bool operator<(const relt &a)const
    {
        return a.t<t;
    }
};
priority_queue<relt>q;
void out(int x)
{
    del[x]=1;
    nxt[pre[x]]=nxt[x];
    pre[nxt[x]]=pre[x];
    q.push(relt(pre[x],nxt[x],cal(p[pre[x]],p[nxt[x]])));
}
int main()
{
    ios::sync_with_stdio(false);
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>l;
        memset(del,0,sizeof(del));
        for(int i=0;i<n;i++)
        {
            cin>>p[i].d;
            p[i].pw=i;
        }
        for(int i=0;i<n;i++)
            cin>>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(relt(i,nxt[i],cal(p[i],p[nxt[i]])));
        }
        int le=n;
        node ans;
        while(!q.empty())
        {
            relt x=q.top();
            q.pop();
            if(del[x.a] || del[x.b]) continue;
            if(--le==1)
            {
                ans=x.t;
                break;
            }
            if(p[x.a].pw>p[x.b].pw) out(x.b);
            else out(x.a);
        }
        cout<<ans.num<<'/'<<ans.den<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值