公式
B - Music Player 解説 by en_translator
We can simulate the operations while maintaining two variables representing the current volume and whether the music is currently playing.
Let \(x\) be the variable for the current volume, and \(y\) be a variable taking \(y = 0\) if the music is stopped and \(y = 1\) if it is playing. Then \(x\) and \(y\) transition as follows:
- If \(A_i = 1\), set \(x\) to \(x + 1\).
- If \(A_i = 2\), if \(x > 0\), set \(x\) to \(x - 1\), and do nothing otherwise.
- If \(A_i = 3\), set \(y\) to \(1-y\).
Note that setting \(y\) to \(1-y\) flips it between \(0\) and \(1\).
After each operation, the answer is Yes if \(x \geq 3\) and \(y = 1\), and No otherwise.
Sample code
#include <bits/stdc++.h>
using namespace std;
int main() {
int x = 0, y = 0;
int q;
cin >> q;
while (q--) {
int a;
cin >> a;
if (a == 1) x++;
else if (a == 2 && x > 0) x--;
else if (a == 3) y = 1 - y;
if (x >= 3 && y == 1) cout << "Yes\n";
else cout << "No\n";
}
}
投稿日時:
最終更新: