当前位置:网站首页>AcWing 1995. Meet and greet (simulation)
AcWing 1995. Meet and greet (simulation)
2022-06-12 10:55:00 【Eurya song】
【 Title Description 】
as everyone knows , Cows are very socially polite animals : Whenever two cows meet after they separate , They all use friendly “ Moo ” Greet each other .
Bessie the cow and her friend ash are walking along a long road on Farmer John's farm .
We can think of this path as a one-dimensional number axis .
Bessie and ashy both start from the origin , At the same speed ( 1 1 1 Unit distance / Unit time ) Walk for a while .
Please describe the movement of each cow , Determine how many times they greet each other .
Bessie and ash can stop moving at different time points , And they don't move longer than 1000000 1000000 1000000 Company .
【 Input format 】
The first line contains two integers B B B and E E E.
Next B B B That's ok , Describes Bessie's movement . Each line contains a positive integer followed by a character L L L or R R R, It means Bessie is in a certain direction ( Left or right ) Moved several units of distance .
Next E E E That's ok , Describes ash's movement . Each line contains a positive integer followed by a character L L L or R R R, It means Ashley is in a certain direction ( Left or right ) Moved several units of distance .
【 Output format 】
Output the number of times they greet each other .
Be careful , When initially in the initial position , They don't say hello .
【 Data range 】
1 ≤ B , E ≤ 50000 1≤B,E≤50000 1≤B,E≤50000
【 sample input 】
4 5
3 L
5 R
1 L
2 R
4 R
1 L
3 L
4 R
2 L
【 sample output 】
3
【 Sample explanation 】
Bessie and ash are in time 7 , 9 , 13 7,9,13 7,9,13 Meet and say hello .
【 analysis 】
At the beginning, both cows were at the origin , Therefore, the point parity of two cows is the same , According to the distance that two cows move one unit per unit of time, it can be seen that when two cows move at the same time, they must maintain the same parity of their positions ; When a cow stops moving , It must stop at the whole point , So whenever two cows meet, they must meet on the whole point , That is, at the end of a whole unit of time , Instead of meeting in the middle .
Then we can simulate and calculate the position of two cows in each unit time , Then enumerate each moment i i i, If i i i At all times, the two cows are in the same position and i − 1 i-1 i−1 The position of the two cows is different at all times , That means two cows met , Add one... To the answer .
【 Code 】
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1000010;
int pos1[N], pos2[N];
int n, m, res;
int main()
{
cin >> n >> m;
int t = 0;
while (n--)
{
char d;
int x, v = 1;
cin >> x >> d;
if (d == 'L') v = -1;
while (x--) t++, pos1[t] = pos1[t - 1] + v;
}
while (t < N - 1) t++, pos1[t] = pos1[t - 1];
t = 0;
while (m--)
{
char d;
int x, v = 1;
cin >> x >> d;
if (d == 'L') v = -1;
while (x--) t++, pos2[t] = pos2[t - 1] + v;
}
while (t < N - 1) t++, pos2[t] = pos2[t - 1];
for (int i = 1; i < N; i++)
if (pos1[i] == pos2[i] && pos1[i - 1] != pos2[i - 1]) res++;
cout << res << endl;
return 0;
}
边栏推荐
- How to play the 2022 Taobao 618 Super Cat Games? Playing skills of 2022 Taobao 618 Cat Games
- 2021-03-24
- On the improvement of 3dsc by harmonic shape context feature HSC
- AcWing 128. Editor (to effectively modify the specified position in the top stack)
- AcWing 1995. 见面与问候(模拟)
- A hundred secrets and a few secrets - Caesar encryption
- PHP Apple purchase verification steps
- 淺談調和形狀上下文特征HSC對3DSC的改進
- Reading mysql45 lecture - self summary (part)
- JS scale down the width and height of the picture
猜你喜欢

AcWing 135. Maximum subsequence sum (prefix sum + monotone queue to find the minimum value of fixed length interval)

MYSQL——内置函数

2022京東618預售定金怎麼退?京東618定金能退嗎?

Leetcode 2169. Get operands of 0

How to refund the pre-sale deposit of JD 618 in 2022? Can JD 618 deposit be refunded?

Module 8 job

分布式存储探索

Leetcdoe 2037. 使每位学生都有座位的最少移动次数(可以,一次过)

Leetcode2154. Multiply the found value by 2 (binary search)

Machine learning is not something you can use if you want to use it
随机推荐
The solution of Lenovo notebook ThinkPad t440 WiFi dropping all the time
淺談調和形狀上下文特征HSC對3DSC的改進
What can QA do in a "de QA" project?
FPGA-按键实验
自然语言处理nlp 数据集下载地址
Valentina Studio Pro for Mac(mac数据库管理软件)
Vscode code debugging skills
Malicious code analysis practice - lab03-01 Exe basic dynamic analysis
Handwritten common interview questions
AcWing 128. Editor (to effectively modify the specified position in the top stack)
Tp6+memcached configuration
On 3dsc theory and application of 3D shape context feature
元宇宙链游与传统游戏的区别
Global and local existence of array, integer and character variables
A few secrets - a special day
How to upload the video on the computer to the mobile phone without network
Why check the @nonnull annotation at run time- Why @Nonnull annotation checked at runtime?
Leetcode 2169. 得到 0 的操作数
Flex layout
PHP Apple internal purchase callback processing