AcWing 4619. 减法操作
原题链接
中等
作者:
no_one
,
2022-09-24 20:51:56
,
所有人可见
,
阅读 160
模拟整个思路,具体见代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 200010;
bool yin[N];
int a[N];
int n;
int main()
{
cin >> n;
for(int i = 0; i < n; i ++)
{
int x;
cin >> x;
if(x >= 2 && x % 2 == 0) yin[i] = true;
x = x % 2;
a[i] = x;
}
bool res = true;
for(int i = 0; i < n; i ++)
{
if(a[i] == 1 && a[i + 1] == 1)
{
a[i] = 0;
a[i + 1] = 0;
continue;
}
if(a[i] == 1 && a[i + 1] == 0)
{
if(!yin[i + 1])
{
res = false;
break;
}
if(yin[i + 1])
{
if(a[i + 2] != 1 && !yin[i + 2])
{
res = false;
break;
}
}
if(yin[i + 1] && a[i + 2] == 1)
{
a[i] = 0;
a[i + 1] = 0;
a[i + 2] = 0;
}
if(yin[i + 1] && yin[i + 2])
{
a[i] = 0;
a[i + 1] = 0;
a[i + 2] = 1;
}
}
}
if(res) cout << "YES" << endl;
else puts("NO");
return 0;
}