#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define fi first
#define se second
const int N = 1e6 + 5;
int par[N],sz[N],h[N],f[N];
pii a[N];
void make_set(int v) {
par[v] = v;
sz[v] = 1;
}
int find_set(int v) {
if (v == par[v]) return v;
int p = find_set(par[v]);
par[v] = p;
return p;
}
void union_sets(int a,int b) {
a = find_set(a),b = find_set(b);
if (a == b) return;
if (sz[a] < sz[b]) swap(a,b);
sz[a] += sz[b];
par[b] = a;
}
int gw(int N,int *H) {
int n = N;
for (int i = 1; i <= n; i++) {
a[i].fi = H[i - 1];
a[i].se = i;
}
sort(a + 1,a + n + 1,greater<pii>());
for (int i = 1; i <= n; i++) make_set(i);
int ans = 0,cnt = 0;
for (int i = 1; i <= n; i++) {
cnt++;
if (i > 1 && f[a[i].se - 1]) {
union_sets(a[i].se - 1,a[i].se);
cnt--;
}
if (i < n && f[a[i].se + 1]) {
union_sets(a[i].se + 1,a[i].se);
cnt--;
}
f[a[i].se] = 1;
ans = max(ans,cnt);
}
return ans;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> h[i];
cout << gw(n,h) << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
2896 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
3656 KB |
Output is correct |
2 |
Incorrect |
12 ms |
3408 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
204 ms |
33152 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
207 ms |
32472 KB |
Output is correct |
2 |
Correct |
217 ms |
32584 KB |
Output is correct |
3 |
Correct |
218 ms |
32584 KB |
Output is correct |
4 |
Incorrect |
182 ms |
26696 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |