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 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 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 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 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){
down(id, l, r);
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){
assert(r <= n);
update(1, 1, n, l, r, x);
}
int get(int l, int r){
assert(r <= n);
return get(1, 1, n, 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, n);
mem(R, -0x3f);
for (int i = n; i >= 1; i--){
if (i < n - D) R[i] = ST.get(a[i], a[i]);
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();
ST.update(1, a[dq.front()] - 1, max(-(i + D - 1), -n));
}
}
ST.build(1, 1, n);
for (int i = 1; i <= n; i++)
maxi(R[i], -n);
for (int i = 1; i <= n; i++)
b[i] = {a[i], i};
// for (int i = 1; i <= n; i++)
// cout << i << ' ' << -R[i] << '\n';
sort(b + 1, b + n + 1, cmp);
for (int i = 1; i <= n; i++){
int id = b[i].se;
int tmp = max(ST.get(id + 1, -R[id]), 0) + 1;
ST.update(id, id, tmp);
}
cout << ST.get(1, n);
}
}
int main(){
load();
input();
compress();
subtask6::solve();
}
Compilation message (stderr)
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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |