Dmaxiya

[蓝桥杯 2026 省 B] LQ 聚合 参考代码

Apr 11th, 2026
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define endl '\n'
  5.  
  6. typedef long long LL;
  7. const int maxn = 100000 + 100;
  8. int n;
  9. LL cntL, cntQ, ans, tmp;
  10. string str;
  11.  
  12. int main() {
  13.     ios::sync_with_stdio(false);
  14.  
  15.     cin >> n >> str;
  16.     for (int i = 0; i < n; ++i) {
  17.         if (str[i] == 'L') {
  18.             ++cntL;
  19.         } else {
  20.             tmp += cntL;
  21.             ++cntQ;
  22.         }
  23.     }
  24.     ans = tmp;
  25.     cntL = 0;
  26.     for (int i = 0; i < n; ++i) {
  27.         if (str[i] == 'L') {
  28.             ++cntL;
  29.         } else if (str[i] == 'Q') {
  30.             --cntQ;
  31.         } else {
  32.             --cntQ;
  33.             tmp = tmp - cntL + cntQ;
  34.             ans = max(ans, tmp);
  35.             ++cntL;
  36.         }
  37.     }
  38.     cout << ans << endl;
  39.  
  40.     return 0;
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment