-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathislands.cpp
More file actions
91 lines (73 loc) · 2.16 KB
/
islands.cpp
File metadata and controls
91 lines (73 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <algorithm>
using namespace std;
struct input{
int height;
int pos;
char type;
};
bool inputcomp(input a, input b){
return a.height<b.height;
}
input islands[100000];
int main(){
int n, previnput=-1, tempvar, sizecounter=0;
cin >> n;
for(int i=0; i<n; i++){
cin >> tempvar;
if(i==0 or (i>=1 and previnput!=tempvar)){
islands[sizecounter].height=tempvar;
islands[sizecounter].pos=i;
previnput=islands[sizecounter].height;
sizecounter++;
// cout << previnput << " " << tempvar << endl;
}
}
for(int i=0; i<sizecounter; i++){
if(islands[i].pos==0){
if(islands[i].height>islands[i+1].height){
islands[i].type='P';
}
else{
islands[i].type='Z';
}
}
else if(islands[i].pos==sizecounter-1 and islands[i-1].height<islands[i].height){
islands[i].type='P';
}
else if(islands[i].height<islands[i-1].height and islands[i].height < islands[i+1].height){
islands[i].type='D';
}
else if(islands[i].height>islands[i-1].height and islands[i].height>islands[i+1].height){
islands[i].type='P';
}
else{
islands[i].type='Z';
}
}
sort(islands, islands+sizecounter, inputcomp);
// for(int i=0; i<sizecounter; i++){
// cout << islands[i].height << " " << islands[i].pos << " " << islands[i].type << endl;
// }
// return 0;
int counter=1, cur, prev=1, maxx=1;
for(int i=0; i<sizecounter; i++){
cur = islands[i].height;
if(cur!=prev){
// cout << cur << endl;
if(counter>maxx){
maxx=counter;
}
}
if(islands[i].type=='P'){
counter--;
}
else if(islands[i].type=='D'){
counter++;
}
// cout << counter << endl;
prev = islands[i].height;
}
cout << maxx << endl;
return 0;
}