이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
typedef pair<int,int> pi;
typedef vector <int> vi;
typedef vector <pi> vpi;
typedef pair<pi, ll> pii;
typedef set <ll> si;
typedef long double ld;
#define f first
#define s second
#define mp make_pair
#define FOR(i,s,e) for(int i=s;i<=int(e);++i)
#define DEC(i,s,e) for(int i=s;i>=int(e);--i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i,x) for (auto i: x)
#define mem(x,i) memset(x,i,sizeof x)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define maxn 300010
#define INF (ll)1e18
typedef pair <vi, int> pvi;
typedef pair <int,pi> ipi;
typedef vector <pii> vpii;
#define DEBUG 0
#define MOD 1000003233
int TC;
int N,M,D;
int A[maxn],nxt[maxn],sz[maxn];
vi B[maxn];
bool inc[maxn];
int p[maxn],dp[maxn];
int fl(int x){
if (p[x] == x) return x;
return p[x] = fl(p[x]);
}
void join(int a,int b){
a = fl(a); b = fl(b);
if (a == b) return;
p[a] = b;
sz[b] += sz[a];
}
struct node{
int s,e,m,val=0;
node *l,*r;
node(int ss,int ee){
s = ss; e = ee; m = (s + e) / 2;
if (s != e){
l = new node(s,m); r = new node(m+1,e);
}
}
int qry(int a,int b){
if (a <= s && e <= b) return val;
else if (a > e || s > b) return 0;
return max(l->qry(a,b),r->qry(a,b));
}
void upd(int p,int v){
val = max(val, v);
if (s == e) return;
if (p > m) r->upd(p,v);
else l->upd(p,v);
}
}*root;
void solve(){
cin >> N >> D;
vi tmp;
FOR(i,1,N){
cin >> A[i]; tmp.pb(A[i]);
}
sort(all(tmp));
FOR(i,1,N) A[i] = lower_bound(all(tmp), A[i]) - tmp.begin() + 1;
FOR(i,1,N) B[A[i]].pb(i);
FOR(i,1,N) p[i] = i,sz[i] = 1;
set <int> st;
DEC(i,N,1){
aFOR(j, B[i]){
auto it = st.upper_bound(j);
if (it == st.end()) nxt[j] = N;
else nxt[j] = *it - 1;
}
aFOR(j, B[i]){
inc[j] = 1;
if (inc[j-1]) join(j,j-1);
if (inc[j+1]) join(j+1,j);
int x = fl(j); //assert(fl(x-1) != x);
if (sz[x] >= D) st.insert(x);
}
}
root = new node(1,N);
dp[N] = 0;
/*
DEC(i,N-1,1){
FOR(j,i+1,nxt[i]+D) if (A[j] > A[i]) dp[i] = max(dp[i], dp[j] + 1);
}
*/
DEC(i,N,1){
aFOR(j, B[i]){
dp[j] = root->qry(j+1,nxt[j]+D) + 1;
}
aFOR(j, B[i]) root->upd(j, dp[j]);
}
//FOR(i,1,N) cout << dp[i] << ' ';
//cout << '\n';
cout << *max_element(dp+1,dp+N+1);
}
int32_t main(){
fast;
solve();
//cin >> TC;
//while (TC--) solve();
}
# | 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... |