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 sz(x) (int)x.size()
#define chmin(x, v) x = min(x, v)
#define chmax(x, v) x = max(x, v)
#define all(x) x.begin(), x.end()
#define pb push_back
#define pii pair<int, int>
#define deb first
#define fin second
#define int long long
using namespace std;
const int N = (1 << 19);
deque<int> enAttente[N];
int arbre[N * 2];
int getMax(int gauche, int droite){
if (droite < gauche) return 0;
if (gauche%2 == 1) return max(arbre[gauche], getMax(gauche + 1, droite));
if (droite%2 == 0) return max(arbre[droite], getMax(gauche, droite - 1));
return getMax(gauche/2, droite/2);
}
int temps = 0;
int dp[N];
void update(int val, int newVal){
int noeud = N + val;
arbre[noeud] = newVal;
// cout << "upd " << val << " " << arbre[noeud] << endl;
noeud /= 2;
for (; noeud > 0; noeud /= 2)
arbre[noeud] = max(arbre[noeud * 2], arbre[noeud * 2 + 1]);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int nVals, distMax; cin >> nVals >> distMax;
vector<int> vals(nVals), triees;
for (int& val : vals){
cin >> val;
triees.pb(val);
}
sort(all(triees));
map<int, int> conv;
for (int i = 0; i < nVals; ++i)
conv[triees[i]] = i;
for (int& val : vals)
val = conv[val];
int maxi = 0;
for (int iVal = 0; iVal < nVals; ++iVal){
if (iVal > distMax && !enAttente[vals[iVal - distMax -1]].empty() && enAttente[vals[iVal - distMax - 1]].back() == iVal - distMax - 1){
//cout << "degage" << iVal << endl;
int befId = iVal - distMax - 1, befVal = vals[befId];
enAttente[befVal].pop_back();
if (!enAttente[befVal].empty())
update(befVal, dp[enAttente[befVal].back()]);
else
update(befVal, -1);
}
int res = getMax(N, N + vals[iVal] - 1) + 1;
dp[iVal] = res;
while (!enAttente[vals[iVal]].empty() && dp[enAttente[vals[iVal]].front()] <= res)
enAttente[vals[iVal]].pop_front();
enAttente[vals[iVal]].push_front(iVal);
update(vals[iVal], dp[enAttente[vals[iVal]].back()]);
chmax(maxi, dp[iVal]);
// cout << "res " << iVal << ": "<< dp[iVal] << endl;
}
// cout << endl;
cout << maxi << endl;
return 0;
}
# | 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... |