当前位置:网站首页>AcWing 1995. 见面与问候(模拟)
AcWing 1995. 见面与问候(模拟)
2022-06-12 10:49:00 【柃歌】
【题目描述】
众所周知,奶牛是非常有社交礼貌的动物:每当两头奶牛分开后相遇,它们都会用友好的“哞哞”声互相问候。
奶牛贝茜和她的朋友艾希正在农夫约翰的农场中的一条很长的道路上散步。
我们可以将此道路视为一个一维数轴。
贝茜和艾希都从原点出发,以相同的速度( 1 1 1单位距离/单位时间)行走一段时间。
请根据每头奶牛的运动情况描述,确定它们相互打招呼的次数。
贝茜和艾希可以在不同的时间点停止移动,并且她们的移动时间都不会超过 1000000 1000000 1000000单位。
【输入格式】
第一行包含两个整数 B B B和 E E E。
接下来 B B B行,描述了贝茜的移动。每行包含一个正整数后跟一个字符 L L L或 R R R,表示贝茜沿某个方向(左或者右)移动了若干单位距离。
再接下来 E E E行,描述了艾希的移动。每行包含一个正整数后跟一个字符 L L L或 R R R,表示艾希沿某个方向(左或者右)移动了若干单位距离。
【输出格式】
输出她们相互打招呼的次数。
注意,在最初位于初始位置时,她们不打招呼。
【数据范围】
1 ≤ B , E ≤ 50000 1≤B,E≤50000 1≤B,E≤50000
【输入样例】
4 5
3 L
5 R
1 L
2 R
4 R
1 L
3 L
4 R
2 L
【输出样例】
3
【样例解释】
贝茜和艾希在时间 7 , 9 , 13 7,9,13 7,9,13碰面打招呼。
【分析】
初始时两头牛都在原点,因此两头牛所在的点奇偶性相同,根据两头牛每单位时间都是移动一个单位的距离可知两头牛在同时移动的时候一定还是保持所在位置的奇偶性相同;当有一头牛停止移动时,其一定是停在整点上,因此两头牛在任何时候相遇时都一定在整点上相遇,也就是在一整个单位时间结束时相遇,而不会在半中间相遇。
那么我们可以模拟求出两头牛在每个单位时间所在的位置,然后枚举每个时刻 i i i,如果 i i i时刻两头牛的位置相同且 i − 1 i-1 i−1时刻两头牛的位置不同,说明两头牛相遇了,答案加一。
【代码】
#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;
}
边栏推荐
- A snare - Cookie spoofing
- How to refund the pre-sale deposit of JD 618 in 2022? Can JD 618 deposit be refunded?
- AcWing 135. Maximum subsequence sum (prefix sum + monotone queue to find the minimum value of fixed length interval)
- 元宇宙系统搭建与构造
- 分布式存储探索
- 890. 查找和替换模式
- Flex layout
- Class. Forname connection MySQL driver keeps throwing classnotfoundexception exception solution
- PHP can load different new data methods for each refresh
- Fiddler automatically saves the result of the specified request to a file
猜你喜欢

Malicious code analysis practice -- using apatedns and inetsim to simulate network environment

AcWing 41. Stack containing min function (monotone stack)

AcWing 128. Editor (to effectively modify the specified position in the top stack)

淺談調和形狀上下文特征HSC對3DSC的改進
![[machine learning] practice of logistic regression classification based on Iris data set](/img/c6/0233545d917691b8336f30707e4636.png)
[machine learning] practice of logistic regression classification based on Iris data set

Don't swallow rice with vinegar! Teach you 2 moves to make the fish bones "run out" safely

Malicious code analysis practice - lab06-01 exe~Lab06-04. Exe dynamic analysis

How to upload the video on the computer to the mobile phone without network

Leetcode 2169. 得到 0 的操作数

分布式存储探索
随机推荐
ID obfuscation
PHP Apple purchase verification steps
Error during session start; please check your PHP and/or webserver log file and configure your PHP
Solution to the problem that the applet developer tool cannot input simplified Chinese
Get array median
Properties Chinese garbled code
PHP generate schedule
重量级代理缓存服务器Squid
CTF freshman cup PHP deserialization question - EzPop
JS open common application market
file_ get_ Contents() JSON after reading_ Decode cannot be converted to array
Distributed storage exploration
Set SVG color
Binassii module - converting between binary and ASCII
Mysql5.6.24 installation free deployment method
Wechat payment, wechat refund, Alibaba payment
LVS基于应用层的健康状态检测
How to play the 618 super cat games on Taobao? Here comes the introduction to the overall activities of the Super Cat Games
M-Arch(番外13)GD32L233评测-来点音乐
PHP maximum balance method to solve the problem that the sum of the final percentages is not equal to 100