-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB_Approximating_a_Constant_Range.cpp
More file actions
282 lines (252 loc) · 6.12 KB
/
B_Approximating_a_Constant_Range.cpp
File metadata and controls
282 lines (252 loc) · 6.12 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//onAC(ALPHA) =>{Target Next ICPC ^-^}
// ^-^ ^-^ ^-^ ^-^ ^-^
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef double dl;
#define PB push_back
#define F first
#define S second
#define MP make_pair
#define endl '\n'
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define mid(l, r) ((r + l) / 2)
#define left(node) (node * 2)
#define right(node) (node * 2 + 1)
#define mx_int_prime 999999937
#define arrin(v,n) for(int i=0;i<n;i++)cin>>v[i];
const double PI = (2.0 * acos(0.0));
const double eps = 1e-9;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define MOD 1000000007
const int N=1e6+123;
bitset<N> is_prime;
#define mem(a, b) memset(a, b, sizeof(a))
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a * (b / gcd(a, b)))
#define sqr(a) ((a) * (a))
#define optimize() \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define fraction() \
cout.unsetf(ios::floatfield); \
cout.precision(10); \
cout.setf(ios::fixed, ios::floatfield);
#define file() \
freopen("inputf.in", "r", stdin); \
freopen("outpuft.out", "w", stdout);
typedef vector<int>::iterator vit;
typedef set<int>::iterator sit;
int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
// int dx[] = {+1, 0, -1, 0, +1, +1, -1, -1};
// int dy[] = {0, +1, 0, -1, +1, -1, +1, -1};
template <typename F, typename S>
ostream &operator<<(ostream &os, const pair<F, S> &p)
{
return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v)
{
os << "{";
for (auto it = v.begin(); it != v.end(); ++it)
{
if (it != v.begin())
os << ", ";
os << *it;
}
return os << "}";
}
template <typename T>
ostream &operator<<(ostream &os, const set<T> &v)
{
os << "[";
for (auto it = v.begin(); it != v.end(); ++it)
{
if (it != v.begin())
os << ", ";
os << *it;
}
return os << "]";
}
template <typename T>
ostream &operator<<(ostream &os, const multiset<T> &v)
{
os << "[";
for (auto it = v.begin(); it != v.end(); ++it)
{
if (it != v.begin())
os << ", ";
os << *it;
}
return os << "]";
}
template <typename F, typename S>
ostream &operator<<(ostream &os, const map<F, S> &v)
{
os << "[";
for (auto it = v.begin(); it != v.end(); ++it)
{
if (it != v.begin())
os << ", ";
os << it->first << " = " << it->second;
}
return os << "]";
}
#define dbg(args...) \
do \
{ \
cerr << #args << " : "; \
faltu(args); \
} while (0)
void faltu()
{
cerr << endl;
}
template <typename T>
void faltu(T a[], int n)
{
for (int i = 0; i < n; ++i)
cerr << a[i] << ' ';
cerr << endl;
}
void seive(){
is_prime.set();
is_prime[0] = is_prime[1] = false;
for (int p = 2; p * p <= N; ++p) {
if (is_prime[p]) {
for (int i = p * p; i <= N; i += p)
is_prime[i] = false;
}
}
}
template <typename T, typename... hello>
void faltu(T arg, const hello &...rest)
{
cerr << arg << ' ';
faltu(rest...);
}
ll pw(ll base , ll ex){
ll r=1;
base%=MOD;
while(ex>0){
if(ex&1) r=(r*base)%MOD;
base=(base*base)%MOD;
ex>>=1;
}
return r;
}
vector<ll> dijkstra(int n, vector<vector<pair<int,int>>>& g, int src) {
const ll INF = 1e18;
vector<ll> dist(n, INF);
priority_queue<pair<ll,int>, vector<pair<ll,int>>, greater<pair<ll,int>>> pq;
dist[src] = 0;
pq.push({0, src});
while (!pq.empty()) {
auto [d,u] = pq.top(); pq.pop();
if (d != dist[u]) continue;
for (auto [v,w] : g[u]) {
if (dist[u] + w < dist[v]) {
dist[v] = dist[u] + w;
pq.push({dist[v], v});
}
}
}
return dist;
}
vector<ll> prefix_sum(const vector<int>& a) {
int n = a.size();
vector<ll> p(n+1,0);
for (int i = 1; i <= n; i++)
p[i] = p[i-1] + a[i-1];
return p;
}
ll gcdll(ll a, ll b) {
while (b) {
ll t = a % b;
a = b;
b = t;
}
return a;
}
ll lcmll(ll a, ll b) {
return a / gcdll(a, b) * b;
}
ll mod_add(ll a, ll b, ll mod) {
a += b;
if (a >= mod) a -= mod;
return a;
}
ll mod_sub(ll a, ll b, ll mod) {
a -= b;
if (a < 0) a += mod;
return a;
}
ll MEX(const vector<ll>& arr) {
ll n = (ll)arr.size();
vector<char> seen(n + 1, 0);
for (ll x : arr) {
if (0 <= x && x <= n) seen[(ll)x] = 1;
}
for (ll i = 0; i <= n; ++i) {
if (!seen[i]) return i;
}
return n + 1;
}
string toBinary64(ll n) {
string s(64, '0');
for (int i = 63; i >= 0; --i) {
s[i] = (n & 1) ? '1' : '0';
n >>= 1;
}
return s;
}
void solve(){
ll n;cin>>n;
vector<ll>v(n);
arrin(v,n);
map<ll,ll>mp;
ll l=0,ans=0;
for (int i = 0; i < n; i++)
{
mp[v[i]]++;
while(!mp.empty()){
ll mn=mp.begin()->first;
ll mx=mp.rbegin()->first;
if(mx-mn<=1) break;
if (--mp[v[l]] == 0) mp.erase(v[l]);
++l;
}
ans=max(ans,i-l+1);
}
cout<<ans<<endl;
}
int main()
{
optimize();
int tc = 1;
seive();
// cin >> tc;
while (tc--)
{
solve();
}
return 0;
}