This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define pll pair<ll, ll>
#define pii pair<int, int>
#define fi first
#define se second
using namespace std;
const int N = 5e5+5;
const int M = 5e3+5;
const int mod = 1e9+7;
const ll base = 75;
const int inf = 1e9+7;
int m, n, t, b[N], c[N], d[N], cnt, ntime, id[N];
int dp[N], k, a[N];
int ans, tong;
vector<int> adj[N];
vector<pii> g[N];
vector<pii> kq;
string s;
bool vis[N];
ll pw(ll k, ll n)
{
ll total = 1;
for(; n; n >>= 1)
{
if(n&1)total = total * k % mod;
k = k * k % mod;
}
return total;
}
vector<pll> vi;
struct IT
{
int n;
vector<int> st;
IT(int _n)
{
n = _n;
st.resize(n*4+4, 0);
}
void build(int id, int l, int r, int pos, int x)
{
if(l == r)
{
st[id] = x;
return;
}
int mid = (l+r)>>1;
if(pos <= mid)build(id<<1, l, mid, pos, x);
else build(id<<1|1, mid+1, r, pos, x);
st[id] = max(st[id<<1], st[id<<1|1]);
}
void build(int pos, int x)
{
build(1, 1, n, pos, x);
}
int get(int id, int l, int r, int u, int v)
{
if(u <= l && r <= v)return st[id];
if(u > r || l > v)return 0;
int mid = (l+r)>>1;
return max(get(id<<1, l, mid, u, v), get(id<<1|1, mid+1, r, u, v));
}
int get(int l, int r)
{
return get(1, 1, n, l, r);
}
};
struct DSU
{
int n;
vector<int> lab, mn;
DSU(int _n)
{
n = _n;
lab.resize(n+1, -1);
mn.resize(n+1);
for(int i = 0; i <= n; i ++)mn[i] = i;
}
int findp(int u)
{
return lab[u] < 0 ? u : lab[u] = findp(lab[u]);
}
void hop(int u, int v)
{
u = findp(u);
v = findp(v);
if(u == v)return;
if(lab[u] > lab[v])swap(u, v);
lab[u] += lab[v];
lab[v] = u;
mn[u] = min(mn[u], mn[v]);
}
};
pll p[N];
void sol()
{
cin >> n >> k;
for(int i = 1; i <= n; i ++)
{
cin >> p[i].fi;
p[i].se = i;
}
sort(p+1, p+1+n);
set<int> pos;
IT st(n);
DSU dsu(n);
for(int i = 1; i <= n; )
{
int j = i;
while(i <= n && p[i].fi == p[j].fi)
{
auto it = pos.lower_bound(p[i].se);
if(it != pos.begin())
{
--it;
if(p[i].se-(*it) <= k)
a[i] = st.get(dsu.mn[dsu.findp(*it)], p[i].se)+ 1;
else a[i] = 1;
}
else a[i] = 1;
ans = max(ans, a[i]);
//cout << p[i].se <<" "<<p[i].fi<<" "<<a[i] << '\n';
++i;
}
for(int x = j; x < i; x ++)
{
st.build(p[x].se, a[x]);
auto it = pos.lower_bound(p[x].se);
if(it != pos.end() && (*it)-p[x].se <= k)dsu.hop(p[x].se, (*it));
if(it != pos.begin())
{
--it;
if(p[x].se-(*it) <= k)dsu.hop(p[x].se, (*it));
}
pos.insert(p[x].se);
}
}
cout << ans;
}
int main()
{
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
#define task "test"
if(fopen(task".inp", "r"))
{
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int test = 1;
//cin >> test;
while(test -- > 0)sol();
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:152:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
152 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:153:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
153 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |