1. 程式人生 > >hdu 6136 Death Podracing(模擬)

hdu 6136 Death Podracing(模擬)

wap mes priority bool ++ col href else pla

題目鏈接:hdu 6136 Death Podracing

題意:

有n個人在一個環形的跑道上,第i個人有一個power i,每個人有一個起始點和一個不同的速度。

如果兩個人相遇,那麽power大的那個人就會將power小的那個人淘汰出局。

然後問決出勝負需要多少時間。

題解:

顯然,每次有人被淘汰出局的時候,都是被相鄰的人幹掉的,那麽我們先預處理出相鄰的人相遇的時間,然後扔進優先隊列裏面,每次選擇最小的時間,將一個人淘汰掉,然後再更新一下當前的局勢就行了。

技術分享
 1 #include<bits/stdc++.h>
 2 #define F(i,a,b) for(int i=(a);i<=(b);++i)
 3
using namespace std; 4 typedef pair<int,int>P; 5 6 const int N=1e5+7; 7 int t,n,L,head,vis[N],rev[N],TIM; 8 9 struct VAL 10 { 11 int a,b,x,y;double val; 12 VAL(){} 13 VAL(int _a,int _b,int _x,int _y):a(_a),b(_b),x(_x),y(_y){val=1.0*_a/_b;} 14 bool operator <(const VAL&B)const
{return val>B.val;} 15 }; 16 struct LIST{int idx,pre,nxt;}lst[N]; 17 P a[N],tmp[N]; 18 priority_queue<VAL>Q; 19 20 VAL cal(int x,int y) 21 { 22 if(a[x].first>a[y].first)swap(x,y); 23 int dis,V=abs(a[x].second-a[y].second); 24 if(a[x].second>a[y].second) 25 dis=a[y].first-a[x].first;
26 else dis=L-a[y].first+a[x].first; 27 int gcd=__gcd(dis,V); 28 dis/=gcd,V/=gcd; 29 return VAL(dis,V,x,y); 30 } 31 32 void del(int x,int y) 33 { 34 int lx=rev[x],ly=rev[y]; 35 if(lst[lx].pre==ly) 36 { 37 lst[lx].pre=lst[ly].pre; 38 lst[lst[ly].pre].nxt=lx; 39 Q.push(cal(x,lst[lst[ly].pre].idx)); 40 }else 41 { 42 lst[lx].nxt=lst[ly].nxt; 43 lst[lst[ly].nxt].pre=lx; 44 Q.push(cal(x,lst[lst[ly].nxt].idx)); 45 } 46 } 47 48 int main(){ 49 scanf("%d",&t); 50 while(t--) 51 { 52 TIM++,scanf("%d%d",&n,&L); 53 F(i,1,n)scanf("%d",&a[i].first); 54 F(i,1,n)scanf("%d",&a[i].second); 55 F(i,1,n)tmp[i]=P(a[i].first,i); 56 sort(tmp+1,tmp+1+n),head=1; 57 F(i,1,n)lst[i]=LIST{tmp[i].second,i-1,i+1},rev[tmp[i].second]=i; 58 lst[n].nxt=1,lst[1].pre=n; 59 while(!Q.empty())Q.pop(); 60 F(i,1,n)Q.push(cal(lst[i].idx,lst[lst[i].nxt].idx)); 61 VAL ans; 62 F(i,2,n) 63 { 64 VAL now=Q.top();Q.pop(); 65 while(vis[now.x]==TIM||vis[now.y]==TIM) 66 now=Q.top(),Q.pop(); 67 if(i==n)ans=now; 68 int x=now.x,y=now.y; 69 if(x<y)swap(x,y); 70 del(x,y),vis[y]=TIM; 71 } 72 printf("%d/%d\n",ans.a,ans.b); 73 } 74 return 0; 75 }
View Code

hdu 6136 Death Podracing(模擬)