hdu 6136 模拟+优先队列

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
2017 Multi-University Training Contest - Team 8
 

Recommend

liuyiding   |   We have carefully selected several similar problems for you:  6170 6169 6168 6167 6166 


题意:一个圆形跑道,n个人跑步,每个人有不同的起始点和速度(大小和方向均可能不同),标号1-n,当两个人相遇,标号小的将会被淘汰,问最后还剩一个人时的时间。


思路:大模拟+优先队列   首先模拟一下操场的情况,将n个人按起点的大小排序,然后用优先队列维护相邻两人之间追逐需要的时间,每次淘汰下表小得人并且更新剩下人的左右,并将新的追逐时间入队列。


ac代码:

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int MAXN=2*1e5+1;
int n,l;
int L[MAXN],R[MAXN];
bool vis[MAXN];
struct node
{
    int s;
    int v;
    int id;
}a[MAXN];
bool cmp(node a,node b)
{
    return a.sb.time;
    }
};
int gcd(int x1,int y1)
{
    if(y1==0)
    return x1;
    return gcd(y1,x1%y1);
}
double get_time(int x1,int y1)
{
    int dx=((a[y1].s-a[x1].s)%l+l)%l;
    int dv=a[x1].v-a[y1].v;
    if(dv<0)
    {
        dv=-dv;
        dx=l-dx;
    }
    return (double)dx/dv;
}
void output(int x1,int y1)
{
    int dx=((a[y1].s-a[x1].s)%l+l)%l;
    int dv=a[x1].v-a[y1].v;
    if(dv<0)
    {
        dv=-dv;
        dx=l-dx;
    }
    int pk=gcd(dx,dv);
    dx/=pk;
    dv/=pk;
    printf("%d/%d\n",dx,dv);
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        memset(vis,0,sizeof(vis));
        cin>>n>>l;
        for(int i=0;i que;
        node1 N;
        for(int i=0;i1)
        {
           N=que.top();
           que.pop();
           int p1=N.x,p2=N.y;
           if(vis[p1]||vis[p2])
           continue;
           if(L[p1]==p2&&R[p1]==p2)
           break;
           if(a[p1].id>a[p2].id)
           {
               vis[p2]=1;
               R[p1]=R[p2];
               L[R[p2]]=p1;
               double tim=get_time(p1,R[p2]);
               N.x=p1;
               N.y=R[p2];
               N.time=tim;
               que.push(N);
           }
           else
           {
               vis[p1]=1;
               L[p2]=L[p1];
               R[L[p1]]=p2;
               double tim=get_time(L[p1],p2);
               N.x=L[p1];
               N.y=p2;
               N.time=tim;
               que.push(N);
           }
           k++;
           if(k==n)
           break;
        }
        N=que.top();
        que.pop();
        int p1=N.x,p2=N.y;
        output(p1,p2);
    }
    return 0;
}


你可能感兴趣的:(多校)