#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int n, res = 0, ans = 0;
pair <int, int> a[N];
struct disj {
int par[N];
bool mark[N];
void init (int n) {
iota(par + 1, par + n + 1, 1);
memset(mark, 0, sizeof(mark));
}
int Find (int x) {
return par[x] == x ? x : par[x] = Find(par[x]);
}
void join (int x, int y) {
x = Find(x); y = Find(y);
if (x == y) return;
par[y] = x;
mark[x] |= mark[y];
}
} dsu;
int main(){
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i].first), a[i].second = i;
sort(a + 1, a + n + 1, greater <pair <int, int> >());
int ptr = 1;
dsu.init(n);
for (int i = 1; i <= n; i = ptr) {
while (ptr <= n && a[ptr].first == a[i].first) {
if (a[ptr].second == n) {
if (dsu.mark[dsu.Find(a[ptr].second - 1)]) dsu.join(a[ptr].second, a[ptr].second - 1);
else res++, dsu.mark[a[ptr].second] = true;
}
else if (a[ptr].second == 1) {
if (dsu.mark[dsu.Find(a[ptr].second + 1)]) dsu.join(a[ptr].second, a[ptr].second + 1);
else res++, dsu.mark[a[ptr].second] = true;
}
else {
if (dsu.mark[dsu.Find(a[ptr].second - 1)]) {
if (dsu.mark[dsu.Find(a[ptr].second + 1)]) {
dsu.join(a[ptr].second, a[ptr].second - 1);
dsu.join(a[ptr].second, a[ptr].second + 1);
res--;
}
else dsu.join(a[ptr].second, a[ptr].second - 1);
}
else {
if (dsu.mark[dsu.Find(a[ptr].second + 1)]) dsu.join(a[ptr].second, a[ptr].second + 1);
else res++, dsu.mark[a[ptr].second] = true;
}
}
ptr++;
ans = max(ans, res);
}
}
printf("%d", ans);
return 0;
}
Compilation message
gw.cpp: In function 'int main()':
gw.cpp:31:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
gw.cpp:32:58: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (int i = 1; i <= n; i++) scanf("%d", &a[i].first), a[i].second = i;
~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1432 KB |
Output is correct |
2 |
Correct |
2 ms |
1272 KB |
Output is correct |
3 |
Correct |
2 ms |
1272 KB |
Output is correct |
4 |
Correct |
3 ms |
1276 KB |
Output is correct |
5 |
Correct |
3 ms |
1272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
23 ms |
2680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
3448 KB |
Output is correct |
2 |
Correct |
18 ms |
3192 KB |
Output is correct |
3 |
Correct |
28 ms |
3320 KB |
Output is correct |
4 |
Correct |
28 ms |
3320 KB |
Output is correct |
5 |
Correct |
27 ms |
3320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
297 ms |
22392 KB |
Output is correct |
2 |
Correct |
303 ms |
22392 KB |
Output is correct |
3 |
Correct |
272 ms |
22392 KB |
Output is correct |
4 |
Correct |
286 ms |
22524 KB |
Output is correct |
5 |
Correct |
261 ms |
21752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
287 ms |
21752 KB |
Output is correct |
2 |
Correct |
283 ms |
21824 KB |
Output is correct |
3 |
Correct |
300 ms |
21844 KB |
Output is correct |
4 |
Incorrect |
232 ms |
15992 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |