Problem:
Rank Promotion
Quiz Solver is a popular online computer game. Each time a player opens the mobile application
of the game, a new quiz is displayed. The player submits an answer to the quiz, and then it is
judged either as correct or incorrect, which is accumulated in the database. When the player
shows high accuracy for a number of quizzes, the
rank
of the player in the game is promoted.
Player ranks are non-negative integers, and each player starts the game at the rank 0. The player
will be promoted to the next rank when the player achieves a high ratio of correct answers
during a sufficiently long sequence of quizzes. More precisely, the rank promotion system is
defined by two parameters: an integer $c$, and a rational number $p/q$. After finishing the $e$-th
quiz, the player’s rank is immediately incremented by one if there exists an integer$ $s satisfying
the following conditions.
$1 \leq s \leq e − c + $1.
The player was already at the current rank before starting the $s$-th quiz.
The ratio of correct answers of the quizzes from the $s$-th through the $e$-th is higher than or equal to $p/q$.
Otherwise, the rank stays the same.
One day, the administrator of Quiz Solver realized that the rank data of the players were lost
due to a database failure. Luckily, the log of quiz solving records was completely secured without
any damages. Your task is to recompute the rank of each player from the solving records for
the player.
Input
The input consists of a single test case in the following format.
$n$ $c$ $p$ $q$
$S_1$ · · · $S_n$
The first line consists of four integers satisfying the following constraints: $1 \leq n \leq 5 \times 10^5$, $1 \leq c \leq 200$, and $1 \leq p \leq q \leq 5 \times 10^5$. The first integer $n$ is the number of quizzes answered by a single player. The meanings of the parameters $c$, $p$, and $q$ are described in the problem
statement.
$S_1$ · · · $S_n$ is a string describing the quiz solving records of the player. Each $S_i$ is either
Y
meaning that the player’s answer for the $i$-th quiz was correct,  or
N
meaning incorrect.
Output
Output the final rank of the player after finishing the $n$-th quiz in one line.
Sample Input 1
12 4 4 7
YYYNYYNNNYYN
Sample Output 1
2
Sample Input 2
10 1 1 1
YNYNYNYNYN
Sample Output 2
5
Sample Input 3
17 5 250000 500000
YYYYYYYYYYYYYYYYY
Sample Output 3
3
Sample Input 4
8 3 2 3
YNNYYYYN
Sample Output 4
2
In Sample Input 1, the player is promoted to the rank 1 after finishing the fourth quiz, because
the ratio of the correct answers $3/4$ is higher than $p/q = 4/7$. Note that, the promotion didn’t
happen at the third quiz because only three quizzes had been answered, which is less than $c = 4$.
Then, after the eleventh quiz, the player is promoted to the rank 2.
Figure B.1. The timings of rank promotions of Sample Input 1

Submitted solutions:

code_snippet: #include  <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for(ll i=0;i<n;i++)

int main() {
    ll n, c, p, q;
    cin >> n >> c >> p >> q;
    string S;
    cin >> S;

    ll Y = 0, N = 0, rank = 0, reset_index = 0;
    rep(e, n) {
        if (S[e] == 'Y') Y++;
        else N++;
        if (Y + N > c) {
            if (S[e - c] == 'Y') Y--;
            else N--;
        }
        if (Y + N != c) continue;

        int ry = 0, rn = 0;
        for (int s = e - c; s >= max(reset_index - 1, e - 2 * c - 1); s--) {
            if ((Y + ry) * q >= p * ((Y + ry) + (N + rn))) {
                rank++;
                Y = N = 0;
                reset_index = e + 1;
                break;
            }
            if (s >= 0) {
                if (S[s] == 'Y') ry++;
                else rn++;
            }
        }
    }
    cout << rank << endl;
}
accuracy: 1
time_ms: 170
memory_kb: 4144
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
using ll = long long;
using vec = vector<ll>;
using Graph = vector<vec>;
using Pair = pair<ll,ll>;
using ld = long double;

void debug1(vec v){for(auto x:v)cout << x << ' ';cout << endl;}
void debug2(vector<Pair> v){for(auto x:v)cout << '(' << x.first << ',' << x.second << ')' << endl;}
void debug3(Graph v){rep(i,0,v.size()-1)debug1(v[i]);cout << endl;}
void yes(bool ans){if(ans)cout << "Yes" << endl;else cout << "No" << endl;}
ll mod = 998244353;
ll inf = 1000000000000000000;
int main(){
    srand( (unsigned)time( NULL ) );
    ll n,c,p,q;cin >> n >> c >> p >> q;
    vector<char> s(n+1);
    rep(i,1,n)cin >> s[i];
    vec a(n+1),b(n+1);
    rep(i,1,n)a[i] = (s[i] == 'Y') ? 1 : 0;
    vec sum(n+1);
    rep(i,1,n)sum[i] = sum[i-1] + a[i];
    vec d(n+1);
    rep(i,1,n)d[i] = q*sum[i] - p*((ll)i);
    //debug1(d);
    ll ans = 0;
    ll pre = 0;
    while(1){
        ll Min = inf;
        if(pre+c>n)break;
        bool change = false;
        rep(i,pre+1,n-c+1){
            Min = min(Min,d[i-1]);
            if(Min <= d[i+c-1]){
                pre = i+c-1;
                ans++;
                change = true;
                break;
            }
        }
        //cout << pre << endl;
        if(!change)break;
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 19272
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using a2 = array<ll, 2>;
using a3 = array<ll, 3>;

bool chmin(auto& a, const auto& b) { return a > b ? a = b, 1 : 0; }
bool chmax(auto& a, const auto& b) { return a < b ? a = b, 1 : 0; }

ll mod = 998244353;

const ll INF = 1e18;

ifstream in;
ofstream out;

int main(int argc, char** argv) {
    ios::sync_with_stdio(false);
    cin.tie(0);

    if (argc > 2) {
        in.open(argv[1]);
        cin.rdbuf(in.rdbuf());
        out.open(argv[2]);
        cout.rdbuf(out.rdbuf());
    }

    ll n, c, p, q;
    string s;
    cin >> n >> c >> p >> q >> s;

    vector<a2> yesno(n + 1, {0, 0});
    for (int i = 0; i < n; i++) {
        yesno[i + 1][0] = yesno[i][0] + (s[i] == 'Y');
        yesno[i + 1][1] = yesno[i][1] + (s[i] == 'N');
    }

    vector<ll> val(n, 0);
    for (int i = 1; i < n; i++) {
        val[i] = val[i - 1] + p - q * (s[i - 1] == 'Y');
    }

    ll ans = 0;
    set<a2> sh;
    for (int i = c - 1; i < n; i++) {
        sh.insert({val[i - (c - 1)], i - (c - 1)});
        auto itr = *sh.rbegin();
        ll yes = yesno[i + 1][0] - yesno[itr[1]][0];
        ll no = yesno[i + 1][1] - yesno[itr[1]][1];

        if (yes * q >= (yes + no) * p) {
            ans++;
            sh.clear();
            i += c - 1;
        }
    }
    cout << ans << endl;

    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 46876
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;

bool chmin(auto &a, auto b) { return a > b ? a = b, true : false; }
bool chmax(auto &a, auto b) { return a < b ? a = b, true : false; }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N, C;
    ll p, q;
    cin >> N >> C >> p >> q;
    string S;
    cin >> S;
    int pre = 0, ans = 0;
    for (int i = 0; i < N; i++) {
        ll y = 0, n = 0;
        for (int j = i; j >= pre && j >= i - 2 * C; j--) {
            (S[j] == 'Y' ? y : n)++;
            if (j <= i - C + 1 && q * y >= p * (y + n)) {
                pre = i + 1;
                ans++;
                break;
            }
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 230
memory_kb: 3968
final_rank: 8

code_snippet: #include <bits/stdc++.h>

#define pb push_back
#define x first
#define y second
#define endl '\n'

using namespace std;

typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> PII;
typedef double db;

const LL N = 5e5 + 10;
const LL M = N << 1;
LL n, c, p, q;
string s;
LL ans;
LL suc = 0;
LL sum = 0;

void solve() {
	cin >> n >> c >> p >> q;
	cin >> s;
	int i = 0;
	int pre = 0;
	while (i < s.length()) {
		suc = 0;
		sum = 0;
		for (int j = i; j >= pre; j -- ) {
			if (i - j + 1 >= 3 * c) break;
			sum += 1;
			suc += (s[j] == 'Y');
			if (i - j + 1 >= c) {
//				cout << sum << ' ' << suc << " ";
//				cout << sum * p << ' ' << suc * q << endl;
				if (sum && sum * p <= suc * q) {
//					cout << i << ' ' << j << endl;
//					cout << sum << ' ' << suc << ' ' << endl;
					ans ++ ;
					pre = i + 1;
					break;
				}
			}
		}
		i ++ ;
	}
	cout << ans << endl;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int T = 1;
//	cin >> T;
	while (T -- ) {
		solve();
	}
	return 0;
}
accuracy: 1
time_ms: 300
memory_kb: 3724
final_rank: 14

code_snippet: // AOJ 1445 - Rank Promotion
// ICPC Asia Japan Regional Contest 2023 - Problem B

// upsolve - O(nc) solution

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, c, p, q; cin >> n >> c >> p >> q;
    string S; cin >> S;

    vector<pair<int,int>> promote;
    for(int i=0; i<n; ++i) {
        int correct = 0;
        for(int j=0; j<min(2*c,n-i); ++j) {
            if(S[i+j] == 'Y') ++correct;
            if(j+1 >= c && correct * q >= p * (j+1)) {
                promote.emplace_back(i+j, i);
                break;
            }
        }
    }
    sort(promote.begin(), promote.end());

    int rank = 0, last = -1;
    for(auto i : promote) {
        if(i.second > last) {
            ++rank;
            last = i.first;
        }
    }
    cout << rank << endl;
}
accuracy: 1
time_ms: 220
memory_kb: 10432
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define loop(i, a, b) for (ll i = a; i <= (ll)b; ++i)

int dx[] = {1, 0, 0, -1};
int dy[] = {0, 1, -1, 0};
int main(){
    ll n, c, p, q;
    cin >> n >> c >> p >> q;
    string s;
    cin >> s;

    ll ans = 0;
    ll last = 0;
    vector<pair<ll, ll>> sum(n + 1, {0, 0});
    loop(i, 1, n)
    {
        sum[i] = {sum[i - 1].first + (s[i - 1] == 'Y' ? 1 : 0), sum[i - 1].second + 1};
        if (i - c < 0) continue;

        loop(j, max(last, i - 2 * c), i - c) {
            ll a = (sum[i].first - sum[j].first) * q;
            ll b = p * (sum[i].second - sum[j].second);
            if (b <= a) {
                ans++;
                last = i;
                break;
            }
        }
    }

    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 11540
final_rank: 7

code_snippet: // #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

// #include <atcoder/all>
// using namespace atcoder;

#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;

const int dy[] = {-1, 0, 0, 1};
const int dx[] = {0, -1, 1, 0};

template <typename T, typename T1, typename T2>
bool isrange(T target, T1 low, T2 high) {
    return low <= target && target < high;
}

// #include "titan_cpplib/others/print.cpp"

#include <iostream>
#include <vector>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
using namespace std;

// print

// -------------------------
// pair<K, V>
template<typename K, typename V> ostream& operator<<(ostream& os, const pair<K, V>& p);
// tuple<T1, T2, T3>
template<typename T1, typename T2, typename T3> ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &t);
// tuple<T1, T2, T3, T4>
template<typename T1, typename T2, typename T3, typename T4> ostream &operator<<(ostream &os, const tuple<T1, T2, T3, T4> &t);
// vector<T>
template<typename T> ostream& operator<<(ostream& os, const vector<T>& a);
// vector<vector<T>>
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& a);
// set<T>
template<typename T> ostream& operator<<(ostream& os, const set<T>& s);
// multiset<T>
template<typename T> ostream& operator<<(ostream& os, const multiset<T>& s);
// unordered_set<T>
template<typename T> ostream& operator<<(ostream& os, const unordered_set<T>& a);
// map<K, V>
template<typename K, typename V> ostream& operator<<(ostream& os, const map<K, V>& mp);
// unordered_map<K, V>
template<typename K, typename V> ostream& operator<<(ostream& os, const unordered_map<K, V>& mp);
// -------------------------

// color
static const string PRINT_RED = "\033[91m"; // 赤字
static const string PRINT_GREEN = "\033[92m"; // 緑字
static const string PRINT_BLUE = "\033[94m";  // 青字
static const string PRINT_NONE = "\033[m"; // 色を元に戻す
static const string PRINT_BOLD = "\u001b[1m"; // 太字

string to_red(const string &s) {
    return PRINT_RED + s + PRINT_NONE;
}

string to_green(const string &s) {
    return PRINT_GREEN + s + PRINT_NONE;
}

string to_blue(const string &s) {
    return PRINT_BLUE + s + PRINT_NONE;
}

string to_bold(const string &s) {
    return PRINT_BOLD + s + PRINT_NONE;
}

string spacefill(const string s, const int f) {
    int n = s.size();
    string t;
    for (int i = 0; i < f-n; ++i) t += " ";
    t += s;
    return t;
}

string zfill(const string s, const int f) {
    int n = s.size();
    string t;
    for (int i = 0; i < f-n; ++i) t += "0";
    t += s;
    return t;
}

string bin(long long s) {
    string t;
    while (s) {
        if (s & 1) {
            t += '1';
        } else {
            t += '0';
        }
        s >>= 1;
    }
    reverse(t.begin(), t.end());
    return t;
}

// pair<K, V>
template<typename K, typename V>
ostream& operator<<(ostream& os, const pair<K, V>& p) {
    os << "(" << p.first << ", " << p.second << ")";
    return os;
}

// tuple<T1, T2, T3>
template<typename T1, typename T2, typename T3>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &t) {
    os << "(" << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ")";
    return os;
}

// tuple<T1, T2, T3, T4>
template<typename T1, typename T2, typename T3, typename T4>
ostream &operator<<(ostream &os, const tuple<T1, T2, T3, T4> &t) {
    os << "(" << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ", " << get<3>(t) << ")";
    return os;
}

// vector<T>
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& a) {
    int n = (int)a.size();
    os << "[";
    for (int i = 0; i < n-1; ++i) {
        os << a[i] << ", ";
    }
    if (n > 0) {
        os << a.back();
    }
    os << "]";
    return os;
}

// vector<vector<T>>
template<typename T>
ostream& operator<<(ostream& os, const vector<vector<T>>& a) {
    os << "[\n";
    int h = (int)a.size();
    for (int i = 0; i < h; ++i) {
        os << "  " << a[i] << '\n';
    }
    os << "]";
    return os;
}

// set<T>
template<typename T>
ostream& operator<<(ostream& os, const set<T>& s) {
    os << "{";
    auto it = s.begin();
    while (it != s.end()) {
        os << *it;
        ++it;
        if (it != s.end()) {
            os << ", ";
        }
    }
    os << "}";
    return os;
}

// multiset<T>
template<typename T>
ostream& operator<<(ostream& os, const multiset<T>& s) {
    os << "{";
    auto it = s.begin();
    while (it != s.end()) {
        os << *it;
        ++it;
        if (it != s.end()) {
            os << ", ";
        }
    }
    os << "}";
    return os;
}

// unordered_set<T>
template<typename T>
ostream& operator<<(ostream& os, const unordered_set<T>& a) {
    set<T> s;
    for (const T &x : a) {
        s.insert(x);
    }
    os << s;
    return os;
}

// map<K, V>
template<typename K, typename V>
ostream& operator<<(ostream& os, const map<K, V>& mp) {
    os << "{";
    auto it = mp.begin();
    while (it != mp.end()) {
        os << it->first << ": " << it->second;
        ++it;
        if (it != mp.end()) {
            os << ", ";
        }
    }
    os << "}";
    return os;
}

// unordered_map<K, V>
template<typename K, typename V>
ostream& operator<<(ostream& os, const unordered_map<K, V>& mp) {
    map<K, V> m;
    for (const auto &[k, v] : mp) {
        m[k] = v;
    }
    os << m;
    return os;
}

// #include "titan_cpplib/data_structures/lazy_segment_tree.cpp"
#include <vector>
#include <cassert>
using namespace std;

// LazySegmentTree
namespace titan23 {

    template <class T,
            T (*op)(T, T),
            T (*e)(),
            class F,
            T (*mapping)(F, T),
            F (*composition)(F, F),
            F (*id)()>
    class LazySegmentTree {
      private:
        int n, log, size;
        vector<T> data;
        vector<F> lazy;

        static int bit_length(const int x) {
            if (x == 0) return 0;
            return 32 - __builtin_clz(x);
        }

        void update(int k) {
            data[k] = op(data[k << 1], data[k << 1 | 1]);
        }

        void all_apply(int k, F f) {
            data[k] = mapping(f, data[k]);
            if (k >= size) return;
            lazy[k] = composition(f, lazy[k]);
        }

        void propagate(int k) {
            if (lazy[k] == id()) return;
            all_apply(k << 1, lazy[k]);
            all_apply(k << 1 | 1, lazy[k]);
            lazy[k] = id();
        }

        void upper_propagate(int l, int r) {
            for (int i = log; i > 0; --i) {
                if ((l>>i<<i) != l) {
                    propagate(l >> i);
                }
                if (((r>>i<<i) != r) && ( ((l>>i) != ((r-1)>>i)) || ((l>>i<<i) == l) )) {
                    propagate((r-1) >> i);
                }
            }
        }

      public:
        LazySegmentTree() : n(0), log(0), size(0) {}
        LazySegmentTree(int n) {
            this->n = n;
            this->log = bit_length(n-1);
            this->size = 1 << log;
            data.resize(this->size<<1, e());
            lazy.resize(this->size, id());
        }

        LazySegmentTree(const vector<T> a) {
            this->n = a.size();
            this->log = bit_length(n-1);
            this->size = 1 << log;
            data.resize(this->size<<1, e());
            for (int i = size; i < size+n; ++i) {
                data[i] = a[i-size];
            }
            for (int i = size-1; i > 0; --i) {
                data[i] = op(data[i<<1], data[i<<1|1]);
            }
            lazy.resize(this->size, id());
        }

        void apply_point(int k, F f) {
            k += size;
            for (int i = log; i > 0; --i) {
                propagate(k >> i);
            }
            data[k] = mapping(f, data[k]);
            for (int i = 1; i <= log; ++i) {
                update(k >> i);
            }
        }

        void apply(int l, int r, F f) {
            if (l == r || f == id()) return;
            l += size;
            r += size;
            upper_propagate(l, r);
            int l2 = l, r2 = r;
            while (l < r) {
                if (l & 1) {
                    all_apply(l, f);
                    l += 1;
                }
                if (r & 1) {
                    all_apply(r ^ 1, f);
                }
                l >>= 1;
                r >>= 1;
            };
            int l3 = l2, r3 = r2 - 1;
            for (int i = 1; i <= log; ++i) {
                l3 >>= 1;
                r3 >>= 1;
                if ((l3 << i) != l2) {
                    update(l3);
                }
                if ((((l3 << i) == l2) || (l3 != r3)) && ((r2>>i<<i) != r2)) {
                    update(r3);
                }
            }
        }

        void all_apply(F f) {
            lazy[1] = f == id() ? f : composition(f, lazy[1]);
        }

        T prod(int l, int r) {
            if (l == r) return e();
            l += size;
            r += size;
            upper_propagate(l, r);
            T lres = e(), rres = e();
            while (l < r) {
                if (l & 1) lres = op(lres, data[l++]);
                if (r & 1) rres = op(data[r^1], rres);
                l >>= 1;
                r >>= 1;
            }
            return op(lres, rres);
        }

        T all_prod() const {
            return data[1];
        }

        T get(int k) {
            k += size;
            for (int i = log; i > 0; --i) {
                propagate(k >> i);
            }
            return data[k];
        }

        void set(int k, T v) {
            k += size;
            for (int i = log; i > 0; --i) {
                propagate(k >> i);
            }
            data[k] = v;
            for (int i = 1; i <= log; ++i) {
                update(k >> i);
            }
        }

        template<typename Func>
        int max_right(int l, Func f) {
            assert(0 <= l <= n);
            if (l == size) return n;
            l += size;
            for (int i = log; i > 0; --i) {
                propagate(l>>i);
            }
            T s = e();
            while (true) {
                while (l & 1 == 0) {
                    l >>= 1;
                }
                if (!f(op(s, data[l]))) {
                    while (l < size) {
                        propagate(l);
                        l <<= 1;
                        if (f(op(s, data[l]))) {
                            s = op(s, data[l]);
                            l |= 1;
                        }
                    }
                    return l - size;
                }
                s = op(s, data[l]);
                l++;
                if (l & (-l) == l) break;
            }
            return n;
        }

        template<typename Func>
        int min_left(int r, Func f) {
            assert(0 <= r <= n);
            if (r == 0) return 0;
            r += size;
            for (int i = log; i > 0; --i) {
                propagate((r - 1) >> i);
            }
            T s = e();
            while (true) {
                r -= 1;
                while ((r > 1) && (r & 1)) {
                    r >>= 1;
                }
                if (!f(op(data[r], s))) {
                    while (r < size) {
                        propagate(r);
                        r = r << 1 | 1;
                        if (f(op(data[r], s))) {
                            s = op(data[r], s);
                            r ^= 1;
                        }
                    }
                    return r + 1 - size;
                }
                s = op(data[r], s);
                if (r & (-r) == r) break;
            }
            return 0;
        }

        vector<T> tovector() {
            for (int i = 1; i < size; ++i) {
                propagate(i);
            }
            vector<T> res(data.begin()+size, data.begin()+size+n);
            return res;
        }
    };
} // namespace titan23


ll op(ll s, ll t) { return max(s, t); }
ll mapping(ll f, ll t) { return f + t; }
ll comp(ll s, ll t) { return s + t; }
ll e() { return -1e11; }
ll id() { return 0; }

void solve() {
    ll n, c, p, q;
    cin >> n >> c >> p >> q;
    string S; cin >> S;
    int ans = 0;
    vector<ll> init(n, 0);
    titan23::LazySegmentTree<ll, op, e, ll, mapping, comp, id> seg(init);
    int rank_idx = 0;
    ll x = 0;
    rep(e, n) {
        if (S[e] == 'Y') x++;
        if (e-c+1 >= 0 && S[e-c+1] == 'Y') x--;
        if (e-c+1 >= 0) {
            seg.apply(0, e-c+2, (S[e-c+1] == 'Y' ? q-p : -p));
        }
        ll mx = seg.prod(rank_idx, max(0ll, e-c+2));
        if (e-rank_idx+1 >= 0 && mx >= -q*x+p*(c-1)) {
            ans++;
            rank_idx = e+1;
        }
    }
    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);
    cerr << fixed << setprecision(15);

    int t = 1;
    // cin >> t;
    for (int i = 0; i < t; ++i) {
        solve();
    }

    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 24036
final_rank: 17

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(){
  cin.tie(0)->sync_with_stdio(0);
  int n,c,p,q;string s;cin>>n>>c>>p>>q>>s;
  int ans=0;
  int prev=-1<<30;
  for(int r=1;r<=n;++r){
    int cnt=0;
    for(int l=r-1;0<=l&&r-l<2*c;--l){
      int len=r-l;
      if(s[l]=='Y')++cnt;
      // p / q <= cnt / len
      if(c<=len&&p*len<=cnt*q){
        if(prev<=l){
          ++ans;
          prev=r;
        }
        break;
      }
    }
  }
  cout<<ans<<endl;
}
accuracy: 1
time_ms: 190
memory_kb: 3732
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(){
  cin.tie(0)->sync_with_stdio(0);
  using ll=long long;
  int n,c;ll p,q;string s;cin>>n>>c>>p>>q>>s;
  vector<ll>cum(n+1);
  for(int i=0;i<n;++i)cum[i+1]=cum[i]+(s[i]=='Y');
  auto check=[&](int l,int r){
    // p / q <= (cum[r] - cum[l]) / (r - l)
    return p*(r-l)<=(cum[r]-cum[l])*q;
  };
  vector<pair<int,int>>rl;
  for(int l=0;l+c<=n;++l){
    int r=l+c;
    if(check(l,r)){
      rl.emplace_back(r,l);
      continue;
    }
    for(;r<=n&&r<=l+2*c;++r){
      if(check(l,r)){
        rl.emplace_back(r,l);
        break;
      }
    }
  }
  sort(rl.begin(),rl.end());
  int cur=-1<<30;
  int ans=0;
  for(auto[r,l]:rl){
    if(cur<=l){
      cur=r;
      ++ans;
    }
  }
  cout<<ans<<endl;
}
accuracy: 1
time_ms: 70
memory_kb: 13600
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i,l,r) for(ll i=l;i<r;i++)
#define REP(i,n) FOR(i,0,n)
struct segtree{
    ll op(ll a,ll b){return min(a,b);}
    ll e(){return 1e18;}
    vector<ll>seg;
    ll n;
    segtree(ll N){
        n=1;
        while(n<N)n<<=1;
        seg.assign(2*n,e());
    }
    void set(ll i,ll x){
        i+=n;
        seg[i]=x;
        while(i>1){
            i>>=1;
            seg[i]=op(seg[2*i],seg[2*i+1]);
        }
    }
    ll prod(ll l,ll r){
        l+=n;r+=n;
        ll L=e(),R=e();
        while(r>l){
            if(l%2)L=op(L,seg[l]),l++;
            if(r%2)r--,R=op(seg[r],R);
            l>>=1;r>>=1;
        }
        return op(L,R);
    }
};
int main(){
    int N,C,P,Q;cin>>N>>C>>P>>Q;
    string S;cin>>S;
    vector<ll>A(N);
    REP(i,N)A[i]=(S[i]=='Y'?Q-P:-P);
    vector<ll>B(N+1);
    REP(i,N)B[i+1]=A[i]+B[i];
    segtree seg(N+1);
    REP(i,N+1)seg.set(i,B[i]);
    ll j=0,rank=0;
    REP(i,N){
        if(i<j+C-1)continue;
        if(seg.prod(j,i+2-C)<=B[i+1])rank++,j=i+1;
    }
    cout<<rank<<endl;
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 19648
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto&& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;

void chmax(ll &a, ll b) { a = max(a, b); }
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, c, p, q;
    cin >> n >> c >> p >> q;
    string s; cin >> s;
    vector<ll> dp(n + 1, 0LL);
    for(int i = 1; i <= n; i ++) {
        dp[i] = dp[i - 1];
        if(i >= c) {
            ll lim = min((ll)i, c * 2); 
            ll x = 0, y = 0;
            for(ll j = i; j > i - lim; j --) {
                if(s[j - 1] == 'Y') x ++;
                y ++;
                if(i - j + 1 >= c) {
                    if(q * x >= y * p) {
                        chmax(dp[i], dp[j - 1] + 1);
                    }
                }
            }
        }
        // cout << dp[i] << endl;
    }
    cout << dp[n] << endl;
    return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 7476
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);

  ll n, c, p, q;
  string S;
  cin >> n >> c >> p >> q >> S;

  int rank = 0;
  int lastPromotion = -1;
  vi hist;
  rep(i, 0, n) {
    hist.push_back(S[i] == 'Y');
    if (i - lastPromotion < c) continue;
    ll cnt = 0;
    for (int j = sz(hist) - 1; j >= max(0ll, sz(hist) - 3 * c); j--) {
      ll len = sz(hist) - j;
      cnt += hist[j];
      if (len < c) continue;
      if (cnt * q >= p * len) {
        hist.clear();
        rank++;
        lastPromotion = i;
        break;
      }
    }
  }

  cout << rank << endl;
}
accuracy: 1
time_ms: 240
memory_kb: 5748
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
	int n, c, p, q;
	cin >> n >> c >> p >> q;
	string s;
	cin >> s;
	vector<int> sum(n + 1, 0);
	for (int i = 0; i < n; i++) {
		if (s[i] == 'Y') sum[i + 1]++;
	}
	for (int i = 0; i < n; i++) {
		sum[i + 1] += sum[i];
	}
	auto cnt = [&](int l, int r) {
		// [l,r)
		return sum[r] - sum[l];
	};
	vector<int> dp(n + 1, -1);
	dp[0] = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = max(0, i - c * 2 - 5); j + c <= i; j++) {
			// [j,i)
			int l = i - j;
			// if (cnt(j + 1, i + 1) / l >= p / q)
			if ((ll)cnt(j, i) * q >= (ll)p * l) {
				dp[i] = max(dp[i], dp[j] + 1);
			}
		}
		dp[i] = max(dp[i], dp[i - 1]);
	}
	// for (int i = 0; i <= n; i++) {
	// 	cerr << dp[i] << " ";
	// }
	// cerr << endl;
	cout << dp[n] << endl;
	return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 7556
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T>
bool chmax(T& a, const T& b){
  return (a < b) ? a = b, true : false;
}

constexpr ll INF = 1LL << 60;

int main(){
  ll N, C, P, Q;
  cin >> N >> C >> P >> Q;

  string S;
  cin >> S;

  vector<ll> score(N);
  for(int i = 0; i < N; i++){
    score[i] = (S[i] == 'Y');
  }

  int ans = 0;

  ll ma = -INF, add = 0;
  queue<ll> que;

  for(int i = 0; i < N; i++){
    ll diff = Q * score[i] - P;

    if(-INF < ma){
      ma += diff;
    }
    add += diff;
    que.emplace(diff - add);

    while(C <= (ll)que.size()){
      chmax(ma, que.front() + add);
      que.pop();
    }

    if(0 <= ma){
      ans++;
      ma = -INF;

      while(!que.empty()){
        que.pop();
      }
    }
  }

  cout << ans << "\n";

  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 7716
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

// l <= r ?
bool cmp(pair<int, int> l, pair<int, int> r) {
    auto [p1, q1] = l;
    auto [p2, q2] = r;
    return ll(p1) * ll(q2) <= ll(p2) * ll(q1);
}

bool cmp_eq(pair<int, int> l, pair<int, int> r) {
    auto [p1, q1] = l;
    auto [p2, q2] = r;
    return ll(p1) * ll(q2) == ll(p2) * ll(q1);
}

int main() {
    int N, C, P, Q;
    cin >> N >> C >> P >> Q;
    string S;
    cin >> S;

    vector<int> y_cnt(N + 1), n_cnt(N + 1);
    for (int i = 0; i < N; i++) {
        if (S[i] == 'Y') y_cnt[i + 1]++;
        else if (S[i] == 'N') n_cnt[i + 1]++;
    }
    for (int i = 0; i < N; i++) {
        y_cnt[i + 1] += y_cnt[i];
        n_cnt[i + 1] += n_cnt[i];
    }

    int rank = 0;
    int prev = -1;
    pair<int, int> ratio{P, Q};
    vector<pair<int, int>> bests; // size <= 2
    for (int i = 0; i < N; i++) {
        if (i - C >= prev) {
            int y = y_cnt[i + 1] - y_cnt[i - C + 1];
            int n = n_cnt[i + 1] - n_cnt[i - C + 1];
            pair<int, int> item{y, n + y};
            // cerr << "# item = " << y << ", " << y + n << endl;
            // bests からの遷移
            vector<pair<int, int>> nxts;
            nxts.push_back(item);
            for (auto best: bests) {
                // cerr << "# best = " << best.first << ", " << best.second << endl;
                best.second += 1;
                if (S[i] == 'Y') best.first += 1;
                if (cmp_eq(item, best)) {
                    nxts.push_back(best);
                }
                else if (cmp(item, best)) {
                    nxts.clear();
                    nxts.push_back(best);
                    item = best;
                }
            }
            // cerr << "# " << i << ", nxts: ";
            // for (auto item: nxts) {
            //     cerr << "(" << item.first << ", " << item.second << "), ";
            // }
            // cerr << endl;
            if (cmp(ratio, nxts.front())) {
                prev = i;
                rank++;
                bests.clear();
            } else {
                bests = nxts;
            }
        } 
    }
    cout << rank << endl;
}
accuracy: 0.6222222222222222
time_ms: 20
memory_kb: 7520
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(ll i = 0;i < (ll)(n);i++)

int main() {
  ll n,C,P,Q;cin >> n>> C >> P >> Q;
  string s;cin >> s;
  vector<ll> prom(n);
  ll lv = 0;
  rep(r,n)if (r >= C-1) {
    ll l = r;
    ll ysum = 0;
    while (l >= 0 && abs(r-l)<= 2 * C + 1) {
      if (prom[l] == 1) {
        break;
      }
      if (s[l] == 'Y') {
        ysum++;
      }
      if (abs(r-l)+1 >= C) {
        ll len = r - l + 1;
        if (ysum * Q >= P * len) {
          prom[r] = 1;
          lv++;
          break;
        }
      }
      l--;
    }
  }
  cout << lv << endl;
}
accuracy: 1
time_ms: 290
memory_kb: 7760
final_rank: 16

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <iostream>
using namespace std;

int main () {
    long long n, c;
    double p, q;
    cin >> n >> c >> p >> q;
    string S;
    cin >> S;

    vector<long long> psum(n+1, 0);
    for (long long i = 0; i < n; ++i) {
        psum[i+1] = psum[i];
        if (S[i] == 'Y') ++psum[i+1];
    }

    long long last_ru = 0, rank = 0;
    for (long long i = 0; i < n; ++i) {
        for (long long s = i; i-s+1 <= 2*c && s >= last_ru; --s) {
            if (i-s+1 >= c && (double)(psum[i+1]-psum[s]) / (i-s+1) >= p/q) {   
                ++rank;
                last_ru = i+1;
                break;
            }
        }
    }
    cout << rank << endl;
}
accuracy: 1
time_ms: 740
memory_kb: 7496
final_rank: 19

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <iostream>
using namespace std;

int main () {
    long long n, c, p, q;
    cin >> n >> c >> p >> q;
    string S;
    cin >> S;

    vector<long long> psum(n+1, 0);
    for (long long i = 0; i < n; ++i) {
        psum[i+1] = psum[i]-p;
        if (S[i] == 'Y') psum[i+1] += q;
    }

    long long last_ru = 0, rank = 0;
    for (long long i = 0; i < n; ++i) {
        for (long long s = i; s >= last_ru && i-s+1 <= 2*c; --s) {
            if (i-s+1 >= c && psum[i+1]-psum[s] >= 0) {   
                ++rank;
                last_ru = i+1;
                break;
            }
        }
    }
    cout << rank << endl;
}
accuracy: 1
time_ms: 130
memory_kb: 7384
final_rank: 5

code_snippet: /*
*　所要時間:
*　　問題読み:4分
*  　コーディング:35分
*
*/

#include <iostream>
#include <string>
#include <vector>
#include <iostream>
using namespace std;

int main () {
    long long n, c, p, q;
    cin >> n >> c >> p >> q;
    string S;
    cin >> S;

    vector<long long> psum(n+1, 0);
    for (long long i = 0; i < n; ++i) {
        psum[i+1] = psum[i];
        if (S[i] == 'Y') ++psum[i+1];
    }

    long long last_ru = 0, rank = 0;
    for (long long i = 0; i < n; ++i) {
        for (long long s = i - 2*c; i-s+1 >= c; ++s) {
            if (s >= last_ru && (psum[i+1]-psum[s]) * q >= (i-s+1) * p) {   
                ++rank;
                last_ru = i+1;
                break;
            }
        }
    }
    cout << rank << endl;
}
accuracy: 1
time_ms: 80
memory_kb: 7380
final_rank: 2