Codeforces Round 1029 (Div. 3) Writeup
A. False Alarm
题目


思路
首先先遍历Array直到找到第一个1,即关着的门。然后这个时候使用开关,看是否能到达终点,或者是到达的位置后面是否存在关着的门。
简化下来就是:首先找到第一个关着的门的位置,然后确定最后一个关着的门的位置,最后根据他们的距离是否小于x的值来判断是否可以通行。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| #include <iostream> #include <vector> using namespace std;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr);
int t; cin >> t; while (t--) { int n, x; cin >> n >> x; vector<int> a(n); for (int &v : a) cin >> v;
int first = -1, last = -1; for (int i = 0; i < n; ++i) { if (a[i] == 1) { if (first == -1) first = i; last = i; } }
if (last - first + 1 <= x) cout << "YES\n"; else cout << "NO\n"; } return 0; }
|
B. Shrink
题目


思路
不难发现,对于任意的n,我们只需要构造一个这样的数列即可满足要求:
即前半段是奇数递增,后半段是偶数递减。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| #include <iostream> using namespace std;
int main() { int t; cin >> t; while (t--) { int n; cin >> n;
for (int i = 1; i <= n; i += 2) { cout << i << " "; }
int start = (n % 2 == 0) ? n : n - 1; for (int i = start; i >= 2; i -= 2) { cout << i << " "; }
cout << '\n'; } return 0; }
|