【多校训练】hdu 6136 Death Podracing 优先队列
2017-08-31 11:15
267 查看
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 (T≤1000),
denoting the number of test cases.
For each test case, the first line contains two integers n (2≤n≤105)
and L (1≤L≤109).
The following line contains n distinct
integers d1,d2,...,dn (0≤di<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
题意:
给你n个点的位置和速度,第i个点有i的强度,当有点相遇时,强度小的会撞死,求最后剩下一点的时间。
思路:
最初始状态环上有 nn 个人,第一次淘汰发生必然是环上相邻的两个人相撞。注意到第一个被淘汰的人不会对后续过程有任何影响,如果我们能找出谁是第一个被淘汰的,直接把这个人从初始状态中删除,就能把问题变成一个只有 n−1n−1 个人的子问题,这个子问题和原问题有相同的答案。
于是我们可以用一个堆维护环上所有相邻人相遇的时间,从中取出最小值,就能找到第一个被淘汰的人,这个人删除后,原本不相邻的两个人就相邻了,同样求出他们的相遇时间,加入堆中,重复执行这一过程,直到找到最后一个被淘汰的人为止。算法复杂度 O(nlogn)O(nlogn)
#include <iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define ll long long
using namespace std;
const int N=210000;
int n,vis
,l
,r
;
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
;
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);
}
}
> 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 (T≤1000),
denoting the number of test cases.
For each test case, the first line contains two integers n (2≤n≤105)
and L (1≤L≤109).
The following line contains n distinct
integers d1,d2,...,dn (0≤di<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
题意:
给你n个点的位置和速度,第i个点有i的强度,当有点相遇时,强度小的会撞死,求最后剩下一点的时间。
思路:
最初始状态环上有 nn 个人,第一次淘汰发生必然是环上相邻的两个人相撞。注意到第一个被淘汰的人不会对后续过程有任何影响,如果我们能找出谁是第一个被淘汰的,直接把这个人从初始状态中删除,就能把问题变成一个只有 n−1n−1 个人的子问题,这个子问题和原问题有相同的答案。
于是我们可以用一个堆维护环上所有相邻人相遇的时间,从中取出最小值,就能找到第一个被淘汰的人,这个人删除后,原本不相邻的两个人就相邻了,同样求出他们的相遇时间,加入堆中,重复执行这一过程,直到找到最后一个被淘汰的人为止。算法复杂度 O(nlogn)O(nlogn)
#include <iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define ll long long
using namespace std;
const int N=210000;
int n,vis
,l
,r
;
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
;
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);
}
}
相关文章推荐
- HDU 4869 Turn the pokers (2014多校联合训练第一场1009) 解题报告(维护区间 + 组合数)
- hdu 4941 Magical Forest(STL之map应用)2014多校训练第7场
- HDU 6170 && 2017 多校训练:Two strings(DP)
- 2017 多校训练第二场 HDU 6047 Maximum Sequence(贪心+优先队列)
- 2017 多校训练第十场 HDU 6178 Monkeys 最小点覆盖+树形DP
- HDU-5821 多校训练第8场-1001(巧妙模拟)
- 2017 多校训练第一场 HDU 6044 Limited Permutation
- HDU 5289 Assignment (二分+RMQ) 2015多校训练一 1002
- 多校联合训练4&&HDU 5763
- HDU 4920(杭电多校训练#5 1010 题) Matrix multiplication(不知道该挂个什么帽子。。。)
- (HDU 5723)Abandoned country <最小生成树 + 树上所有两点之间的距离的期望> 多校训练1
- 2017 多校训练第三场 HDU 6063 RXD and math
- 2018HDU多校训练一 C -Triangle Partition
- 【(好题)组合数+Lucas定理+公式递推(lowbit+滚动数组)+打表找规律】2017多校训练七 HDU 6129 Just do it
- 【多校训练】hdu 6043 KazaQ's Socks
- hdu 4911 Inversion(归并排序求逆序对数)2014多校训练第5场
- (HDU 5792)World is Exploding <树状数组+去重> 多校训练5
- 2017 多校训练第二场 HDU 6045 Is Derek lying?(思维)
- hdu 4617 2013多校联合训练第二场weapon简单的计算几何
- 【多校训练】hdu 6166 Senior Pan 最短路径 Dijkstra