#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
const ll MAXN = 2e5 + 10;
const ll MAXA = 4e18 + 10;
const ll MAXX = 1e9 + 10;
struct update
{
ll idx;
ll val;
ll old;
update()
{
idx = 0;
val = old = -1;
}
update(ll _idx, ll _val, ll _old)
{
idx = _idx;
val = _val;
old = _old;
}
};
ll n, x;
ll a[MAXN];
vector < update > updates;
ll lis[MAXN];
ll lis_suff[MAXN];
ll ans;
void read()
{
cin >> n >> x;
for(ll i = 0; i < n; i++)
{
cin >> a[i];
}
}
void find_lis_suff()
{
ll len = 1;
lis_suff[0] = -MAXA;
lis_suff[1] = -a[n - 1];
updates.push_back(update(1, a[n - 1], 0));
for(ll i = n - 2; i >= 0; i--)
{
ll j = lower_bound(lis_suff, lis_suff + len + 1, -a[i]) - lis_suff;
updates.push_back(update(j, a[i], -lis_suff[j]));
lis_suff[j] = -a[i];
len = max(len, j);
}
ans = len;
/*
for(update u : updates)
{
cout << "Change at position " << u.idx << " : from " << u.old << " to " << u.val << endl;
cout << "idx : " << u.idx << endl;
cout << "old : " << u.old << endl;
cout << "val : " << u.val << endl;
}
*/
for(ll i = 1; i <= n; i++)
{
lis_suff[i] *= -1;
}
}
void rollback()
{
assert(!updates.empty());
update u = updates.back();
lis_suff[u.idx] = u.old;
updates.pop_back();
}
ll bs(ll k)
{
ll left = 0;
ll right = n + 1;
ll mid;
while(right - left > 1)
{
mid = left + (right - left) / 2;
if(lis_suff[mid] != 0 && lis_suff[mid] > k)
left = mid;
else
right = mid;
}
return left;
}
void check(ll len)
{
for(int i = len - 100; i <= len; i++)
{
ll val = lis[i] - x;
ll pos = bs(val);
ans = max(ans, i + pos);
}
}
void print()
{
cout << "Reversed LIS : " << endl;
for(ll i = 1; i <= n; i++)
{
cout << lis_suff[i] << " ";
}
cout << endl << endl;
cout << "Normal LIS : " << endl;
for(ll i = 1; i <= n; i++)
{
cout << lis[i] << " ";
}
cout << endl << endl;
}
void solve()
{
ll len = 1;
lis[0] = -MAXA;
lis[1] = a[0];
rollback();
/// print();
check(len);
for(ll i = 1; i < n; i++)
{
ll j = lower_bound(lis, lis + len + 1, a[i]) - lis;
lis[j] = a[i];
len = max(len, j);
rollback();
///print();
check(len);
}
ans = max(ans, len);
/// cout << "Answer : " << ans << endl;
cout << ans << endl;
}
int main()
{
ios_base :: sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
read();
find_lis_suff();
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
2 ms |
4440 KB |
Output is correct |
20 |
Correct |
2 ms |
4560 KB |
Output is correct |
21 |
Correct |
3 ms |
4444 KB |
Output is correct |
22 |
Correct |
2 ms |
4620 KB |
Output is correct |
23 |
Correct |
2 ms |
4444 KB |
Output is correct |
24 |
Correct |
2 ms |
4624 KB |
Output is correct |
25 |
Correct |
2 ms |
4444 KB |
Output is correct |
26 |
Correct |
2 ms |
4628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
391 ms |
10180 KB |
Output is correct |
2 |
Correct |
378 ms |
10956 KB |
Output is correct |
3 |
Correct |
371 ms |
11156 KB |
Output is correct |
4 |
Correct |
375 ms |
10444 KB |
Output is correct |
5 |
Correct |
335 ms |
11176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
5840 KB |
Output is correct |
2 |
Correct |
80 ms |
5836 KB |
Output is correct |
3 |
Correct |
86 ms |
5836 KB |
Output is correct |
4 |
Correct |
80 ms |
4312 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
75 ms |
4264 KB |
Output is correct |
7 |
Correct |
79 ms |
5836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
151 ms |
7884 KB |
Output is correct |
2 |
Correct |
146 ms |
7112 KB |
Output is correct |
3 |
Correct |
337 ms |
10692 KB |
Output is correct |
4 |
Correct |
304 ms |
10744 KB |
Output is correct |
5 |
Correct |
156 ms |
6068 KB |
Output is correct |
6 |
Correct |
288 ms |
10164 KB |
Output is correct |
7 |
Correct |
313 ms |
10956 KB |
Output is correct |
8 |
Correct |
159 ms |
7112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
2 ms |
4440 KB |
Output is correct |
20 |
Correct |
2 ms |
4560 KB |
Output is correct |
21 |
Correct |
3 ms |
4444 KB |
Output is correct |
22 |
Correct |
2 ms |
4620 KB |
Output is correct |
23 |
Correct |
2 ms |
4444 KB |
Output is correct |
24 |
Correct |
2 ms |
4624 KB |
Output is correct |
25 |
Correct |
2 ms |
4444 KB |
Output is correct |
26 |
Correct |
2 ms |
4628 KB |
Output is correct |
27 |
Correct |
391 ms |
10180 KB |
Output is correct |
28 |
Correct |
378 ms |
10956 KB |
Output is correct |
29 |
Correct |
371 ms |
11156 KB |
Output is correct |
30 |
Correct |
375 ms |
10444 KB |
Output is correct |
31 |
Correct |
335 ms |
11176 KB |
Output is correct |
32 |
Correct |
83 ms |
5840 KB |
Output is correct |
33 |
Correct |
80 ms |
5836 KB |
Output is correct |
34 |
Correct |
86 ms |
5836 KB |
Output is correct |
35 |
Correct |
80 ms |
4312 KB |
Output is correct |
36 |
Correct |
1 ms |
4444 KB |
Output is correct |
37 |
Correct |
75 ms |
4264 KB |
Output is correct |
38 |
Correct |
79 ms |
5836 KB |
Output is correct |
39 |
Correct |
151 ms |
7884 KB |
Output is correct |
40 |
Correct |
146 ms |
7112 KB |
Output is correct |
41 |
Correct |
337 ms |
10692 KB |
Output is correct |
42 |
Correct |
304 ms |
10744 KB |
Output is correct |
43 |
Correct |
156 ms |
6068 KB |
Output is correct |
44 |
Correct |
288 ms |
10164 KB |
Output is correct |
45 |
Correct |
313 ms |
10956 KB |
Output is correct |
46 |
Correct |
159 ms |
7112 KB |
Output is correct |
47 |
Correct |
191 ms |
8140 KB |
Output is correct |
48 |
Correct |
181 ms |
8648 KB |
Output is correct |
49 |
Correct |
374 ms |
12992 KB |
Output is correct |
50 |
Correct |
347 ms |
11980 KB |
Output is correct |
51 |
Correct |
248 ms |
11208 KB |
Output is correct |
52 |
Correct |
341 ms |
12716 KB |
Output is correct |
53 |
Correct |
319 ms |
12488 KB |
Output is correct |
54 |
Correct |
373 ms |
12724 KB |
Output is correct |
55 |
Correct |
358 ms |
12220 KB |
Output is correct |