【多校训练】hdu 6136 Death Podracing 优先队列

来源:互联网 发布:西安财经大学行知学院 编辑:程序博客网 时间:2024/05/04 16:37

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
22 40 23 210 10056 89 62 71 7 24 83 1 47 529 -16 34 -38 47 49 -32 17 39 -9
 

Sample Output
2/137/7
 

题意:

给你n个点的位置和速度,第i个点有i的强度,当有点相遇时,强度小的会撞死,求最后剩下一点的时间。


思路:

最初始状态环上有 nn 个人,第一次淘汰发生必然是环上相邻的两个人相撞。注意到第一个被淘汰的人不会对后续过程有任何影响,如果我们能找出谁是第一个被淘汰的,直接把这个人从初始状态中删除,就能把问题变成一个只有 n−1n1 个人的子问题,这个子问题和原问题有相同的答案。
于是我们可以用一个堆维护环上所有相邻人相遇的时间,从中取出最小值,就能找到第一个被淘汰的人,这个人删除后,原本不相邻的两个人就相邻了,同样求出他们的相遇时间,加入堆中,重复执行这一过程,直到找到最后一个被淘汰的人为止。算法复杂度 O(nlogn)O(nlogn)

#include <iostream>#include<queue>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#define ll long longusing namespace std;const int N=210000;int n,vis[N],l[N],r[N];ll L;typedef pair<ll,ll>P;struct node1{    ll d,v;    int i;    friend bool operator < (const node1 &a, const node1 b)    {        return a.d<b.d;    }}c[N];P cal_time(int i,int j){    if(c[i].d<c[j].d)   swap(i,j);    ll d1=c[i].d-c[j].d;    ll d2=L-d1;    if(c[j].v>c[i].v)   return P(d1,c[j].v-c[i].v);    else    return P(d2,c[i].v-c[j].v);    }struct node{    int i,j;    friend bool operator < (const node &a,const node &b)    {        P p1=cal_time(a.i,a.j);        P p2=cal_time(b.i,b.j);        return (double)p1.first/p1.second>(double)p2.first/p2.second;    }};ll gcd(ll a,ll b){    return b==0?a:gcd(b,a%b);}priority_queue<node> q;int main(int argc, const char * argv[]) {    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d%lld",&n,&L);        for(int i=0;i<n;i++)    scanf("%lld",&c[i].d),c[i].i=i;        for(int i=0;i<n;i++)    scanf("%lld",&c[i].v);        sort(c,c+n);        for(int i=0;i<n;i++)    r[i]=(i+1)%n,l[i]=(i-1+n)%n;        while(!q.empty())   q.pop();        for(int i=0;i<n;i++)    q.push(node{i,(i+1)%n});        memset(vis,0,sizeof(vis));        ll up=0,down=1;        while(!q.empty())        {            node t=q.top();            q.pop();            int i=t.i,j=t.j;            if(vis[i]||vis[j])  continue;            if(c[j].i>c[i].i) swap(i,j);            P p=cal_time(i,j);            up=p.first,down=p.second;            vis[j]=1;            int l2=l[j],r2=r[j];            r[l2]=r2;l[r2]=l2;            if(l2!=i)   q.push(node{i,l2});            if(r2!=i)   q.push(node{i,r2});        }        ll t=gcd(up,down);        printf("%lld/%lld\n",up/t,down/t);    }}

原创粉丝点击