forked from bhavyahoda/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
194 lines (170 loc) · 3.71 KB
/
stack.cpp
File metadata and controls
194 lines (170 loc) · 3.71 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// || || |____| //
// || || || //
// \\__// || //
#include<bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#include <bitset>
#define mp make_pair
#define vll vector <ll>
#define pb push_back
#define M 1000000007
#define INT_BITS 32
#define SIZE_INT sizeof(int) * 8
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define test ll t;read(t);while(t--)
#define f(a, b) for (ll i = a; i < b; i++)
#define fr(a, b) for (ll j = a; j >= b; j--)
#define fi(a, b) for (ll j = a; j < b; j++)
#define fii(a, b) for (ll k = a; k < b; k++)
#define forab(i,a,b,c) for(ll (i) = a ; (i) <= (b) ; (i)+=(c))
using namespace std;
template <typename T>
void read(T &x){
cin >> x;
}
template <typename T , typename T0>
void read(T &x,T0 &y){
cin >> x >> y;
}
template <typename T , typename T0 , typename T1>
void read(T &x,T0 &y,T1 &z){
cin >> x >> y >> z;
}
template <typename T , typename T0 , typename T1 , typename T2>
void read(T &x,T0 &y,T1 &z,T2 &w){
cin >> x >> y >> z >> w;
}
template <typename T>
void write1(T &x){
cout << x << " ";
}
template <typename T>
void write(T &x){
cout << x << "\n";
}
template <typename T , typename T0>
void write(T &x,T0 &y){
cout << x << " " << y << "\n";
}
template <typename T , typename T0 , typename T1>
void write(T &x,T0 &y,T1 &z){
cout << x << " " << y << " " << z << "\n";
}
template <typename T , typename T0 , typename T1 , typename T2>
void write(T &x,T0 &y,T1 &z,T2 &w){
cout << x << " " << y << " " << z << " " << w << "\n";
}
//GCD of 2 numbers START
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
//LCM of two numbers STARTS
ll lcm(ll a, ll b){
return a / __gcd(a, b) * b;
}
//Check Prime STARTS
int isPrime(ll n)
{
if((n%2==0 && n!=2) || n==1)
return 0;
for (ll i=3;i*i<=n;i+=2)
{
if (n%i==0)
return 0;
}
return 1;
}
//BINARY SEARCH STARTS
int BS(int a[], int l, int r, int find)
{
if (l<=r)
{
int mid=(l + r)/2;
if (a[mid]==find)
return mid ;
if (a[mid]>find)
return BS(a,l,mid-1,find);
if (a[mid]<find)
return BS(a,mid+1,r,find);
}
return -1;
}
//POWER function STARTS
ll power(ll x, ll y)
{
ll temp;
if( y == 0)
return 1;
temp = power(x, y/2);
if (y%2 == 0)
return temp*temp;
else
return x*temp*temp;
}
//BINARY TO DECIMAL STARTS
int binaryToDecimal(ll n)
{
ll num = n, dec_value = 0,base = 1;
ll temp = num;
while (temp) {
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base;
base = base * 2;
}
return dec_value;
}
//Count set Bits function STARTS
ll countSetBits(ll n) {
if (n == 0)
return 0;
else
return 1 + countSetBits(n & (n - 1));
}
//START CODE
int main()
{
fast;
ll n; cin>>n;
ll depth=0,posdepth=0,length=0,poslength=0;
stack<int> s;
ll size,top;
f(1,n+1)
{
ll a; cin>>a;
if(a==1)
{
s.push(i);
size=s.size();
if(size>depth)
{
depth=size;
posdepth=i;
}
}
// 1 2 1 1 2 2 1 2 1 1 2 1 2 2 1 1 2 1 2 2
// 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// 1 1 2 1 2 2
// 1 2 3 4 5 6
else if(a==2)
{
top=s.top();
s.pop();
if(s.empty())
{
size=i-top+1;
if(size>length)
{
length=size;
poslength=top;
}
}
}
}
write(depth,posdepth,length,poslength);
return 0;
}