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;
#define mp make_pair
#define INF 10000000
#define MOD 1000000007
#define MID ((l+r)/2)
#define HASHMOD 2305843009213693951
#define ll long long
#define ull unsigned long long
#define F first
#define S second
typedef pair<ll, ll> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef map<int, int> mii;
#define EPS 1e-6
#define FOR(i,n) for(int i=0;i<((int)(n));i++)
#define FORi(i,a,b) for(int i=((int)(a));i<((int)(b));i++)
#define FOA(v, a) for(auto v : a)
vi a, b;
int n, k;
map<int, int> m;
int dp[9000];
int pos[9000];
int rec(int x){
int ans=1;
if(dp[x]!=-1) return dp[x];
FORi(i,x+1, min(n, pos[x]+1)){
if(a[i]>a[x]) ans = max(ans, 1 + rec(i));
}
//assert(ans>=dp[x]);
dp[x] = max(dp[x], ans);
return ans;
}
struct node{
node *L, *R;
int val;
void build(int l=0, int r=n-1){
if(l==r) val = 0;
else{
L = new node();
R = new node();
L->build(l,MID);
R->build(MID+1, r);
val = 0;
}
}
void update(int pos, int v, int l=0, int r=n-1){
if(l==r && l==pos) val = v;
else if (l==r) return;
else{
L->update(pos,v,l,MID);
R->update(pos,v,MID+1,r);
val = max(L->val, R->val);
}
}
int query(int rl, int rr, int l=0, int r=n-1){
if(rl<=l && r<=rr) return val;
else if(l>rr || rl>r) return 0;
else return max(L->query(rl,rr,l,MID), R->query(rl,rr,MID+1,r));
}
};
int main(){
node seg;
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>k;
seg.build();
//FOR(i,n) FOR(j,n) dp[i][j] = -1;
FOR(i,n+1) dp[i] = -1;
a.resize(n);
FOR(i,n) cin>>a[i];
int cnt=0;
set<int> all;
FOR(i,n) all.insert(a[i]);
FOA(v, all) m[v] = ++cnt;
FOR(i,n) a[i] = m[a[i]];
//FOR(i,n) cout<<a[i]<<" ";
//cout<<endl;
FOR(i,n){
int cnt=0;
pos[i] = n-1;
FORi(j,i+1,n){
if(a[j]>a[i]) cnt++;
else cnt=0;
if(cnt<=k && a[j]>a[i]) pos[i] = j;
if(cnt>=k) break;
}
}
vii b(n);
FOR(i,n) b[i] = {-a[i], i};
sort(b.begin(), b.end());
FOA(v,b){
int ans = seg.query(v.S+1, pos[v.S]);
//cout<<v.S<<" "<<ans<<endl;
seg.update(v.S, 1+seg.query(v.S+1, pos[v.S]));
}
cout<<seg.query(0, n-1)<<endl;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:109:7: warning: unused variable 'ans' [-Wunused-variable]
109 | int ans = seg.query(v.S+1, pos[v.S]);
| ^~~
# | 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... |