Codeforces Round 1029 (Div. 3)
Dashboard - Codeforces Round 1029 (Div. 3) - Codeforces
https://codeforces.com/contest/2117/problem/A
遇到第一个门之后,使用按钮,直接把索引跳到+x后,再判断后面是否有门
#include<bits/stdc++.h>
using namespace std;int main()
{int t;cin >> t;while (t--){int a, b;cin >> a >> b;vector<int>arr(a);int flag = 1;for (int i = 0; i < a; i++){cin >> arr[i];}int c = 1;for (int i = 0; i < a; i++){if(arr[i]==1&&c==0){flag = 0;}if (arr[i] == 1&&c==1){c = 0;i = i+b-1;}}if (flag){cout << "YES" << "\n";}else{cout << "NO" << "\n";}}}
Problem - B - Codeforces
不难发现规律,先左后右,一次从0到n,往中心填数
#include<bits/stdc++.h>
using namespace std;int main()
{int t;cin >> t;while (t--){int n;cin >> n;vector<int>arr(n);int left = 0;int right = n - 1;int now = 1;while (1){arr[left] = now;now++;arr[right] = now;now++;right--;left++;if (left == right){arr[left] = now;break;}else if (left > right){break;}}for (int i = 0; i < n; i++){cout << arr[i] << " ";}cout << "\n";}}
Problem - C - Codeforces
想第一版代码的时候,想的很复杂:
我们认为第一个数就是第一个酷数组,然后用map保存,当往后访问的时候,再次遇到a,让map[a] = 0;假设移动了m个单位,我们用for(int i = 0;i<m;i++)让这段区间的数进入map,再开始
简洁解法:
稍微思考,不难发现,后面的酷数组一定包含前面所有酷数组的元素。
创建两个set,第一个收录上一个酷数组的元素,用来erase,当size为0时,说明下一个酷数组已经出现。
第二个收录当前酷数组的元素,用insert,当这个元素不在set中,就直接insert。
每当第一个set清空时,把第二个set数据给第一个
来看代码:
#include<bits/stdc++.h>
using namespace std;int main()
{int t;cin >> t;while (t--){int n;cin >> n;vector<int>arr(n);for (int i = 0; i < n; i++){cin >> arr[i];}set<int>now, last;//一开始 只有第一个now.insert(arr[0]);last = now;int cnt = 1;for (int i = 1; i < n; i++){auto it = last.find(arr[i]);if (it != last.end()){last.erase(it);if (last.empty()){cnt++;last = now;}}else{now.insert(arr[i]);}}cout << cnt << "\n";}}