HDU 6136 Death Podracing(循环链表)
【题目链接】 http://acm.hdu.edu.cn/showproblem.php?pid=6136
【题目大意】
一堆人在操场上跑步,他们都有一定的速度和初始位置,
当两个人相遇的时候编号较小的就会出局,当场上剩下最后一个人的时候游戏结束,
问时长为多少
【题解】
我们发现每次发生碰撞的一定是相邻的两个人,
所以我们找出相邻关系中最先发生碰撞的,将碰撞失败的那个人删除,
之后继续这个过程,
按照上述的做法我们建立一个循环链表,把所有人串起来,
当发生淘汰的时候把那个人从循环链表中删去即可,
用优先队列维护相邻关系,每次出队最小相遇时间即可。
【代码】
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
const int N=100010,INF=0x3f3f3f3f;
struct data{int d,v,pw;}p[N];
bool cmp(data a,data b){return a.d<b.d;}
int T,Del[N],n,L,nxt[N],pre[N];
struct Frac{
int Den,Num;
Frac(){}
Frac(int _Num,int _Den){Den=_Den;Num=_Num;}
bool operator <(const Frac &rhs)const{return 1LL*Num*rhs.Den<1LL*rhs.Num*Den;}
};
Frac Cal(data a,data b){
int V=b.v-a.v,D=a.d-b.d;
if(D<0)D+=L; if(V<0)V=-V,D=L-D;
if(V==0)return Frac(INF,1);
int GCD=__gcd(V,D);
return Frac(D/GCD,V/GCD);
}
struct Relt{
int a,b;Frac t;
Relt(){}
Relt(int _a,int _b,Frac _t){a=_a;b=_b;t=_t;}
bool operator < (const Relt &x)const{return x.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(){
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&L);
memset(Del,0,sizeof(Del));
for(int i=0;i<n;i++)scanf("%d",&p[i].d),p[i].pw=i;
for(int i=0;i<n;i++)scanf("%d",&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 Lft=n; Frac Ans;
while(Q.size()){
Relt x=Q.top(); Q.pop();
if(Del[x.a]||Del[x.b])continue;
if(--Lft==1){Ans=x.t;break;}
if(p[x.a].pw>p[x.b].pw)Out(x.b);
else Out(x.a);
}printf("%d/%d\n",Ans.Num,Ans.Den);
}return 0;
}
HDU 6136 Death Podracing(循环链表)的更多相关文章
- HDU 6136 Death Podracing (堆)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=6136 题解 完了,普及题都不会做了... 发现一个重要性质是只有相邻的人才会相撞,于是直接拿堆维护即可 ...
- HDU 5860 Death Sequence(死亡序列)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- HDU 5860 Death Sequence(递推)
HDU 5860 Death Sequence(递推) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 Description You ...
- 2016 Multi-University Training Contest 10 || hdu 5860 Death Sequence(递推+单线约瑟夫问题)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 题目大意:给你n个人排成一列编号,每次杀第一个人第i×k+1个人一直杀到没的杀.然后 ...
- hdu 5860 Death Sequence(递推+脑洞)
Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historian liv ...
- HDU 5860 Death Sequence
用线段树可以算出序列.然后o(1)询问. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<c ...
- 字符串(AC自动机):HDU 5129 Yong Zheng's Death
Yong Zheng's Death Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/O ...
- HDU 5129 Yong Zheng's Death
题目链接:HDU-5129 题目大意为给一堆字符串,问由任意两个字符串的前缀子串(注意断句)能组成多少种不同的字符串. 思路是先用总方案数减去重复的方案数. 考虑对于一个字符串S,如图,假设S1,S2 ...
- HDU 6140 17多校8 Hybrid Crystals(思维题)
题目传送: Hybrid Crystals Problem Description > Kyber crystals, also called the living crystal or sim ...
随机推荐
- 用体渲染的方法在Unity中渲染云(18/4/4更新)
github: https://github.com/yangrc1234/VolumeCloud 更新的内容在底部 最近在知乎上看到一篇文章讲云层的渲染(https://zhuanlan.zhihu ...
- SSL证书生成方法【转】
转自 SSL证书生成方法 - fyang的专栏 - 博客频道 - CSDN.NEThttp://blog.csdn.net/fyang2007/article/details/6180361 一般情况 ...
- 选中一行并且选中该行的radio
$("tr").bind("click",function(){ $("input:radio").attr("checked&q ...
- php7.33 configure
To assign environment variables (e.g., CC, CFLAGS...), specify them as VAR=VALUE. See below for desc ...
- acm专题---键树
题目来源:http://hihocoder.com/problemset/problem/1014?sid=982973 #1014 : Trie树 时间限制:10000ms 单点时限:1000ms ...
- java基础14 多态(及关键字:instanceof)
面向对象的三大特征: 1.封装 (将一类属性封装起来,并提供set()和get()方法给其他对象设置和获取值.或者是将一个运算方法封装起来,其他对象需要此种做运算时,给此对象调用) 2.继承 ...
- bzoj 2819(DFS序+树状数组+博弈+lca)
2819: Nim Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 2045 Solved: 795[Submit][Status][Discuss] ...
- hdu 1907(Nim博弈)
John Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submis ...
- 字节对齐&&sizeof
转:http://blog.chinaunix.net/uid-722885-id-124878.html 1. sizeof应用在结构上的情况 请看下面的结构: struct MyStruct { ...
- day4装饰器
Python装饰器 1.必备 def foo(): print(foo) <function foo at 0x7f62db093f28> >>> foo <fun ...