C. Robot Collisions(思维)

题目链接
outputstandard output
There are n robots driving along an OX axis. There are also two walls: one is at coordinate 0 and one is at coordinate m.

The i-th robot starts at an integer coordinate xi (0<xi<m) and moves either left (towards the 0) or right with the speed of 1 unit per second. No two robots start at the same coordinate.

Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.

Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn’t collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.

For each robot find out if it ever explodes and print the time of explosion if it happens and −1 otherwise.

Input
The first line contains a single integer t (1≤t≤1000) — the number of testcases.

Then the descriptions of t testcases follow.

The first line of each testcase contains two integers n and m (1≤n≤3⋅105; 2≤m≤108) — the number of robots and the coordinate of the right wall.

The second line of each testcase contains n integers x1,x2,…,xn (0<xi<m) — the starting coordinates of the robots.

The third line of each testcase contains n space-separated characters ‘L’ or ‘R’ — the starting directions of the robots (‘L’ stands for left and ‘R’ stands for right).

All coordinates xi in the testcase are distinct.

The sum of n over all testcases doesn’t exceed 3⋅105.

Output
For each testcase print n integers — for the i-th robot output the time it explodes at if it does and −1 otherwise.

Example
inputCopy
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
outputCopy
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
Note
Here is the picture for the seconds 0,1,2 and 3 of the first testcase:
在这里插入图片描述
Notice that robots 2 and 3 don’t collide because they meet at the same point 2.5, which is not integer.

After second 3 robot 6 just drive infinitely because there’s no robot to collide with.

分析:

在0-m的数轴上,有n个机器人,每个机器人初始在坐标xi上(各不相同),向左或向右移动,花费一秒移动1。若遇到0或m,则改变方向。如果机器人同时移动到同一个整数位置,那么这些机器人就会爆炸。求每个机器人爆炸的时间,如果不会爆炸,就输出-1.
发现只有初始坐标同为奇数或者同为偶数的机器人才会相撞,所以根据机器人初始坐标的奇偶分别处理。
两个机器人相撞,一个机器人p初始位置为x1,方向为d1,另一个机器人q初始位置为x2,方向为d2(x1<x2)
(1)d1是向右,d2是向左:(x2-x1)/2

(2)d1是向右,d2是向右,那么q遇到m后改变方向与p相撞:m-x2+(m-(x1+m-x2)/2),化简得:(2*m-x1-x2)/2

(3)d1是向左,d2是向左,那么p遇到0后改变方向与q相撞:x1+(x2-x1)/2化简得:(x2+x1)/2

(4)d1是向左,d2是向右:m-x2+(m-(m-x2-x1)/2),化简得:(2*m+x1-x2)/2

#include<bits/stdc++.h>
using namespace std;
#define ll unsigned long long
const int N = 3e5+10;
struct node
{
	int x,d,t;
}e[N];
int l[N],a[N],n,m;
bool cmp(int x,int y)
{
	return e[x].x<e[y].x;
}
void ff(int z)
{
	int p=0,t;
	for(int i=1;i<=n;i++)
	{
		t=l[i];
		if(e[t].x%2==z)
		{
			if(e[t].d==1||!p)
			{
				a[++p]=t;
			}
			else
			{
				e[t].t=e[a[p]].t=(e[t].x-e[a[p]].x*e[a[p]].d)/2;
				p--;
			}
		}
	}
	for(;p>1;p=p-2)
	{
		e[a[p]].t=e[a[p-1]].t=(2*m-e[a[p]].x-e[a[p-1]].x*e[a[p-1]].d)/2;
	}
	if(p)
	e[a[p]].t=-1;
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&e[i].x);
			l[i]=i;
		}
		char s;
		for(int i=1;i<=n;i++)
		{
			cin>>s;
			e[i].d=s=='R'?1:-1;
		}
		sort(l+1,l+1+n,cmp);
		ff(0),ff(1);
		for(int i=1;i<=n;i++)
		{
			printf("%d ",e[i].t);
		}
		printf("\n");
	}
	return 0;
}

内容概要:本文详细介绍了施耐德M580系列PLC的存储结构、系统硬件架构、上电写入程序及CPU冗余特性。在存储结构方面,涵盖拓扑寻址、Device DDT远程寻址以及寄存器寻址三种方式,详细解释了不同类型的寻址方法及其应用场景。系统硬件架构部分,阐述了最小系统的构建要素,包括CPU、机架和模块的选择与配置,并介绍了常见的系统拓扑结构,如简单的机架间拓扑和远程子站以太网菊花链等。上电写入程序环节,说明了通过USB和以太网两种接口进行程序下载的具体步骤,特别是针对初次下载时IP地址的设置方法。最后,CPU冗余部分重点描述了热备功能的实现机制,包括IP通讯地址配置和热备拓扑结构。 适合人群:从事工业自动化领域工作的技术人员,特别是对PLC编程及系统集成有一定了解的工程师。 使用场景及目标:①帮助工程师理解施耐德M580系列PLC的寻址机制,以便更好地进行模块配置和编程;②指导工程师完成最小系统的搭建,优化系统拓扑结构的设计;③提供详细的上电写入程序指南,确保程序下载顺利进行;④解释CPU冗余的实现方式,提高系统的稳定性和可靠性。 其他说明:文中还涉及一些特殊模块的功能介绍,如定时器事件和Modbus串口通讯模块,这些内容有助于用户深入了解M580系列PLC的高级应用。此外,附录部分提供了远程子站和热备冗余系统的实物图片,便于用户直观理解相关概念。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值