C. Robot Collisions(思维)

2023-10-22 18:40
文章标签 思维 robot collisions

本文主要是介绍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;
}

这篇关于C. Robot Collisions(思维)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/262982

相关文章

颠覆你的开发模式:敏捷思维带来的无限可能

敏捷软件开发作为现代软件工程的重要方法论,强调快速响应变化和持续交付价值。通过灵活的开发模式和高效的团队协作,敏捷方法在应对动态变化和不确定性方面表现出色。本文将结合学习和分析,探讨系统变化对敏捷开发的影响、业务与技术的对齐以及敏捷方法如何在产品开发过程中处理持续变化和迭代。 系统变化对敏捷软件开发的影响 在敏捷软件开发中,系统变化的管理至关重要。系统变化可以是需求的改变、技术的升级、

0906作业+思维导图梳理

一、作业: 1、创捷一个类似于qq登录的界面 1)源代码 #include "widget.h"#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget){ui->setupUi(this);//QPushbutton:登录、退出this->join = new QP

[机缘参悟-222] - 系统的重构源于被动的痛苦、源于主动的精进、源于进化与演进(软件系统、思维方式、亲密关系、企业系统、商业价值链、中国社会、全球)

目录 前言:系统的重构源于被动的痛苦、源于主动的精进、源于进化与演进 一、软件系统的重构 1、重构的定义与目的 2、重构的时机与方法 3、重构的注意事项 4、重构的案例分析 二、大脑思维的重构 1、大脑思维重构的定义 2、大脑思维重构的方法 3、大脑思维重构的挑战与前景 三、认知的重构 1、定义 2、目的 3、方法 四、实例 五、总结 四、婚姻家庭的重构 1、婚

每日一题~cf 970 div3 (A思维,B小模拟,C二分,D排列数建图成环,E 26个字母暴力+前缀和,F 逆元,G 数论gcd )

A 题意: 有 a 个1 ,b 个2.问是否能将这些数划分为两个数值相等的集合。 输出 YES 或者 NO —————— 问题等价于 将数组 分成两个数值相同的数组。所以sum 应该是偶数。也就是说 1 的个数是偶数。在i1的个数是偶数的情况下,将 2 分成两份,如果2 的个数是偶数,OK。如果是奇数那么需要1来补齐,如果1 的个数大于等于2那么可以补齐。(1 的个数是偶数,需要2个1来补齐,剩下

散户炒股票为什么进步慢,学习程序化交易思维

炒股自动化:申请官方API接口,散户也可以 python炒股自动化(0),申请券商API接口 python炒股自动化(1),量化交易接口区别 Python炒股自动化(2):获取股票实时数据和历史数据 Python炒股自动化(3):分析取回的实时数据和历史数据 Python炒股自动化(4):通过接口向交易所发送订单 Python炒股自动化(5):通过接口查询订单,查询账户资产 散户炒股的常见难题

数业智能心大陆告诉你如何培养孩子的批判性思维?

现今的教育体系自小学起便强调培养孩子的批判性思维,这种能力被视为在复杂世界中生存和发展的关键。在当今信息爆炸的时代,它能让我们在海量信息中辨别真伪、深入思考并做出明智决策。如今,如数业智能心大陆产出的AI 心理咨询平台的出现为培养孩子批判性思维提供了新可能,其通过互动引导孩子思考,助力孩子提升批判性思维能力。 什么是批判性思维呢? 批判性思维是一种思考方式,它能够使我们在接收信

华为OD机试 - 最优结果的a数组数量 - 贪心思维(Java 2024 E卷 100分)

华为OD机试 2024E卷题库疯狂收录中,刷题点这里 专栏导读 本专栏收录于《华为OD机试(JAVA)真题(E卷+D卷+A卷+B卷+C卷)》。 刷的越多,抽中的概率越大,私信哪吒,备注华为OD,加入华为OD刷题交流群,每一题都有详细的答题思路、详细的代码注释、3个测试用例、为什么这道题采用XX算法、XX算法的适用场景,发现新题目,随时更新,全天CSDN在线答疑。 一、题目描述

互联网思维总结

目录 互联网八大思维 一 互联网思维 一 用户思维 二 产品思维 三 免费思维 四 社群思维 五 大数据思维 六 自媒体思维 七 跨界思维 八 平台思维 总结 互联网八大思维 简要列举以下思维,用户思维,社会化思维,大数据思维,简约思维,极致思维,跨界思维,流量思维,迭代思维,平台思维。 任何企业都可以找到一个竞争对手打,但是有一个对手是打不赢的,就是趋势,趋势一

0904作业+思维导图

一、作业 (将昨天的作业修改为标准模板类的) 1、代码 #include <iostream>#include <stack>using namespace std;//队列模板类template<typename T>class Queue{private:int max; //队列最大容量int num; //队列内元素数T *ptr; //容器

关于UML的思维导图

UML的构造块、规则、公共机制、5种视图、关系、图 综合如下: UML的构造块: UML的规则: UML的公共机制: UML的5种视图: UML的关系: UML图: