题目链接:https://codeforces.com/contest/1427/problem/B

题意

给出一个长为 \(n\) 由 W, L 组成的字符串,如果一个 W 左侧为 W,则它提供 2 分,否则为 1 分。最多可以将 \(k\) 个 L 变为 W,问字符串可以得到的最大分值。

题解

本题的关键是字符串中 W 的有无及两两构成的封闭区间长度。

  • 如果全为 L,则字符串的最大分值为 \(max(2k-1,\ 0)\) 。
  • 如果存在 W,则每次操作都会至少增加 2 分,如果操作的为两个 W 区间内的最后一个 L,则会额外再增加 1 分。

    所以计算字符串的初始分值,加上 \(2 \times min(k, cntL)\) 分,然后区间长度排序,每当可以减去一个完整区间长就再加上 1 分。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
string s;
cin >> s;
int cntL = count(s.begin(), s.end(), 'L');
if (cntL == n) {
cout << max(2 * k - 1, 0) << "\n";
continue;
}
vector<int> posW;
for (int i = 0; i < n; i++)
if (s[i] == 'W') posW.push_back(i);
vector<int> seg;
for (int i = 1; i < int(posW.size()); i++)
if (posW[i] - posW[i - 1] - 1 > 0) seg.push_back(posW[i] - posW[i - 1] - 1);
int ans = s[0] == 'W';
for (int i = 1; i < n; i++)
if (s[i] == 'W') ans += s[i - 1] == 'W' ? 2 : 1;
ans += 2 * min(k, cntL);
sort(seg.begin(), seg.end());
for (auto len : seg) if (k >= len) k -= len, ans += 1;
cout << ans << "\n";
}
return 0;
}

最新文章

  1. shared_ptr
  2. SQL 还原数据库
  3. httpclient提示Cookie rejected: violates RFC 2109: domain must start with a dot
  4. 【转】Intel RealSense(实感技术)概览
  5. MATLAB基本命令
  6. Ruby on Rails Tutorial 第六章 用户模型
  7. error LNK2001: 无法解析的外部符号 _IID_IDirectDraw7
  8. open_table与opened_table --2
  9. 基于Equinox构建OSGi项目
  10. 在C#中使用 Win32 和其他库
  11. 标准IO:常用函数集合
  12. Mahout系列之----距离度量
  13. redis注册成window服务
  14. 《Python 数据科学实践指南》读书笔记
  15. mysql 解压版安装
  16. 替换JDK 对eclipse的影响?
  17. 一个CLR20r3 错误解决。
  18. Oracle&amp;SQLServer中实现跨库查询
  19. pycharm shortcut
  20. CPU简单科普

热门文章

  1. 瞄到BindingGroup用法
  2. Linux下安装Oracle11g服务器【转】
  3. Docker-ce Centos8 笔记一:安装Docker-ce
  4. Docker-ce Centos8 笔记二:常见问题
  5. 【Python】在CentOS6.8中安装pip9.0.1和setuptools33.1
  6. SDUST数据结构 - chap2 线性表
  7. mysql—information_schema数据库
  8. js reduce数组转对象
  9. openpose c++ 配置教程 + python api
  10. Docker 建站小记