Codeforces Global Round 11 B. Chess Cheater(贪心)
题目链接: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;
}
Codeforces Global Round 11 B. Chess Cheater(贪心)的更多相关文章
- Codeforces Global Round 11 B. Chess Cheater (贪心,结构体排序)
题意:你和朋友进行了\(n\)个回合的棋艺切磋,没有平局,每次要么输要么赢,每次赢可以得一分,假如前一局也赢了,那么可以得两分,结果已成定局,但是你确可以作弊,最多修改\(k\)个回合的结果,问你作弊 ...
- Codeforces Global Round 11 个人题解(B题)
Codeforces Global Round 11 1427A. Avoiding Zero 题目链接:click here 待补 1427B. Chess Cheater 题目链接:click h ...
- Codeforces Global Round 11 A~D题解
A.Avoiding Zero 题目链接:https://codeforces.ml/contest/1427 题目大意:给定一个数组a1,a2...,an,要求找出一个a重排后的数组b1,b2,.. ...
- Codeforces Beta Round #11 A. Increasing Sequence 贪心
A. Increasing Sequence 题目连接: http://www.codeforces.com/contest/11/problem/A Description A sequence a ...
- Codeforces Global Round 11【ABCD】
比赛链接:https://codeforces.com/contest/1427 A. Avoiding Zero 题意 将 \(n\) 个数重新排列使得不存在为 \(0\) 的前缀和. 题解 计算正 ...
- Codeforces Global Round 11 D. Unshuffling a Deck(构造/相邻逆序对)
题目链接:https://codeforces.com/contest/1427/problem/D 题意 给出一个大小为 \(n\) 的排列,每次操作可以将 \(n\) 个数分为 \(1 \sim ...
- Codeforces Global Round 11 C. The Hard Work of Paparazzi(dp/最长上升子序列)
题目链接:https://codeforces.com/contest/1427/problem/C 题意 \(r\) 行与 \(r\) 列相交形成了 \(r \times r\) 个点,初始时刻记者 ...
- Codeforces Global Round 11 A. Avoiding Zero(前缀和)
题目链接:https://codeforces.com/contest/1427/problem/A 题意 将 \(n\) 个数重新排列使得不存在为 \(0\) 的前缀和. 题解 计算正.负前缀和,如 ...
- Codeforces Global Round 8 A. C+=(贪心)
题目链接:https://codeforces.com/contest/1368/problem/A 题意 给出 $a,b$,只可以使用 '+=' 运算符,问至少要使用多少次使得 $a$ 或 $b$ ...
随机推荐
- 【模拟】P1143进制转换
题目相关 题目描述 请你编一程序实现两种不同进制之间的数据转换. 输入格式 共三行,第一行是一个正整数,表示需要转换的数的进制n(2≤n≤16),第二行是一个n进制数,若n>10则用大写字母A- ...
- WPF Line 的颜色过度动画
<Line Grid.Column="2" Grid.ColumnSpan="2" VerticalAlignment="Center" ...
- vagrant up报错【io.rb:32:in `encode': "\x95" followed by "\"" on GBK (Encoding::InvalidByteSequenceError)】
vagrant up报错[io.rb:32:in `encode': "\x95" followed by """ on GBK (Encoding: ...
- 使用modify修改内表
modify修改内表,有这样一种方式,MODIFY TABLE itab FROM wa [TRANSPORTING ..]. 然后这里的内表itab是有条件的,这个itab必须要有table key ...
- 干电池升压3.3V的电源芯片
PW5100适用于一节干电池升压到3.3V,两节干电池升压3.3V的升压电路,PW5100干电池升压IC. 干电池1.5V和两节干电池3V升压到3.3V的测试数据 两节干电池输出500MA测试: PW ...
- Navicat linux 官方最新版安装破解
我们直接去官网下载linux版navicat 下载好之后执行命令打开软件 chmod +x navicat15-premium-cs.AppImage ./navicat15-premium-cs.A ...
- matlab图像处理程序大集合
1.图像反转 MATLAB程序实现如下:I=imread('xian.bmp');J=double(I);J=-J+(256-1); %图像反转线性变换H=uint8( ...
- Vue之事件绑定
Vue事件绑定 点击事件 @click="事件名" or v-on:click="事件名" 结构部分: <el-button type="pri ...
- linux/git常用命令收集中
1.进入文件夹 cd 文件名 进入某个文件 cd .. 返回上一级目录 cd / 进入根目录 cd ~ 切换到当前 cd - 切换到上一个目录 2.查看 pwd 文件名 查看路 ...
- asctime_s asctime
asctime_s asctime // rand随机数.cpp : 此文件包含 "main" 函数.程序执行将在此处开始并结束. // #include "pch.h ...