Official contest of the season gunboats

topic

Description
gunboats contest is a dangerous game. In order to win this game, contestants may sacrifice their lives.

Participants will be a length \ (L \) circular track on the game. At the start of the game ( \ (0 \) time), all participants standing on different orbital positions, wherein the i-th entrant standing position \ (di (0≤di <L) \) on. Then start the race. Each participant drove its gunboats, speed \ (vi \) (speed can be positive or negative, can also \ (0 \) . Entrants moving speed is positive clockwise speed is negative representation participants move counter-clockwise). Each participant's speed is different.

The first \ (i \) contestants have (i \) \ -point energy value. During the race, participants are likely to encounter (meet here refers to the contestants falls exactly in the same place at the same time). Each two contestants \ (i, j \) when met, low energy value Participants were shot out.

When just one person on the field, ending the game.

When asked end of the game.

Input
The first line contains two positive integers \ (n, L (1 \ leq n \ leq 10 ^ 5,1 \ leq L \ leq 10 ^ 9) \)

The next line contains n different integers \ (di (0 \ leq di <L) \)

The next line contains n different integers $ vi (| vi | \ leq109) $

Output
output a fractional \ (X / Y \) represents the end time, where \ (GCD (X-, the Y) =. 1 \) . If the answer is \ (0 \) , should only be output " \ (0 \) " (without the quotes).

answer

Direct simulation.
The first order.
Obviously the first to meet certain adjacent.
So we met temporally adjacent gunboats thrown into a small heap root. Top of the stack is then removed, deleted will be defeated, then the newly generated time meet adjacent gunboat thrown rootlets stack (here simulated by gunboats chain sequence). This process is repeated until only a speedboat.

Code

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>

#include <vector>
#include <queue>

using namespace std;


typedef long long LL;


typedef pair <LL, LL> pr;


inline LL gcd(LL a, LL b) { return !b ? a : gcd(b, a % b); }


const int N = 1e5 + 10;


struct Data
{
    int x, y; double t;
    Data() { }
    Data(int _1, int _2, double _3) : x(_1), y(_2), t(_3) { }
    inline bool operator < (Data rhs) const { return t > rhs.t; }
};


priority_queue <Data> q;


int n; LL L;


struct Info
{
    LL d, v; int w, pre, nxt; 
    Info() { }
    Info(LL _1, LL _2, int _3, int _4, int _5) : d(_1), v(_2), w(_3), pre(_4), nxt(_5) { }
} a[N];


pr calc(int x, int y)
{
    if (a[x].v >= a[y].v) return make_pair((a[y].d - a[x].d + L) % L, a[x].v - a[y].v);
    return make_pair(L - ((a[y].d - a[x].d + L) % L), a[y].v - a[x].v);
}


bool tag[N];


int main()
{
    scanf("%d %lld", &n, &L);
    
    if (n <= 1) return 0 & puts("0");
    
    for (int i = 1; i <= n; i++)
        scanf("%lld", &a[i].d);
    for (int i = 1; i <= n; i++)
        scanf("%lld", &a[i].v), a[i].w = i;
    
    sort(a + 1, a + 1 + n, [](Info a, Info b) { return a.d < b.d; });
    
    a[1].pre = n; a[1].nxt = 2;
    a[n].pre = n - 1; a[n].nxt = 1;
    for (int i = 2; i < n; i++)
        a[i].pre = i - 1, a[i].nxt = i + 1;
    
    for (int i = 1; i <= n; i++)
    {
        pr t = calc(i, a[i].nxt);
        q.push( Data(i, a[i].nxt, (double) t.first / t.second) );
    }
    
    int cnt = 0;
    
    Data tmp;
    while (q.size() > 1) 
    {
        tmp = q.top(); q.pop();
        if (tag[tmp.x] || tag[tmp.y]) continue;
        cnt++;
        if (cnt == n-1) break;
        if (a[tmp.x].w > a[tmp.y].w)
        {
            tag[tmp.y] = 1;
            a[tmp.x].nxt = a[tmp.y].nxt;
            a[a[tmp.x].nxt].pre = tmp.x;
            pr t = calc(tmp.x, a[tmp.x].nxt);
            q.push( Data(tmp.x, a[tmp.x].nxt, (double) t.first / t.second) );
        }
        else
        {
            tag[tmp.x] = 1;
            a[tmp.y].pre = a[tmp.x].pre;
            a[a[tmp.y].pre].nxt = tmp.y;
            pr t = calc(a[tmp.y].pre, tmp.y);
            q.push( Data(a[tmp.y].pre, tmp.y, (double) t.first / t.second) );
        }
    }
    
    tmp = q.top();
    pr t = calc(tmp.x, tmp.y);
    printf("%lld/%lld\n", t.first / gcd(t.first, t.second), t.second / gcd(t.first, t.second));
    
    return 0;
}

Guess you like

Origin www.cnblogs.com/2016gdgzoi509/p/11305273.html