제출 #463712

#제출 시각아이디문제언어결과실행 시간메모리
463712MounirFinancial Report (JOI21_financial)C++14
5 / 100
1148 ms538936 KiB
#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];

      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()]);
          //  cout << "res " << iVal << ": "<< dp[iVal] << endl;
      }
 //     cout << endl;
      
      cout << arbre[1] << endl;
      return 0;
 
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...