题目分析
题目的本意就是告诉你一串序列,根据这串序列去判断另一个序列的有多少种不同的值
方法一:模拟
直接按照题目的意思模拟即可,但是要注意先后顺序,也就是说循环遍历的顺序,最后我们直接用set统计一下有多少个不同的值就是答案
#include <iostream> #include <algorithm> #include <cstdio> #include <set> #include <map> #include <math.h> #include <vector> #include <queue> #include <string.h> typedef long long ll; using namespace std; #define pi acos(-1.0) const int maxn = 1e6 + 10; const int inf = 0x3f3f3f3f; const int mod = 1e9 + 7; int a[maxn], c[maxn]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &c[i]); a[i] = i; } for (int i = 2; i <= n; ++i) if (c[i] == 0) a[i] = a[i - 1]; for (int i = n - 1; i >= 1; --i) if (c[i]) a[i] = a[i + 1]; set<int> s; for (int i = 1; i <= n; ++i) s.insert(a[i]); printf("%d\n", s.size()); }
方法二:找规律
#include <bits/stdc++.h> using namespace std; #define maxn 500005 int n, m, ans = 1; int a[maxn]; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i < n; i++) if (a[i] == 0 && a[i + 1] == 1) ans++; cout << ans; return 0; }