#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector <vector<int>> vvi;
typedef vector<bool> vb;
typedef vector <vector<bool>> vvb;
typedef vector <string> vs;
typedef vector<char> vc;
typedef vector <vector<char>> vvc;
typedef pair<int, int> pii;
typedef vector <pii> vpii;
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define endl "\n"
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
// freopen("warming.in", "r", stdin);
// freopen("warming.out", "w", stdout);
int n;
cin >> n;
vi h(n);
set<int> sh;
vpii sh2(n);
for(int i = 0; i < n; i++) {
cin >> h[i];
sh2[i].first = h[i];
sh2[i].second = i;
sh.insert(h[i]);
}
sort(all(sh2));
int mx_islands = 0;
if(sh.size()<=20) {
sh.insert(0);
for(int v : sh) {
int islands = 0;
for(int i = 0; i < n; i++) {
if(max(0, h[i]-v)>0 && (i==0 || max(0, h[i-1]-v)==0)) islands++;
}
mx_islands = max(mx_islands, islands);
}
} else {
int islands = 0;
mx_islands = 0;
for(int i = 0; i < n; i++) {
if(max(0, h[i])>0 && (i==0 || max(0, h[i-1])==0)) islands++;
}
mx_islands = max(mx_islands, islands);
for(int i = 0; i < n; i++) {
if(sh2[i].first==sh2[i-1].first && abs(sh2[i].second-sh2[i-1].second)==1) continue;
int s = sh2[i].second;
int e = sh2[i].second;
while(e+1<n && h[e] == h[e+1]) e++;
if(s==0 && e == n-1) {
islands = 0;
} else if(s==0 && h[e+1]<h[e]) {
islands--;
} else if(e==n-1 && h[s-1]<h[s]) {
islands--;
} else {
if(h[s-1]>h[s] && h[e+1]>h[e]) {
islands++;
mx_islands = max(mx_islands, islands);
} else if(h[s-1]<h[s] && h[e+1]<h[e]) {
islands--;
}
}
}
mx_islands = max(mx_islands, islands);
}
cout << mx_islands << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
1644 KB |
Output is correct |
2 |
Correct |
23 ms |
1628 KB |
Output is correct |
3 |
Correct |
20 ms |
1628 KB |
Output is correct |
4 |
Correct |
19 ms |
1644 KB |
Output is correct |
5 |
Correct |
20 ms |
1628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
6276 KB |
Output is correct |
2 |
Correct |
24 ms |
6232 KB |
Output is correct |
3 |
Correct |
35 ms |
6276 KB |
Output is correct |
4 |
Correct |
30 ms |
6224 KB |
Output is correct |
5 |
Correct |
36 ms |
6228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
651 ms |
58960 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
512 ms |
38992 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |