当前位置:网站首页>多米诺骨牌
多米诺骨牌
2022-08-09 06:34:00 【未央吖】
A-多米诺骨牌_长沙学院2022暑假训练赛(一) (nowcoder.com)
题目描述
多米诺骨牌(domino)是一种木制、骨制或塑料制成的长方体骨牌,按一定间距排列成行,轻轻碰倒第一枚骨牌,其余的骨牌就会产生连锁反应,依次倒下。
我们将这个多米诺骨牌的连锁反应简化,假设一个骨牌受到大于 ggg 的力就会倒塌。当它因收到来自左侧的力倒塌时,它就会向右侧倒去,同时对右侧的骨牌施加大小为 aaa 的力。另一侧的方向相反,aaa 大小不变。
Bruce12138有 nnn 个多米诺骨牌排成一列,第 iii 个骨牌受到大于 gig_igi 的力后才会倒塌,倒塌后将对倒塌方向的下一个骨牌施加 aia_iai 的力。Bruce12138想要找到一个位置,这个位置在两个骨牌的中间,或者在最左侧骨牌的左边,或者在最右侧骨牌的右边。然后他会在一次推倒操作中,推倒这个位置左右两侧的骨牌(可以没有)。请问他能否通过一次推倒操作使得所有的多米诺骨牌都倒下?
能则输出"Yes",不能则输出"No",不含引号。
输入描述:
第一行一个整数 n(1≤n≤105)n(1\le n\le10^5)n(1≤n≤105) ,表示多米诺骨牌的数量。 第二行 nnn 个用空格分开的整数,表示第 iii 个骨牌能对下一个骨牌施加的力 ai(1≤ai≤105)a_i(1\le a_i\le 10^5)ai(1≤ai≤105)。 第三行 nnn 个用空格分开的整数,表示第 iii 个骨牌受到大于 gi(1≤gi≤105)g_i(1\le g_i\le 10^5)gi(1≤gi≤105) 的力就会倒塌。
输出描述:
一行一个字符串,表示能否通过一次推倒操作使得所有的多米诺骨牌都倒下。
示例1
输入
复制2 2 2 1 1
2 2 2 1 1
输出
复制Yes
Yes
说明
在任何一个位置都可以推倒全部的多米诺骨牌。
代码:
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[100005] = {}, b[100005] = {};
for (int i = 0; i < n; i++) {cin >> a[i];}
for (int i = 0; i < n; i++) {cin >> b[i];}
for (int i = 0; i < n; i++) {if (a[i] < b[i + 1] || a[i] < b[i - 1]){ cout << "No";return 0;}}
cout << "Yes";
return 0;
}
方法二比较好理解
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[100005] = {}, b[100005] = {};
for (int i = 0; i < n; i++) {cin >> a[i];}
for (int i = 0; i < n; i++) {cin >> b[i];}
int i = 0, j = n;
for (; i < n - 1; i++) {
if (a[i] <= b[i + 1])break;
}
for (; j >= 1; j--) {
if (a[j] <= b[j - 1])break;
}
int flag = 0;
if (i >= j - 1) flag = 1;
if (flag) cout << "Yes";
else cout << "No";
return 0;
}
边栏推荐
猜你喜欢
随机推荐
io.lettuce.core。RedisCommandTimeoutException命令超时
golang zip aes base64
运放-运算放大器经典应用电路大全-应用电路大全
The working principle of the transformer (illustration, schematic explanation, understand at a glance)
pycharm环境包导入到另外一个环境
Reverse Engineering
[R language] interaction test data
Use of PlantUML plugin in idea
Fragments
APP product source data interface (taobao, jingdong/spelling/suning/trill platform details a lot data analysis interface) code and docking tutorial
简单工厂模式
The AD in the library of library file suffix. Intlib. Schlib. Pcblib difference
VS2019常用快捷键
Error: flask: TypeError: 'function' object is not iterable
ByteDance Interview Questions: Mirror Binary Tree 2020
The division principle summary within the collection
ZIP压缩包文件删除密码的方法
jdepend
治疗消化性溃疡—Toronto Research Chemicals 甘氨酸铝
【Wwise】ArgumentException: The specified path is not of a legal form (empty). About the path reading error in WwiseGlobal