#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define all(x) (x).begin() , (x).end()
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define file "name"
template <typename T1, typename T2> bool minimize(T1 &a, T2 b){if (a > b) {a = b; return true;} return false;}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b){if (a < b) {a = b; return true;} return false;}
const int inf = 1e9 + 5;
const ll linf = 1e17;
const int mod = 1e9 + 7;
const int N = 3e5 + 5;
const int LOG = 20;
int n, d, res;
int a[N], f[N];
int rmq[N][LOG + 5], lg[N];
void inp()
{
cin >> n >> d;
for(int i = 1; i <= n; ++i) cin >> a[i];
}
void prepare() {
for(int i = 2; i <= n; ++i) lg[i] = lg[i / 2] + 1;
for(int i = 1; i <= n; ++i) rmq[i][0] = a[i];
for(int j = 1; j <= LOG; ++j)
for(int i = 1; i + MASK(j) - 1 <= n; ++i) rmq[i][j] = max(rmq[i][j - 1], rmq[i + MASK(j - 1)][j - 1]);
}
int get(int l, int r) {
int k = lg[r - l + 1];
return max(rmq[l][k], rmq[r - MASK(k) + 1][k]);
}
namespace sub4 {
void prepare() {
for(int i = 2; i <= n; ++i) lg[i] = lg[i / 2] + 1;
for(int i = 1; i <= n; ++i) rmq[i][0] = a[i];
for(int j = 1; j <= LOG; ++j)
for(int i = 1; i + MASK(j) - 1 <= n; ++i) rmq[i][j] = max(rmq[i][j - 1], rmq[i + MASK(j - 1)][j - 1]);
}
int get(int l, int r) {
int k = lg[r - l + 1];
return max(rmq[l][k], rmq[r - MASK(k) + 1][k]);
}
void solve() {
prepare();
for(int i = n; i >= 1; --i) {
int l = i + 1, r = n, id = n + 1;
while(l <= r) {
int m = (l + r) >> 1;
if(get(i, m) > a[i]) {
id = m;
r = m - 1;
}
else l = m + 1;
}
f[i] = f[id] + 1;
maximize(res, f[i]);
}
cout << res;
}
}
namespace sub5 {
int x[N];
void solve() {
for(int i = 1; i <= n; ++i) x[i] = inf;
x[0] = 0;
for(int i = 1; i <= n; ++i) {
int l = 1, r = n, id = 0;
while(l <= r) {
int m = (l + r) >> 1;
if(x[m] >= a[i]) {
id = m;
r = m - 1;
}
else l = m + 1;
}
x[id] = a[i];
maximize(res, id);
}
cout << res;
}
}
namespace sub3 {
void prepare() {
for(int i = 2; i <= n; ++i) lg[i] = lg[i / 2] + 1;
for(int i = 1; i <= n; ++i) rmq[i][0] = a[i];
for(int j = 1; j <= LOG; ++j)
for(int i = 1; i + MASK(j) - 1 <= n; ++i) rmq[i][j] = min(rmq[i][j - 1], rmq[i + MASK(j - 1)][j - 1]);
}
int get(int l, int r) {
int k = lg[r - l + 1];
return min(rmq[l][k], rmq[r - MASK(k) + 1][k]);
}
void solve() {
prepare();
for(int i = 1; i <= n; ++i) {
f[i] = 1;
if(a[i] > a[i - 1]) f[i] = f[i - 1] + 1;
for(int j = 1; j < i - 1; ++j) {
if(a[j] >= a[i]) continue;
int l = max(1, i - d), r = min(i, j + d);
if(l <= r && get(l, r) <= a[j]) maximize(f[i], f[j] + 1);
}
}
cout << f[n];
}
}
void solve()
{
if(d == 1) return void(sub4::solve());
if(d == n) return void(sub5::solve());
if(n <= 7000) return void(sub3::solve());
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
// freopen(file".inp" , "r" , stdin);
// freopen(file".out" , "w" , stdout);
inp();
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4644 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4644 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4644 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
33316 KB |
Output is correct |
2 |
Correct |
77 ms |
33112 KB |
Output is correct |
3 |
Correct |
74 ms |
33360 KB |
Output is correct |
4 |
Correct |
69 ms |
33100 KB |
Output is correct |
5 |
Correct |
68 ms |
33116 KB |
Output is correct |
6 |
Correct |
71 ms |
33368 KB |
Output is correct |
7 |
Correct |
66 ms |
33140 KB |
Output is correct |
8 |
Correct |
69 ms |
33152 KB |
Output is correct |
9 |
Correct |
68 ms |
33292 KB |
Output is correct |
10 |
Correct |
72 ms |
33112 KB |
Output is correct |
11 |
Correct |
70 ms |
33104 KB |
Output is correct |
12 |
Correct |
69 ms |
33108 KB |
Output is correct |
13 |
Correct |
65 ms |
33108 KB |
Output is correct |
14 |
Correct |
67 ms |
33320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
5720 KB |
Output is correct |
2 |
Correct |
38 ms |
5720 KB |
Output is correct |
3 |
Correct |
42 ms |
5716 KB |
Output is correct |
4 |
Correct |
41 ms |
5720 KB |
Output is correct |
5 |
Correct |
45 ms |
6228 KB |
Output is correct |
6 |
Correct |
41 ms |
5724 KB |
Output is correct |
7 |
Correct |
37 ms |
5724 KB |
Output is correct |
8 |
Correct |
26 ms |
5720 KB |
Output is correct |
9 |
Correct |
35 ms |
5588 KB |
Output is correct |
10 |
Correct |
38 ms |
5712 KB |
Output is correct |
11 |
Correct |
39 ms |
5716 KB |
Output is correct |
12 |
Correct |
27 ms |
5724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4644 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |