Solution
2024-06-16 00:39:26
发布于:广东
0阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
const int INF = 2000000000;
const int MAXN = 100005;
int color[MAXN], xmin[MAXN], xmax[MAXN], inStack[MAXN];
int n;
vector<int> s;
int main() {
scanf("%d", &n);
fill(xmin, xmin + MAXN, INF);
fill(xmax, xmax + MAXN, -INF);
color[0] = color[n+1] = 0;
for (int i = 0; i <= n+1; i++) {
if (i >= 1 && i <= n) scanf("%d", &color[i]);
xmin[color[i]] = min(xmin[color[i]], i);
xmax[color[i]] = max(xmax[color[i]], i);
}
int res = 0;
for (int i = 0; i <= n+1; i++) {
int c = color[i];
if (i == xmin[c]) {
s.push_back(c);
res = max(res, (int)s.size());
}
if (s[s.size()-1] != c) {
cout << -1 << "\n";
return 0;
}
if (i == xmax[c]) {
s.pop_back();
}
}
cout << res - 1 << "\n";
return 0;
}
这里空空如也
有帮助,赞一个