#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pill pair<int,ll>
#define mem(a, b) memset(a, b, sizeof(a))
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define name "test"
using namespace std;
const int N = 3e5 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18 + 69;
template <typename T1, typename T2> bool maxi(T1 &a, T2 b){if (a < b){a = b; return 1;} return 0;}
template <typename T1, typename T2> bool mini(T1 &a, T2 b){if (a > b){a = b; return 1;} return 0;}
int n;
int k;
int D;
int a[N];
int f[(int)7e3 + 2][(int)7e3 + 2];
deque <int> dq[(int)7e3 + 2];
vector <int> value;
void load(){
cin.tie(0)->sync_with_stdio(0);
if (fopen(name".inp", "r")){
freopen(name".inp", "r", stdin);
freopen(name".out", "w", stdout);
}
}
void input(){
cin >> n >> D;
for (int i = 1; i <= n; i++){
cin >> a[i];
}
}
void compress(){
for (int i = 1; i <= n; i++)
value.push_back(a[i]);
sort(value.begin(), value.end());
value.erase(unique(value.begin(), value.end()), value.end());
for (int i = 1; i <= n; i++)
a[i] = lower_bound(value.begin(), value.end(), a[i]) - value.begin() + 1;
k = (int)value.size();
}
namespace subtask3{
void solve(){
mem(f, -0x3f);
for (int i = 1; i <= n; i++){
f[i][a[i]] = 1;
for (int j = 1; j <= k; j++){
if (dq[j].size() && dq[j].front() < i - D) dq[j].pop_front();
if (dq[j].size()) maxi(f[i][max(a[i], j)], f[dq[j].front()][j] + (j < a[i]));
}
for (int j = 1; j <= k; j++){
while (dq[j].size() && f[dq[j].back()][j] <= f[i][j])
dq[j].pop_back();
dq[j].push_back(i);
}
}
int ans = 0;
for (int i = 1; i <= k; i++)
maxi(ans, f[n][i]);
cout << ans;
}
}
namespace subtask4{
stack <int> st;
void solve(){
st.push(n);
int ans = 1;
for (int i = n - 1; i >= 1; i--){
while (st.size() && a[st.top()] <= a[i]) st.pop();
st.push(i);
maxi(ans, st.size());
}
cout << ans;
}
}
namespace subtask5{
int b[N];
void solve(){
mem(b, 0x3f);
int ans = 0;
for (int i = 1; i <= n; i++){
int id = lower_bound(b + 1, b + n + 1, a[i]) - b;
b[id] = a[i];
maxi(ans, id);
}
cout << ans;
}
}
namespace subtask6{
int R[N];
pii b[N];
deque<int> dq;
struct SEGTREE{
int lz[N << 2];
int val[N << 2];
SEGTREE(){};
void down(int id, int l, int r){
maxi(val[id], lz[id]);
if (l < r){
maxi(lz[id << 1], lz[id]);
maxi(lz[id << 1 | 1], lz[id]);
}
lz[id] = -mod;
}
void build(int id, int l, int r){
if (l == r) return void(val[id] = lz[id] = -mod);
int m = (l + r) >> 1;
build(id << 1, l, m);
build(id << 1 | 1, m + 1, r);
val[id] = lz[id] = -mod;
}
void update(int id, int l, int r, int u, int v, int x){
down(id, l, r);
if (l > v || r < u) return;
if (l >= u && r <= v){
maxi(lz[id], x);
down(id, l, r);
return;
}
int m = (l + r) >> 1;
update(id << 1, l, m, u, v, x);
update(id << 1 | 1, m + 1, r, u, v, x);
val[id] = max(val[id << 1], val[id << 1 | 1]);
}
int get(int id, int l, int r, int u, int v){
if (l > v || r < u) return -mod;
if (l >= u && r <= v) return val[id];
int m = (l + r) >> 1;
return max(get(id << 1, l, m, u, v), get(id << 1 | 1, m + 1, r, u, v));
}
void update(int l, int r, int x){
update(1, 1, k, l, r, x);
}
int get(int l, int r){
return get(1, 1, k, l, r);
}
} ST;
bool cmp(pii x, pii y){
if (x.fi == y.fi) return x.se < y.se;
return x.fi > y.fi;
}
void solve(){
ST.build(1, 1, k);
mem(R, -0x3f);
for (int i = n; i >= 1; i--){
if (i < n - D) R[i] = ST.get(a[i], k);
while (dq.size() && a[dq.back()] <= a[i]) dq.pop_back();
dq.push_back(i);
if (i <= n - D){
if (dq.front() > i + D) dq.pop_front();
if (a[dq.front()] + 1 <= k) ST.update(a[dq.front()] + 1, k, -i);
}
}
ST.build(1, 1, k);
for (int i = 1; i <= n; i++)
maxi(R[i], -n);
for (int i = 1; i <= n; i++)
b[i] = {a[i], i};
sort(b + 1, b + n + 1, cmp);
for (int i = 1; i <= n; i++){
int tmp = max(ST.get(b[i].se + 1, -R[i]), 0) + 1;
ST.update(b[i].se, b[i].se, tmp);
}
cout << ST.get(1, k);
}
}
int main(){
load();
input();
compress();
// if (n <= (int)7e3){
// subtask3::solve();
// return 0;
// }
// if (D == 1){
// subtask4::solve();
// return 0;
// }
subtask6::solve();
}
Compilation message
Main.cpp: In instantiation of 'bool maxi(T1&, T2) [with T1 = int; T2 = long unsigned int]':
Main.cpp:85:32: required from here
Main.cpp:19:66: warning: comparison of integer expressions of different signedness: 'int' and 'long unsigned int' [-Wsign-compare]
19 | template <typename T1, typename T2> bool maxi(T1 &a, T2 b){if (a < b){a = b; return 1;} return 0;}
| ~~^~~
Main.cpp: In function 'void load()':
Main.cpp:33:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen(name".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:34:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | freopen(name".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16732 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
4 ms |
16732 KB |
Output is correct |
4 |
Correct |
3 ms |
16732 KB |
Output is correct |
5 |
Correct |
4 ms |
16732 KB |
Output is correct |
6 |
Correct |
4 ms |
16732 KB |
Output is correct |
7 |
Correct |
4 ms |
16732 KB |
Output is correct |
8 |
Correct |
3 ms |
16732 KB |
Output is correct |
9 |
Correct |
4 ms |
16732 KB |
Output is correct |
10 |
Correct |
4 ms |
16732 KB |
Output is correct |
11 |
Correct |
3 ms |
16732 KB |
Output is correct |
12 |
Incorrect |
4 ms |
16732 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16732 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
4 ms |
16732 KB |
Output is correct |
4 |
Correct |
3 ms |
16732 KB |
Output is correct |
5 |
Correct |
4 ms |
16732 KB |
Output is correct |
6 |
Correct |
4 ms |
16732 KB |
Output is correct |
7 |
Correct |
4 ms |
16732 KB |
Output is correct |
8 |
Correct |
3 ms |
16732 KB |
Output is correct |
9 |
Correct |
4 ms |
16732 KB |
Output is correct |
10 |
Correct |
4 ms |
16732 KB |
Output is correct |
11 |
Correct |
3 ms |
16732 KB |
Output is correct |
12 |
Incorrect |
4 ms |
16732 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16732 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
4 ms |
16732 KB |
Output is correct |
4 |
Correct |
3 ms |
16732 KB |
Output is correct |
5 |
Correct |
4 ms |
16732 KB |
Output is correct |
6 |
Correct |
4 ms |
16732 KB |
Output is correct |
7 |
Correct |
4 ms |
16732 KB |
Output is correct |
8 |
Correct |
3 ms |
16732 KB |
Output is correct |
9 |
Correct |
4 ms |
16732 KB |
Output is correct |
10 |
Correct |
4 ms |
16732 KB |
Output is correct |
11 |
Correct |
3 ms |
16732 KB |
Output is correct |
12 |
Incorrect |
4 ms |
16732 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
18380 KB |
Output is correct |
2 |
Incorrect |
89 ms |
18284 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
18624 KB |
Output is correct |
2 |
Incorrect |
85 ms |
18324 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16732 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
4 ms |
16732 KB |
Output is correct |
4 |
Correct |
3 ms |
16732 KB |
Output is correct |
5 |
Correct |
4 ms |
16732 KB |
Output is correct |
6 |
Correct |
4 ms |
16732 KB |
Output is correct |
7 |
Correct |
4 ms |
16732 KB |
Output is correct |
8 |
Correct |
3 ms |
16732 KB |
Output is correct |
9 |
Correct |
4 ms |
16732 KB |
Output is correct |
10 |
Correct |
4 ms |
16732 KB |
Output is correct |
11 |
Correct |
3 ms |
16732 KB |
Output is correct |
12 |
Incorrect |
4 ms |
16732 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |