『优先队列·链表』游戏

P r o b l e m \mathrm{Problem} Problem

在这里插入图片描述

S o l u t i o n \mathrm{Solution} Solution

这道题目的n^2暴力就是,枚举任意两个车的相遇时间,再对时间排序并进行模拟即可。但是我们观察题目的性质,我们发现任意两个汽车的相遇只会是相邻两辆车,这很容易脑补出来。因此对于相邻关系,我们选择链表维护。

即,我们队任意相邻两个节点建立循环双向链表,并将所有的相遇时间扔进优先队列里面;那么,我们每一次取出时间最小的一对车,删除战斗力较小的车,并将左右的车合并,知道删除n-1次为止。

这样,我们就用链表+堆来模拟这个过程了,题目逇突破口在还在于相遇只会发生在相邻点,这样就避免了许多无用的状态。

C o d e \mathrm{Code} Code

#include <queue>
#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;
const int N = 3e5;

int n, Len;
int L[N], R[N], vis[N];
struct pig {
	int p, a, v;
	friend bool operator < (pig p1,pig p2) {
		return p1.p < p2.p;
	}
}a[N];
struct node {
	int dis, speed, x, y;
	friend bool operator < (node p1,node p2) {
		return 1LL * p1.dis * p2.speed > 1LL * p1.speed * p2.dis;
	}
};
priority_queue < node > q;

int gcd(int a,int b) {
	if (b == 0) return a;
	return gcd(b,a%b);
}

int next(int x) {
	if (x == n) return 1;
	return x + 1;
}

int read(void)
{
	int s = 0, w = 0; char c = getchar();
	while (c < '0' || c > '9') w |= c == '-', c = getchar();
	while (c >= '0' && c <= '9') s = s*10+c-48, c = getchar();
	return w ? -s : s;
}

pair<int,int> Getdis(int x,int y)
{
	int dis;int speed = abs(a[x].v - a[y].v);
	if (a[x].v < 0 and a[y].v > 0) x^=y^=x^=y;
	if (a[x].v > 0 and a[y].v < 0) dis = a[x].p<a[y].p?a[y].p-a[x].p:Len+a[y].p-a[x].p;
	if (abs(a[x].v ) < abs(a[y].v)) x^=y^=x^=y;
	if (a[x].v > 0 and a[y].v > 0) dis = a[x].p<a[y].p?a[y].p-a[x].p:Len+a[y].p-a[x].p;
	if (a[x].v < 0 and a[y].v < 0) dis = a[x].p<a[y].p?Len+a[x].p-a[y].p:a[x].p-a[y].p;
	int t = gcd(dis,speed);
	dis /= t, speed /= t;
	return {dis,speed};
}

void Delete(int x)
{
	R[L[x]] = R[x];
	L[R[x]] = L[x];
	return;
}

int main(void)
{
	freopen("game.in","r",stdin);
	freopen("game.out","w",stdout);
	n = read(), Len = read();
	for (int i=1;i<=n;++i) a[i].p = read();
	for (int i=1;i<=n;++i) a[i].v = read();
	for (int i=1;i<=n;++i) a[i].a = read();
	sort(a+1,a+n+1);
	for (int i=1;i<=n;++i)
	{
		R[i] = next(i), L[next(i)] = i;
		pair <int,int> t = Getdis(i,next(i));
		q.push(node{t.first,t.second,i,next(i)});
	}
	int cnt = 0;
	while (true)
	{
		node top = q.top(); q.pop();
		if (vis[top.x] +vis[top.y]) continue;
		if (++cnt == n - 1) {
			cout << top.dis << '/' << top.speed;
			break;
		}
		int x = top.x, y = top.y;
		int die = a[x].a > a[y].a ? y : x;
		vis[die] = 1;
		pair <int,int> t = Getdis(L[die],R[die]);
		q.push(node{t.first,t.second,L[die],R[die]});
		Delete(die);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值