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>
using namespace std;
int n = 7000, p;
//long long C[] = {0, 0, 0, 0};
vector<int> wl, wr;
vector<vector<int> > dp;
void upd(int j, int i, int x)
{
if (dp[i][j] == x)
return;
//C[0]++;
dp[i][j] = x;
while (i > 1)
{//C[1]++;
dp[i >> 1][j] = min(dp[i][j], dp[i ^ 1][j]);
i >>= 1;
}
return;
}
int qmin(int j, int l, int r)
{
//C[2]++;
int an = 1000000001;
while (l < r)
{//C[3]++;
if (l & 1)
{
an = min(an, dp[l][j]);
l++;
}
l >>= 1;
if (r & 1)
{
r--;
an = min(an, dp[r][j]);
}
r >>= 1;
}
return an;
}
int main()
{
int d = 4334, i;
cin >> n >> d;
vector<int> v(n);
for (i = 0; i < n; i++)
cin >> v[i];
if (n < 7001)
{
int j, k, m = 0;
p = n;
while (p != (p & -p))
p++;
dp.resize(2 * p);
wl.resize(2 * p);
wr.resize(2 * p);
for (i = p; i < 2 * p; i++)
{
dp[i].resize(n);
for (j = 0; j < n; j++)
dp[i][j] = 1000000001;
wl[i] = i;
wr[i] = i + 1;
}
for (i = p - 1; i >= 0; i--)
{
dp[i].resize(n);
for (j = 0; j < n; j++)
dp[i][j] = 1000000001;
wl[i] = wl[2 * i];
wr[i] = wr[2 * i + 1];
}
for (i = p; i < p + n; i++)
{
for (j = 0; j <= i - p; j++)
{
if (j)
{
upd(j, i, 1000000001);
k = qmin(j, max(p, i - d), i);
upd(j, i, min(dp[i][j], k));
k = qmin(j - 1, max(p, i - d), i);
if (k < v[i - p])
upd(j, i, min(dp[i][j], k));
}
else
upd(j, i, v[i - p]);
upd(j, i, max(dp[i][j], v[i - p]));
if (i == p + n - 1 && dp[i][j] < 1000000001)
m = j;
}
}
cout << m + 1;
}
else if (d == n)
{
vector<int> li = {v[0]};
for (i = 1; i < n; i++)
{
int hi = li.size(), lo = -1, md;
while (hi - lo > 1)
{
md = (hi + lo) / 2;
if (li[md] < v[i])
lo = md;
else
hi = md;
}
if (hi < li.size())
li[hi] = v[i];
else
li.push_back(v[i]);
}
cout << li.size();
}
else if (d == 1)
{
v.push_back(1000000001);
vector<int> ne(n + 1, n), st = {n}, ml(n + 1, 0);
int m = 0;
for (i = n - 1; i >= 0; i--)
{
while (v[i] >= v[st.back()])
st.pop_back();
ne[i] = st.back();
ml[i] = ml[ne[i]] + 1;
m = max(m, ml[i]);
st.push_back(i);
}
cout << m;
}
//cout << " " << C[0] << " " << C[1] << " " << C[2] << " " << C[3] << endl;
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:109:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | if (hi < li.size())
| ~~~^~~~~~~~~~~
# | 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... |