#include <bits/stdc++.h>
#define FOR(i, begin, end) for(int i = (begin); i < (end); i++)
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr)
#define f first
#define s second
#define pb push_back
#define sz(x) ((int)((x).size()))
#define le(vec) vec[vec.size()-1]
#define all(x) (x).begin(), (x).end()
#define TSTS int ttt; cin >> ttt; while(ttt--) solve()
using namespace std;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long double, long double> pdd;
typedef map<int, int> mii;
typedef vector<int> vi;
typedef long double ld;
typedef long long ll;
const int INF=1e9, MOD=1e9+7, mod=998244353;
const ll LINF=1e18;
void setIO()
{
FAST_IO;
}
void setIO(string s)
{
FAST_IO;
freopen((s+".in").c_str(), "r", stdin);
freopen((s+".out").c_str(), "w", stdout);
}
#define int long long
const int N=2e5+10;
int n, xx, a[N], d[N], nd[N], t[4*N], idx;
multiset<int, greater<int>> vals[N];
set<int> st;
mii c, rec;
void build(int l=1, int r=idx, int id=1)
{
assert(l<=r);
if(l==r){
if(!vals[l].empty())
t[id]=*vals[l].begin();
else t[id]=0;
return;
}
int m=(l+r)/2;
build(l, m, id*2);
build(m+1, r, id*2+1);
t[id]=max(t[id*2], t[id*2+1]);
}
void rem(int p, int x, int l=1, int r=idx, int id=1)
{
assert(l<=r);
if(l==r){
assert(!vals[p].empty());
vals[p].erase(vals[p].find(x));
if(vals[p].empty()) st.erase(rec[p]);
t[id]=*vals[p].begin();
return;
}
int m=(l+r)/2;
if(p<=m) rem(p, x, l, m, id*2);
else rem(p, x, m+1, r, id*2+1);
t[id]=max(t[id*2], t[id*2+1]);
}
int get(int x, int y, int l=1, int r=idx, int id=1)
{
if(x>r||y<l) return 0;
if(x<=l&&r<=y) return t[id];
int m=(l+r)/2;
return max(
get(x, y, l, m, id*2),
get(x, y, m+1, r, id*2+1)
);
}
signed main()
{
setIO();
cin >> n >> xx;
FOR(i, 0, n) cin >> a[i], st.insert(a[i]);
for(auto x : st) c[x]=++idx, rec[idx]=x;
vi lds;
for(int i=n-1; i>=0; i--){
int l=0, r=sz(lds);
while(l<r){
int m=(l+r)/2;
if(lds[m]>a[i]) l=m+1;
else r=m;
}
if(l==sz(lds)) lds.pb(a[i]);
else lds[l]=a[i];
d[i]=sz(lds);
nd[i]=le(lds);
vals[c[nd[i]]].insert(d[i]);
}
FOR(i, 1, idx+1) if(vals[i].empty()) st.erase(rec[i]);
build();
vi lis; int ans=sz(lds);
FOR(i, 0, n)
{
assert(t[1]!=0);
rem(c[nd[i]], d[i]);
a[i]-=xx;
int in=lower_bound(all(lis), a[i])-lis.begin();
if(in==sz(lis)) lis.pb(a[i]);
else lis[in]=a[i];
int lst=le(lis), mx=0;
if(st.upper_bound(lst)!=st.end())
mx=get(c[*st.upper_bound(lst)], idx);
ans=max(ans, mx+sz(lis));
}
cout << ans;
}
Compilation message
glo.cpp: In function 'void setIO(std::string)':
glo.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen((s+".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
glo.cpp:34:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | freopen((s+".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Correct |
5 ms |
9676 KB |
Output is correct |
3 |
Correct |
7 ms |
9676 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
5 |
Correct |
5 ms |
9676 KB |
Output is correct |
6 |
Correct |
5 ms |
9664 KB |
Output is correct |
7 |
Correct |
5 ms |
9676 KB |
Output is correct |
8 |
Correct |
4 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9676 KB |
Output is correct |
10 |
Correct |
4 ms |
9676 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Correct |
5 ms |
9676 KB |
Output is correct |
3 |
Correct |
7 ms |
9676 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
5 |
Correct |
5 ms |
9676 KB |
Output is correct |
6 |
Correct |
5 ms |
9664 KB |
Output is correct |
7 |
Correct |
5 ms |
9676 KB |
Output is correct |
8 |
Correct |
4 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9676 KB |
Output is correct |
10 |
Correct |
4 ms |
9676 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9676 KB |
Output is correct |
13 |
Correct |
5 ms |
9736 KB |
Output is correct |
14 |
Correct |
5 ms |
9676 KB |
Output is correct |
15 |
Correct |
5 ms |
9676 KB |
Output is correct |
16 |
Correct |
5 ms |
9676 KB |
Output is correct |
17 |
Correct |
5 ms |
9676 KB |
Output is correct |
18 |
Correct |
5 ms |
9676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Correct |
5 ms |
9676 KB |
Output is correct |
3 |
Correct |
7 ms |
9676 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
5 |
Correct |
5 ms |
9676 KB |
Output is correct |
6 |
Correct |
5 ms |
9664 KB |
Output is correct |
7 |
Correct |
5 ms |
9676 KB |
Output is correct |
8 |
Correct |
4 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9676 KB |
Output is correct |
10 |
Correct |
4 ms |
9676 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9676 KB |
Output is correct |
13 |
Correct |
5 ms |
9736 KB |
Output is correct |
14 |
Correct |
5 ms |
9676 KB |
Output is correct |
15 |
Correct |
5 ms |
9676 KB |
Output is correct |
16 |
Correct |
5 ms |
9676 KB |
Output is correct |
17 |
Correct |
5 ms |
9676 KB |
Output is correct |
18 |
Correct |
5 ms |
9676 KB |
Output is correct |
19 |
Incorrect |
6 ms |
9932 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
369 ms |
56004 KB |
Output is correct |
2 |
Correct |
366 ms |
56012 KB |
Output is correct |
3 |
Correct |
341 ms |
56076 KB |
Output is correct |
4 |
Correct |
340 ms |
56076 KB |
Output is correct |
5 |
Correct |
304 ms |
40316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
21188 KB |
Output is correct |
2 |
Correct |
71 ms |
21308 KB |
Output is correct |
3 |
Correct |
71 ms |
21356 KB |
Output is correct |
4 |
Correct |
59 ms |
17368 KB |
Output is correct |
5 |
Correct |
5 ms |
9676 KB |
Output is correct |
6 |
Correct |
57 ms |
17288 KB |
Output is correct |
7 |
Correct |
71 ms |
21212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
163 ms |
32904 KB |
Output is correct |
2 |
Correct |
166 ms |
32836 KB |
Output is correct |
3 |
Correct |
370 ms |
56136 KB |
Output is correct |
4 |
Correct |
233 ms |
40300 KB |
Output is correct |
5 |
Correct |
158 ms |
33036 KB |
Output is correct |
6 |
Correct |
337 ms |
54336 KB |
Output is correct |
7 |
Correct |
311 ms |
54228 KB |
Output is correct |
8 |
Correct |
159 ms |
32836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9676 KB |
Output is correct |
2 |
Correct |
5 ms |
9676 KB |
Output is correct |
3 |
Correct |
7 ms |
9676 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
5 |
Correct |
5 ms |
9676 KB |
Output is correct |
6 |
Correct |
5 ms |
9664 KB |
Output is correct |
7 |
Correct |
5 ms |
9676 KB |
Output is correct |
8 |
Correct |
4 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9676 KB |
Output is correct |
10 |
Correct |
4 ms |
9676 KB |
Output is correct |
11 |
Correct |
5 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9676 KB |
Output is correct |
13 |
Correct |
5 ms |
9736 KB |
Output is correct |
14 |
Correct |
5 ms |
9676 KB |
Output is correct |
15 |
Correct |
5 ms |
9676 KB |
Output is correct |
16 |
Correct |
5 ms |
9676 KB |
Output is correct |
17 |
Correct |
5 ms |
9676 KB |
Output is correct |
18 |
Correct |
5 ms |
9676 KB |
Output is correct |
19 |
Incorrect |
6 ms |
9932 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |