说在前面
这题不是很难,对代码能力和思维能力的要求有点高。这里分享一种简单粗暴的做法。就是代码有点长(反正我代码挺长)。
题目分析
容易发现,如果相邻的两头奶牛形成了 的形式,那么这两头奶牛的奶量永远不会减少。如果有其他奶牛把牛奶传进来,那么奶量一定会减少。我们把样例三中的传递路径画出来,就是这样的:
不难发现,用绿框框起来的奶牛奶量会减少。当绿框里的奶牛的牛奶消耗完后,会接着消耗它下面的一个结点的奶牛的牛奶,以此类推,直到这一条路径消耗完,下面的环不会被消耗。
具体点说,就是 最左边的节点和 最右边的结点的奶量会被消耗。
所以,思路来了:
- 以 作为块与块的分隔符,对序列进行分块。
- 把每个小块里面再分成 和 的小块,以它作为最终的块。
- 如果可以的话,首尾相连。
- 计算答案。
那答案怎么计算呢?
对于每个块,优先消耗它最左边或最右边的奶牛的牛奶,如果不够,就消耗下一头奶牛,直到这一块消耗完或时间结束。把每一个块消耗的总牛奶量加起来就是答案。
注意事项
- 这是一个环!!!
- 处理每个块时优先处理第一块(因为第一块最特殊,他是首尾相连的块)。
- 如果整体是一个大环,也不会消耗牛奶。
完整代码(真的有点长,165行)
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 200000 + 5;
int a[N], belong[N], st[N], ed[N];
int n, m, tot;
char x[N];
signed main() {
cin >> n >> m;
cin >> (x + 1);
memset(st, 0x3f, sizeof st);
int ans = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
ans += a[i];
}
int tmp = 0;
if (x[1] == 'L' && x[n] == 'R') { // 因为是个环,判一下首尾是否能组成不会消耗牛奶的结构。
tot++;
tmp++;
}
int sta = tmp + 1, en = n - tmp, fr = 0x3f3f3f3f3f3f3f3f;
for (int i = sta; i <= en; i++) {
if (x[i] == 'R' && x[i + 1] == 'L') {
tot++;
i++;
tmp++;
continue;
}
if (x[i] != x[i - 1]) {
++tot;
}
fr = min(fr, tot); // 记录第一个块
belong[i] = tot; // 记录每一位所在的块的位置
}
if (n % 2 == 0 && tmp == n / 2) { // 如果全是不会消耗牛奶的结构
cout << ans;
return 0;
}
for (int i = 1; i <= n; i++) { // 记录块的起始与结束
st[belong[i]] = min(st[belong[i]], i);
ed[belong[i]] = max(ed[belong[i]], i);
}
if (tot == 1 && st[1] == 1 && ed[1] == n) { // 如果整体是一个大环
cout << ans;
return 0;
}
if (x[1] == x[n]) { // 首尾相连
st[belong[1]] = st[belong[n]];
for (int i = st[belong[n]]; i <= ed[belong[n]]; i++) {
belong[i] = belong[1];
}
tot--;
}
if (x[st[fr]] == 'R') { // 处理第一块,因为是首尾相连,所以起始位置可能大于结束位置
if (st[fr] > ed[fr]) {
int t = m;
bool flag = false;
for (int i = st[fr]; i <= n; i++) { // 计算答案
if (t > a[i]) {
t -= a[i];
ans -= a[i];
} else {
flag = true;
ans -= t;
break;
}
}
if (!flag) {
for (int i = 1; i <= ed[fr]; i++) {
if (t > a[i]) {
t -= a[i];
ans -= a[i];
} else {
ans -= t;
break;
}
}
}
} else {
int t = m;
for (int i = st[fr]; i <= ed[fr]; i++) {
if (t > a[i]) {
ans -= a[i];
t -= a[i];
} else {
ans -= t;
break;
}
}
}
} else {
if (st[fr] > ed[fr]) {
int t = m;
bool flag = false;
for (int i = ed[fr]; i >= 1; i--) {
if (t > a[i]) {
t -= a[i];
ans -= a[i];
} else {
flag = true;
ans -= t;
break;
}
}
if (!flag) {
for (int i = n; i >= st[fr]; i--) {
if (t > a[i]) {
t -= a[i];
ans -= a[i];
} else {
ans -= t;
break;
}
}
}
} else {
int t = m;
for (int i = ed[fr]; i >= st[fr]; i--) {
if (t > a[i]) {
ans -= a[i];
t -= a[i];
} else {
ans -= t;
break;
}
}
}
}
for (int i = fr + 1; i <= tot; i++) {
if (st[i] == 0x3f3f3f3f3f3f3f3f || ed[i] == 0) continue; // 一定要注意这个地方,因为块的编号不是连续的,不加这句会 RE
if (x[st[i]] == 'R') {
int t = m;
for (int j = st[i]; j <= ed[i]; j++) {
if (t > a[j]) {
ans -= a[j];
t -= a[j];
} else {
ans -= t;
break;
}
}
} else {
int t = m;
for (int j = ed[i]; j >= st[i]; j--) {
if (t > a[j]) {
ans -= a[j];
t -= a[j];
} else {
ans -= t;
break;
}
}
}
}
cout << ans;
return 0;
}注:后来发现不用这么麻烦,只需要把块内的的都加起来,直接用和去减就行了。
第二种代码(这下简单得多了)
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 200000 + 5;
int a[N], belong[N], st[N], ed[N], sum[N];
int n, m, tot;
char x[N];
signed main() {
cin >> n >> m;
cin >> (x + 1);
memset(st, 0x3f, sizeof st);
int ans = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
ans += a[i];
}
int tmp = 0;
if (x[1] == 'L' && x[n] == 'R') { // 因为是个环,判一下首尾是否能组成不会消耗牛奶的结构。
tot++;
tmp++;
}
int sta = tmp + 1, en = n - tmp, fr = 0x3f3f3f3f3f3f3f3f;
for (int i = sta; i <= en; i++) {
if (x[i] == 'R' && x[i + 1] == 'L') {
tot++;
i++;
tmp++;
continue;
}
if (x[i] != x[i - 1]) {
++tot;
}
fr = min(fr, tot); // 记录第一个块
belong[i] = tot; // 记录每一位所在的块的位置
}
if (n % 2 == 0 && tmp == n / 2) { // 如果全是不会消耗牛奶的结构
cout << ans;
return 0;
}
for (int i = 1; i <= n; i++) { // 记录块的起始与结束
st[belong[i]] = min(st[belong[i]], i);
ed[belong[i]] = max(ed[belong[i]], i);
}
if (tot == 1 && st[1] == 1 && ed[1] == n) { // 如果整体是一个大环
cout << ans;
return 0;
}
if (x[1] == x[n]) { // 首尾相连
st[belong[1]] = st[belong[n]];
for (int i = st[belong[n]]; i <= ed[belong[n]]; i++) {
belong[i] = belong[1];
}
tot--;
}
for (int i = 1; i <= n; i++) {
sum[belong[i]] += a[i];
}
for (int i = 1; i <= tot; i++) {
if (st[i] == 0x3f3f3f3f3f3f3f3f || ed[i] == 0) continue; // 一定要注意这个地方,因为块的编号不是连续的,不加这句会 RE
ans -= min(sum[i], m);
}
cout << ans;
return 0;
}于是这篇简单粗暴又不失优雅的题解到此结束。
说在后面
果然,越粗暴的做法越复杂。(但是好理解,所以我选择粗暴的做法)
若题目或代码讲解有误,欢迎提出。
