International Collegiate Programming Contest 2019 Latin American Regional Contests E. Eggfruit Cake(思维/尺取)
Today is Jaime’s birthday and, to celebrate, his friends ordered a cake decorated with eggfruits and persimmons. When the cake arrived, to their surprise, they noticed that the bakery didn’t use equal amounts of eggfruits and persimmons, but just randomly distributed the fruits over the cake’s border instead. Jaime eats persimmons every day, so he was eager to try some eggfruit on his birthday. However, as he doesn’t want to eat too much, his cake slice should be decorated with at most S fruits. Since Jaime doesn’t like when a fruit is cut into parts, each fruit should either be entirely in his slice or be left in the rest of the cake. The problem is, with the fruits distributed in such a chaotic order, his friends are having trouble cutting a suitable slice for him. Jaime is about to complain that his friends are taking too long to cut his slice, but in order to do so, he needs to know how many different slices with at least one eggfruit and containing at most S fruits there are. A slice is defined just based on the set of fruits it contains. As Jaime is quite focused on details, he is able to distinguish any two fruits, even if both fruits are of the same type. Hence, two slices are considered different when they do not contain exactly the same set of fruits. The following picture shows a possible cake, as well as the six different slices with at most S = 2 fruits that can be cut from it.
Input The first line contains a circular string B (3 ≤|B|≤ 105) describing the border of the cake. Each character of B is either the uppercase letter “E” or the uppercase letter “P”, indicating respectively that there’s an eggfruit or a persimmon at the border of the cake. The second line contains an integer S (1 ≤ S < |B|) representing the maximum number of fruits that a slice can contain. Output Output a single line with an integer indicating the number of different slices with at most S fruits and at least one eggfruit.
Sample input 1
PEPEP
2
Sample output 1
6
Sample input 2
EPE
1
Sample output 2
2
Sample input 3
PPPP
1
Sample output 3
0
Sample input 4
EPEP
2
Sample output 4
6
大意就是给一个包含P和E的字符串(构成一个环),问有几段至少包含一个E的长度小于等于S的子段。
首先想到的是对于每个单个的E直接往左右两边找并累加答案,但很容易发现会重复。比如PPPEPPPEPPP,左边的E已经算过一次EPPPE了,但右边的E还会再算一次。那么对于每个E,不妨正常往右累加答案,往左的时候累加到上一个E之前,因此可以直接计算出每个E到上一个E的距离,直接用这个距离计算对答案的净贡献即可,这样就避开了重复的部分。每个E对答案的贡献用等差数列求和再相减就行了。
网上好多题解都是尺取,没大懂QAQ...
#include <bits/stdc++.h>
#define int long long
using namespace std;
string b;
int s, len, ans = 0;
signed main()
{
cin >> b;
len = b.size();
b = b + b;
b = " " + b;//倍长一下方便处理
cin >> s;
int last = 0;
vector<int> v;
for(int i = len; i >= 1; i--)
{
if(b[i] == 'E')
{
last = i;
break;
}
}
for(int i = len + 1; i <= 2 * len; i++)
{
if(b[i] == 'E')
{
v.push_back(i - last);
last = i;
}
}
for(int i = 0; i < v.size(); i++)
{
if((s - v[i]) >= 0) ans += (1 + s) * s / 2 - (1 + s - v[i]) * (s - v[i]) / 2;//s - v[i]要是非负的才能减。
else ans += (1 + s) * s / 2;
}
cout << ans;
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!