[problem:698A]↵
↵
[user:SXWisON,2023-12-21] [user:molingspance,2023-12-21]↵
↵
### Idea↵
↵
A variable p can be used to represent the available actions, with the following rules:↵
↵
- If no action can be performed on that day, let p=0.↵
- If the input is x=1,2 and can be executed, then the possible action for p is x, that is p=x.↵
- If the input is x=3, there are two possibilities. Either both states can be performed, or only one of them can be executed. ↵
↵
If the previous value of p is 0, than the current value is 3, that means all actions can be executed.↵
↵
If the previous value of p is 1, the current value must be 2.↵
↵
Similarly, if the previous value of p is 2, the current value must be 1.↵
↵
It’s interesting that all the calculations can be expressed as p=3-p.↵
↵
It's easy to determine that a specific day cannot excute any actions is equivalent to:↵
↵
Either x=0 (indicating that no venues are open) or x=p and x is not 3. If x is not 3, it means that the only action that can be taken is exactly the same as the only action that could be taken the previous day, violating the rest principle. Therefore, on that day, no actions can be performed.↵
↵
—————————————————————— in Chinese ————————————————————↵
可以用一个变量p来表示可以执行的动作,有:↵
↵
- 如果某一天不能执行动作,表示为p=0↵
- 如果输入为x=1/2,并且可以执行,那么p可以执行的动作为x,即:p=x↵
- 如果输入为x=3,则有两种可能,两个状态都能进行,要么只能进行另一个,即↵
↵
上一刻p=0 → 此时 p=3(表示都能做↵
↵
上一个p=1 → 此时 p=2、上一刻p=2 → 此时 p=1↵
↵
注意到上述计算都可以表述为p=3-p↵
↵
那么如何判断当天是否能进行动作?### Code↵
↵
↵
~~~~~↵
#include <iostream>↵
↵
int main(int argc, char* argv[]) {↵
int n, x, p = 3, ans = 0;↵
std::cin >> n;↵
for (int i = 1; i <= n; i++) {↵
std::cin >> x;↵
if (x == 0 || (x == p && x != 3))↵
ans++, p = 0;↵
else↵
p = (x == 3) ? 3 - p : x;↵
}↵
std::cout << ans;↵
return 0;↵
}↵
更容易判断的是不能进行,即:↵
↵
要么x==0(场馆都没有开),要么x==p并且x不是3。如果x不是3,表示什么呢,此时唯一能做的事情,恰好和昨天唯一能做的事情相同,违背了休息原则,所以当天是不能进行任何动作的。~~~~~↵
↵
↵
[user:SXWisON,2023-12-21] [user:molingspance,2023-12-21]↵
↵
### Idea↵
↵
A variable p can be used to represent the available actions, with the following rules:↵
↵
- If no action can be performed on that day, let p=0.↵
- If the input is x=1,2 and can be executed, then the possible action for p is x, that is p=x.↵
- If the input is x=3, there are two possibilities. Either both states can be performed, or only one of them can be executed. ↵
↵
If the previous value of p is 0, than the current value is 3, that means all actions can be executed.↵
↵
If the previous value of p is 1, the current value must be 2.↵
↵
Similarly, if the previous value of p is 2, the current value must be 1.↵
↵
It’s interesting that all the calculations can be expressed as p=3-p.↵
↵
It's easy to determine that a specific day cannot excute any actions is equivalent to:↵
↵
Either x=0 (indicating that no venues are open) or x=p and x is not 3. If x is not 3, it means that the only action that can be taken is exactly the same as the only action that could be taken the previous day, violating the rest principle. Therefore, on that day, no actions can be performed.↵
↵
可以用一个变量p来表示可以执行的动作,有:↵
↵
- 如果某一天不能执行动作,表示为p=0↵
- 如果输入为x=1/2,并且可以执行,那么p可以执行的动作为x,即:p=x↵
- 如果输入为x=3,则有两种可能,两个状态都能进行,要么只能进行另一个,即↵
↵
上一刻p=0 → 此时 p=3(表示都能做↵
↵
上一个p=1 → 此时 p=2、上一刻p=2 → 此时 p=1↵
↵
注意到上述计算都可以表述为p=3-p↵
↵
那么如何判断当天是否能进行动作?
↵
↵
~~~~~↵
#include <iostream>↵
↵
int main(int argc, char* argv[]) {↵
int n, x, p = 3, ans = 0;↵
std::cin >> n;↵
for (int i = 1; i <= n; i++) {↵
std::cin >> x;↵
if (x == 0 || (x == p && x != 3))↵
ans++, p = 0;↵
else↵
p = (x == 3) ? 3 - p : x;↵
}↵
std::cout << ans;↵
return 0;↵
}↵
↵
要么x==0(场馆都没有开),要么x==p并且x不是3。如果x不是3,表示什么呢,此时唯一能做的事情,恰好和昨天唯一能做的事情相同,违背了休息原则,所以当天是不能进行任何动作的。
↵