Editorial for Бовінополіс


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
#include <bits/stdc++.h>
using namespace std;

int DP[300001];
int pref[300001];
int main() {
    int N, K; cin >> N >> K;
    string s; cin >> s;
    DP[0] = 0;
    pref[0] = 0;
    for (int i = 0; i < (int) s.size(); i++){
      if (s[i] == 'H'){
        pref[i + 1] = pref[i] + 1;
      }
      else{
        pref[i + 1] = pref[i] - 1;
      }
    }
    //contains all values of dp[i] that are active
    multiset<int> dpvals;
    dpvals.insert(0);
    //contains the values of pre[i] given dp[i]
    multiset<int> elements[300001]; 
    elements[0].insert(0);
    for (int i = 1; i <= N; i++){
      //query
      int mnval = *(dpvals.begin());
      if (*elements[mnval].begin() < pref[i]){
        DP[i] = mnval;
      }
      else{
        DP[i] = mnval + 1;
      }
      dpvals.insert(DP[i]);
      elements[DP[i]].insert(pref[i]);
      //update
      if (i >= K){
        dpvals.erase(dpvals.find(DP[i - K]));
        elements[DP[i - K]].erase(elements[DP[i - K]].find(pref[i - K]));
      }
    }
    cout << DP[N] << endl;
}

Коментарі

Please read the guidelines before commenting.


Ще немає коментарів.