HDU 6136 优先队列 模拟

6 篇文章 0 订阅

Death Podracing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 237    Accepted Submission(s): 81


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
 

题意 : 

在一个操场上一群人在跑圈,每个人有一个起始位置和速度,每个人的攻击力是它的下标。

每当有两个人相撞的时,攻击力低的被消灭,问最后剩下一个人的时候的 时间 输出格式为分数

思路 :

其实不管给出的数据是怎么样的,第一个相撞消失的人不会对后面发生的相撞产生任何影响,所以每次相撞就把人除掉就可以了,最后剩下的两个人去算它们从开始到见面需要的时间就可以了。

首先对每个人根据初始位置进行排序,因为相撞总是先发生在相邻的两个人身上的,所以一开始先对相邻的人两两计算见面时间,然后放进优先队列里(时间短的优先出队),然后依次出队,判定见面的两个人中哪个会消失,然后把消失的人除去,把新建立起来的相邻关系放进优先队列,一直处理直到队列只剩最后一对,然后取出来计算时间就可以了。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
#define maxn 200050

class P{
	public :
		int loc,id,v;
		bool operator < (const P& x) const{
			return loc < x.loc;
		}
}p[maxn];

class Q{
	public :
		int l,r;
		double t;
		Q(int ll = 0,int rr = 0,double tt = 0){
			l = ll,r = rr,t = tt;
		}
		bool operator < (const Q& x) const{
			return t > x.t;
		} 
};

int lp[maxn],rp[maxn],n,all,len;
bool vis[maxn];
priority_queue<Q> q;

int getGcd(int x,int y){       //用于化简分数
    if(y == 0)
		return x;
    return getGcd(y,x % y);
}

double getTime(int x,int y){
	int dx =  ((p[y].loc - p[x].loc) + len) % len;
	int dv = p[x].v - p[y].v;
	if(dv < 0){
		dv = -dv;
		dx = len - dx;
	}
	return (dx * 1.0 / dv);
}

void getAns(int x,int y){
	int dx =  ((p[y].loc - p[x].loc) + len) % len;
	int dv = p[x].v - p[y].v;
	if(dv < 0){
		dv = -dv;
		dx = len - dx;
	}
	int tmp = getGcd(dx,dv);
	dx /= tmp;
	dv /= tmp;
	printf("%d/%d\n",dx,dv);
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%d %d",&n,&len);
		for(int i = 0;i < n;i++){
			scanf("%d",&p[i].loc);
			p[i].id = i;
		}
		for(int i = 0;i < n;i++){
			scanf("%d",&p[i].v);
		}
		sort(p,p + n);
		while(!q.empty())
			q.pop();
		for(int i = 0;i < n;i++){
			double t = getTime(i,(i + 1) % n);
			q.push(Q(i,(i + 1) % n,t));
			lp[i] = ((i  - 1) + n) % n;
			rp[i] = (i + 1) % n;
		}
		all = n;
		memset(vis,0,sizeof(vis));
		while(q.size() > 1){
			Q tmp = q.top();
			q.pop();
			int l = tmp.l,r = tmp.r;
			if(vis[l] || vis[r])
				continue;
			if(lp[l] == r && rp[l] == r)
				break;
			if(p[l].id > p[r].id){
				vis[r] = 1;
				rp[l] = rp[r];
				lp[rp[r]] = l;
				double tm = getTime(l,rp[r]);
				q.push(Q(l,rp[r],tm));
			}else{
				vis[l] = 1;
				lp[r] = lp[l];
				rp[lp[l]] = r;
				double tm = getTime(lp[l],r);
				q.push(Q(lp[l],r,tm));
			}
			if(--all <= 0)
				break;
		}
		Q tmp = q.top();
		q.pop();
		int l = tmp.l,r = tmp.r;
		getAns(l,r);
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值