题目:简单推动##
题意:你被给予两个相同长度为n的数组a[1...n]和b[1...n]
目的是进行一个推入操作,你会选择l,r,k三个数字满足1 <= l <= r <= n 还有 k > 0,
你会添加k到al,al + 1,...,ar的每个数字上
例如:a = [3, 7, 1, 4, 1, 2],选择l = 3, r = 5, k = 2,那么数组a会变成
a = [3, 7, 3, 6, 3, 2]
你可以最多进行一次操作,使得a变成b,即a和b里面的元素每个都相同
输出"YES"和"NO"
分析:要使得两个数组元素相同,可以先求出两个数组每个元素相减的值,查看这些正值是否是连续的,且只有一段连续的正值,再输出YES和NO
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
int T;
int n;
const int N = 100005;
int a[N];
int b[N];
int res[N];
int main()
{
cin >> T;
while (T--)
{
scanf("%d", &n);
int m;
for (int i = 1; i <= n; ++i)
{
scanf("%d", &m);
a[i] = m;
}
for (int i = 1; i <= n; ++i)
{
scanf("%d", &m);
b[i] = m;
}
int flag = 1;
//查看是否连续并且是否一次
for (int i = 1; i <= n; ++i)
{
res[i] = b[i] - a[i];
}
int cnt = 0;//段
int p = 0;//有负数
for (int i = 1; i <= n; ++i)
{
if (res[i] > 0)
{
while (res[i] > 0 && res[i + 1] == res[i])
{
++i;
}
++cnt;
if (cnt == 2)
{
flag = 0;
break;
}
}
else if (res[i] < 0)
{
flag = 0;
break;
}
}
if (flag == 0)
{
puts("NO");
}
else{
puts("YES");
}
}
return 0;
}